• Stars
    star
    429
  • Rank 101,271 (Top 2 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust crate for a convenient RAII scope guard.

scopeguard

Rust crate for a convenient RAII scope guard that will run a given closure when it goes out of scope, even if the code between panics (assuming unwinding panic).

The defer! macro and guard are no_std compatible (require only core), but the on unwinding / not on unwinding strategies require linking to std. By default, the use_std crate feature is enabled. Disable the default features for no_std support.

Please read the API documentation here.

Minimum supported Rust version: 1.20

build_status crates

How to use

#[macro_use(defer)]
extern crate scopeguard;

use scopeguard::guard;

fn f() {
    defer! {
        println!("Called at return or panic");
    }
    panic!();
}

use std::fs::File;
use std::io::Write;

fn g() {
    let f = File::create("newfile.txt").unwrap();
    let mut file = guard(f, |f| {
        // write file at return or panic
        let _ = f.sync_all();
    });
    // access the file through the scope guard itself
    file.write_all(b"test me\n").unwrap();
}

Recent Changes

  • 1.1.0

    • Change macros (defer!, defer_on_success! and defer_on_unwind!) to accept statements. (by @konsumlamm)
  • 1.0.0

    • Change the closure type from FnMut(&mut T) to FnOnce(T): Passing the inner value by value instead of a mutable reference is a breaking change, but allows the guard closure to consume it. (by @tormol)

    • Add defer_on_success!, guard_on_success() and OnSuccess strategy, which triggers when scope is exited without panic. It's the opposite to defer_on_unwind! / guard_on_unwind() / OnUnwind.

    • Add ScopeGuard::into_inner(), which "defuses" the guard and returns the guarded value. (by @tormol)

    • Implement Sync for guards with non-Sync closures.

    • Require Rust 1.20

  • 0.3.3

    • Use #[inline] on a few more functions by @stjepang (#14)
    • Add examples to crate documentation
  • 0.3.2

    • Add crate categories
  • 0.3.1

    • Add defer_on_unwind!, Strategy trait
    • Rename GuardScopeGuard
    • Add ScopeGuard::with_strategy.
    • ScopeGuard now implements Debug.
    • Require Rust 1.11
  • 0.2.0

    • Require Rust 1.6
    • Use no_std unconditionally
    • No other changes
  • 0.1.2

    • Add macro defer!

More Repositories

1

indexmap

A hash table with consistent order and fast iteration; access items by key or sequence index
Rust
1,361
star
2

arrayvec

A vector with a fixed capacity. (Rust)
Rust
714
star
3

maplit

Rust container / collection literal macros for HashMap, HashSet, BTreeMap, BTreeSet.
Rust
378
star
4

either

The enum Either with variants Left and Right is a general purpose sum type with two cases.
Rust
377
star
5

matrixmultiply

General matrix multiplication of f32 and f64 matrices in Rust. Supports matrices with general strides.
Rust
206
star
6

indexing

Sound unchecked indexing using “generativity”; a type system approach to indices, pointers and ranges that are trusted to be in bounds.
Rust
100
star
7

bencher

bencher is just a port of the libtest (unstable) benchmark runner to Rust stable releases. `cargo bench` on stable. "Not a better bencher!" = No feature development. Go build a better stable benchmarking library.
Rust
83
star
8

twoway

Twoway / Fast substring search for strings and byte strings (Rust) / Also assorted benchmarks and string search snippets
Rust
65
star
9

permutohedron

Rust
36
star
10

rustfaq

Bluss' Rust FAQ, intended to answer questions that are often encountered, and being simple to update. There's quite a lot of editing needed to get to a useful FAQ, so we'll see if we get there.
30
star
11

odds

Odds and ends — collection miscellania. Extra functionality for slices, strings and other things
Rust
21
star
12

fast-floats

Experimental (unstable) “fast-math” wrappers for f32, f64
Rust
19
star
13

pyproject-local-kernel

Python Jupyter kernel using pyproject environment manangers like Rye, Uv, PDM, Poetry, Hatch etc.
Python
15
star
14

defmac

A macro to define lambda-like macros inline.
Rust
15
star
15

ixlist

The “ixlist” is a linked list in a vector
Rust
11
star
16

itertools-num

[looking for adoption of the crate] Numerics iterator tools; cumulative sum and linearly spaced floats and such.
Rust
10
star
17

debugit

DebugIt: Use specialization so you can debug-print a value without explicit (and viral) Debug trait bound. (Not really maintained. Uses specialization so it may break)
Rust
10
star
18

thread-tree

A tree-shaped thread pool used for spawning stack-bound scoped jobs with no work stealing. Derived from a little bit of rayon-core code + new thread pool on top.
Rust
10
star
19

tox-rye

Use Rye to discover or fetch Python versions for Tox (v4)
Python
7
star
20

unchecked-index

Unchecked indexing and debug-assertion checked indexing through the regular index syntax.
Rust
7
star
21

qc.rs

quickcheck for Rust
Rust
6
star
22

simplesvg

Very simple drawing/diagramming library with svg output (Rust).
Rust
6
star
23

virtualenv-rye-discovery

Virtualenv plugin for discovery of Python interpreters through Rye.
Python
5
star
24

ryeish-kernel

Python Jupyter kernel using Rye for reproducible notebooks
Python
5
star
25

knuth-morris-pratt

Generic substring search for sequences. Requires elements that are equality comparable.
Rust
5
star
26

galil-seiferas

Galil-Seiferas algorithm: String search in constant space, linear time, for nonorderable alphabets
Rust
5
star
27

rawpointer

Extra methods for raw pointers
Rust
4
star
28

dlx

Dancing Links (“dlx”) solver for the exact cover problem, written in Rust. Can be used to create a sudoku solver.
Rust
4
star
29

rpasha

Restrictive Precise Angle Shadowcasting, a field of view algorithm for a discrete playing field.
Rust
4
star
30

rawslice

Rust
2
star
31

siphash-ppc-altivec

C
2
star
32

asprim

Rust library with a trait for the `as` operator for primitive numeric types.
Rust
2
star
33

blake2-ppc-altivec

BLAKE2s hash function https://blake2.net PowerPC/Altivec implementation by Ulrik Sverdrup
C
2
star
34

trait-group

Rust
2
star
35

blis-sys

[Feel free to adopt/fork this repo!] Experimental Rust bindings for BLIS
Rust
2
star
36

bmap

Experimental B-tree map in Rust using ArrayVec
Rust
1
star
37

rand_derive

(Obsolete) derive(Rand) for use with custom_derive!{}. Works with stable Rust 1.8+
Rust
1
star
38

numeric-loops

Rust
1
star
39

complexfloat

Extended traits for Rust Float and Complex types. Requires Rust 1.8.
Rust
1
star
40

bluss.github.io

HTML
1
star