#tetcoin #send #wrapper #thread-local

tetsy-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. Forked from https://github.com/thk1/send_wrapper because it was unmaintained

Show the crate…

1 unstable release

0.1.0 Mar 11, 2021
0.0.0 Mar 9, 2021

#61 in #thread-local

Download history 61/week @ 2024-07-21 45/week @ 2024-07-28 46/week @ 2024-08-04 46/week @ 2024-08-11 35/week @ 2024-08-18 58/week @ 2024-08-25 51/week @ 2024-09-01 34/week @ 2024-09-08 42/week @ 2024-09-15 64/week @ 2024-09-22 67/week @ 2024-09-29 2/week @ 2024-10-06 18/week @ 2024-10-13 23/week @ 2024-10-20 28/week @ 2024-10-27 21/week @ 2024-11-03

90 downloads per month
Used in 49 crates (5 directly)

MIT/Apache

14KB
128 lines

TetsySendWrapper

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.

The idea for this library 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 library makes it possible that gtk-rs structs can leave the main thread at all, like required in the given

Documentation

Examples

use tetsy_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;

License

tetsy-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.

No runtime deps