Skip to content

Catch everything as it is falling out of scope

Notifications You must be signed in to change notification settings

oconnor663/drop_guard

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

drop_guard

crates.io doc.rs Build Status travis Build status appveyor

Use cases

Joining threads when they fall out of scope:

extern crate drop_guard;

use drop_guard::DropGuard;

use std::thread::{spawn, sleep};
use std::time::Duration;

fn main() {
    // The guard must have a name. _ will drop it instantly, which would lead to unexpected results
    let _g = DropGuard::new(spawn(move || {
                            sleep(Duration::from_secs(2));
                            println!("println! from thread");
                        })
                        , |join_handle| join_handle.join().unwrap());
    
    println!("Waiting for thread ...");
}

Examples

Feel free to run the included examples:

cargo run --example rainbow
cargo run --example thread
cargo run --example threadpool

Contribute

Contributions are very welcome. Feel free to bring your own ideas or continue one of these:

  • Add example/test with panic between guard and drop
  • Add example/test with panic in guard closure
  • Add minimal rustc version

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Catch everything as it is falling out of scope

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%