sendify/lib.rs
1//! An unsafe crate to wrap a reference to make it [`Send`](https://doc.rust-lang.org/nightly/core/marker/trait.Send.html) + [`Sync`](https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html)
2//! to be able to transfer it between threads.
3//! Make sure the reference is still valid when unwrapping it.
4//!
5//! # Examples
6//!
7//! ```no_run
8//! use std::thread;
9//!
10//! fn main() {
11//! let data = "my string".to_owned();
12//!
13//! let ref_val = &data;
14//! // Wrap the reference to make it Send + Sync.
15//! let sendify_val = sendify::wrap(ref_val);
16//!
17//! thread::spawn(move || {
18//! // Unwrap the reference, here make sure that reference is still valid otherwise
19//! // the app might crash.
20//! let ref_val = unsafe { sendify_val.unwrap() };
21//! assert_eq!(ref_val, "my string")
22//! })
23//! .join()
24//! .unwrap();
25//!
26//! assert_eq!(data, "my string")
27//! }
28//! ```
29
30pub use self::sendify::Sendify;
31pub use self::sendify_mut::SendifyMut;
32
33mod sendify;
34mod sendify_mut;
35
36/// Wraps an immutable reference to make it [`Send`](https://doc.rust-lang.org/nightly/core/marker/trait.Send.html) + [`Sync`](https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html).
37pub fn wrap<T>(val: &T) -> Sendify<T> {
38 Sendify::wrap(val)
39}
40
41/// Wraps a mutable reference to make it [`Send`](https://doc.rust-lang.org/nightly/core/marker/trait.Send.html) + [`Sync`](https://doc.rust-lang.org/nightly/core/marker/trait.Sync.html).
42pub fn wrap_mut<T>(val: &mut T) -> SendifyMut<T> {
43 SendifyMut::wrap(val)
44}