#thread #wrapper-type #non-send #original #within #thread-local #dropped

send_wrapper

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.

6 releases (breaking)

0.6.0 Jun 5, 2022
0.5.0 Oct 14, 2020
0.4.0 Nov 18, 2019
0.3.0 Oct 16, 2019
0.1.0 Sep 7, 2017

#83 in Rust patterns

Download history 80709/week @ 2023-11-20 91938/week @ 2023-11-27 98684/week @ 2023-12-04 96161/week @ 2023-12-11 87242/week @ 2023-12-18 49903/week @ 2023-12-25 76582/week @ 2024-01-01 103719/week @ 2024-01-08 113492/week @ 2024-01-15 118259/week @ 2024-01-22 143525/week @ 2024-01-29 129982/week @ 2024-02-05 126568/week @ 2024-02-12 126460/week @ 2024-02-19 117542/week @ 2024-02-26 120251/week @ 2024-03-04

499,319 downloads per month
Used in 534 crates (41 directly)

MIT/Apache

21KB
278 lines

SendWrapper

This Rust crate 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.

The idea for this crate was born in the context of a GTK+/gtk-rs-based application. GTK+ applications are strictly single-threaded. It is not allowed to call any GTK+ method from a thread different to the main thread. Consequently, all gtk-rs structs are non-Send.

Sometimes you still want to do some work in background. It is possible to enqueue GTK+ calls from there to be executed in the main thread using Glib. This way you can know, that the gtk-rs structs involved are only accessed in the main thread and will also be dropped there. This crate makes it possible for gtk-rs structs to leave the main thread.

Examples

use send_wrapper::SendWrapper;
use std::rc::Rc;
use std::thread;
use std::sync::mpsc::channel;

// This import is important. It allows you to unwrap the value using deref(),
// deref_mut() or Deref coercion.
use std::ops::{Deref, DerefMut};

// 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 dereferencing in wrong thread):
	// let value = wrapped_value.deref();

	// 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.deref();

// alternatives for dereferencing:
// let value = *wrapped_value;
// let value: &NonSendType = &wrapped_value;

// alternatives for mutable dereferencing (value and wrapped_value must be mutable too, then):
// let mut value = wrapped_value.deref_mut();
// let mut value = &mut *wrapped_value;
// let mut value: &mut NonSendType = &mut wrapped_value;

Wrapping Futures and Streams

To use SendWrapper on Futures or Streams, you should enable the Cargo feature futures first:

send_wrapper = { version = "0.5", features = ["futures"] }

Then, you can transparently wrap your Future or Stream:

use futures::{executor, future::{self, BoxFuture}};
use send_wrapper::SendWrapper;

// `Rc` is a `!Send` type,
let value = Rc::new(42);
// so this `Future` is `!Send` too as increments `Rc`'s inner counter.
let future = future::lazy(|_| value.clone());

// We now wrap the `future` with `SendWrapper` (value is moved inside),
let wrapped_future = SendWrapper::new(future);
// so now it's `Send` + `Sync` (`BoxFuture` trait object contains `Send` requirement).
let boxed_future: BoxFuture<_> = Box::pin(wrapped_future);

let t = thread::spawn(move || {
	// This would panic (because `future` is polled in wrong thread):
	// executor::block_on(boxed_future)
});

Changelog

See CHANGELOG.md

License

send_wrapper is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE, and LICENSE-MIT for details.

Dependencies