Crate diplomatic_bag[][src]

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.


// `Rc` is neither `Send` nor `Sync`
let foo = DiplomaticBag::new(|_| Rc::new(RefCell::new(0)));

    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.


A type that allows wrapping and unwrapping DiplomaticBags inside the execution context of another bag.

A wrapper around a T that always implements Send and Sync, but doesn’t allow direct access to it’s internals.


Run an arbitrary closure on the shared worker thread.