Expand description
This Rust library implements a wrapper type called SendWrapper which
allows you to move around non-Send types between threads, as long as you
access the contained value only from within the original thread. You also
have to make sure that the wrapper is dropped from within the original
thread. If any of these constraints is violated, a panic occurs.
SendWrapper<T> implements Send and Sync for any type T.
§Examples
use std::{rc::Rc, sync::mpsc::channel, thread};
use compio_send_wrapper::SendWrapper;
// Rc is a non-Send type.
let value = Rc::new(42);
// We now wrap the value with `SendWrapper` (value is moved inside).
let wrapped_value = SendWrapper::new(value);
// A channel allows us to move the wrapped value between threads.
let (sender, receiver) = channel();
let t = thread::spawn(move || {
// This would panic (because of accessing in the wrong thread):
// let value = wrapped_value.get().unwrap();
// Move SendWrapper back to main thread, so it can be dropped from there.
// If you leave this out the thread will panic because of dropping from wrong thread.
sender.send(wrapped_value).unwrap();
});
let wrapped_value = receiver.recv().unwrap();
// Now you can use the value again.
let value = wrapped_value.get().unwrap();
let mut wrapped_value = wrapped_value;
// You can also get a mutable reference to the value.
let value = wrapped_value.get_mut().unwrap();§Features
This crate exposes several optional features:
futures: EnablesFutureandStreamimplementations forSendWrapper.current_thread_id: Uses the unstablestd::thread::current_idAPI (on nightly Rust) to track the originating thread more efficiently.nightly: Enables nightly-only, experimental functionality used by this crate (including support forcurrent_thread_idas configured inCargo.toml).
You can enable them in Cargo.toml like so:
compio-send-wrapper = { version = "...", features = ["futures"] }
# or, for example:
# compio-send-wrapper = { version = "...", features = ["futures", "current_thread_id"] }§License
compio-send-wrapper is distributed under the terms of both the MIT license
and the Apache License (Version 2.0).
See LICENSE-APACHE.txt, and LICENSE-MIT.txt for details.
Structs§
- Send
Wrapper - A wrapper which allows you to move around non-
Send-types between threads, as long as you access the contained value only from within the original thread and make sure that it is dropped from within the original thread.