Crate diplomatic_bag[−][src]
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
BaggageHandler | A type that allows wrapping and unwrapping |
DiplomaticBag | A wrapper around a |