Giter Club home page Giter Club logo

scopeguard's Introduction

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 uwinding strategies requires linking to std.

Requires Rust 1.20.

Please read the API documentation here

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 OnUnwind / guard_on_unwind() / defer_on_unwind!{}.
    • 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 Guard โ†’ ScopeGuard
    • 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!()

scopeguard's People

Contributors

amanieu avatar bbigras avatar bluss avatar konsumlamm avatar reem avatar tormol avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.