Crate diplomatic_bag
source · [−]Expand description
Diplomatic Bag
A mechanism for dealing with !Send
types when you really need
them to be Send
.
This library provides the DiplomaticBag<T>
type that is Send
and
Sync
even if the type it wraps is not. It does this by preventing direct
access to the wrapped type but instead provides methods for interacting with
it on a thread that it never leaves.
This is useful for when you have a !Send
type (usually an FFI
type) that you need store for a long period of time, and needs to be
accessible from multiple threads, for example, in async code.
Examples
// `Rc` is neither `Send` nor `Sync`
let foo = DiplomaticBag::new(|_| Rc::new(RefCell::new(0)));
std::thread::spawn({
let foo = foo.clone();
move || {
foo.as_ref().map(|_, rc| {
*rc.borrow_mut() = 1;
});
}
});
Now, being able to send an Rc
around isn’t very useful, but this comes in
handy when dealing with FFI types that must remain on the same thread for
their existence.
Structs
A type that allows wrapping and unwrapping DiplomaticBag
s inside the
execution context of another bag.
Functions
Run an arbitrary closure on the shared worker thread.