https://github.com/dns2utf8/drop_guard
Catch everything as it is falling out of scope
https://github.com/dns2utf8/drop_guard
Last synced: 10 months ago
JSON representation
Catch everything as it is falling out of scope
- Host: GitHub
- URL: https://github.com/dns2utf8/drop_guard
- Owner: dns2utf8
- Created: 2017-07-13T07:44:09.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2023-10-29T16:41:21.000Z (over 2 years ago)
- Last Synced: 2025-01-13T08:50:27.586Z (over 1 year ago)
- Language: Rust
- Homepage:
- Size: 17.6 KB
- Stars: 3
- Watchers: 2
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# drop_guard
[](https://crates.io/crates/drop_guard)
[](https://docs.rs/drop_guard)
[](https://travis-ci.org/dns2utf8/drop_guard)
[](https://ci.appveyor.com/project/dns2utf8/drop-guard)
A RAII that executes you function whenever the wrapped object gets dropped.
## Use cases
Joining threads when they fall out of scope:
```rust
extern crate drop_guard;
use drop_guard::DropGuard;
use std::thread::{spawn, sleep};
use std::time::guard;
fn main() {
let _ = guard(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:
```bash
cargo run --example drop
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
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your discression.
### 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.