Crates.io | scoped-callback |
lib.rs | scoped-callback |
version | 0.2.0 |
source | src |
created_at | 2019-12-25 10:50:12.388705 |
updated_at | 2020-02-09 17:11:21.393973 |
description | Register scoped functions with local references to 'static lifetime callbacks in a safe manner |
homepage | https://github.com/gardell/scoped-callback |
repository | https://github.com/gardell/scoped-callback |
max_upload_size | |
id | 192311 |
size | 33,646 |
Allows registering scoped functions with local borrows with code that expect
functions taking 'static
lifetimes.
Motivating example:
/// Function for registering a callback with a `'static` lifetime.
fn register(callback: Box<dyn FnMut(i32)>) -> Box<dyn FnMut(i32)> {
callback
}
/// Function for de-registering the handle returned by `register`,
/// in this case the callback itself.
fn deregister(_callback: Box<dyn FnMut(i32)>) {}
/// Variable that can be borrowed from inside the callback closure
let a = 42;
/// After returning from the closure, `scope` guarantees that any callbacks
/// that have not yet been de-registered are de-registered.
scope(|scope| {
/// Register the given closure, which can borrow from the stack outside `scope`
/// using the `register` and `deregister` functions declared above.
/// The returned handle will cause a de-register when dropped.
let _registered = scope.register(
|_| {
let b = a * a;
println!("{}", b);
},
register,
deregister,
);
});
See scope_async and scope_async_local
as well for versions that work with async
scopes.
There are three important concepts in this implementation:
std::mem::forget
(which is not unsafe
!)
the de-registering using the provided function will instead happen after leaving the closure
passed to scope.panic!
.no_std
This crate supports no_std
by disabling its std
feature.
License: Apache-2.0