Struct thread_safe::ThreadSafe [−][src]
pub struct ThreadSafe<T: ?Sized> { /* fields omitted */ }
Expand description
The whole point.
This structure wraps around thread-unsafe data and only allows access if it comes from the thread that the data originated from. This allows thread-unsafe data to be used in thread-safe structures, as long as the data is only used from the originating thread.
Panics
If the ThreadSafe
is dropped in a foreign thread, it will panic. This is because running the drop handle
for the inner data is considered to be using it in a thread-unsafe context.
Implementations
Create a new instance of a ThreadSafe
.
Example
use thread_safe::ThreadSafe;
let t = ThreadSafe::new(0i32);
Attempt to convert to the inner type. This errors if it is not in the origin thread.
Example
use std::{thread, sync::Arc};
use thread_safe::ThreadSafe;
let t = ThreadSafe::new(0i32);
let t = thread::spawn(move || match t.try_into_inner() {
Ok(_) => panic!(),
Err(t) => t,
}).join().unwrap();
t.try_into_inner().unwrap();
Attempt to convert to the inner type, using a thread key.
Attempt to convert to the inner type. This panics if it is not in the origin thread.
Attempt to convert to the inner type, using a thread key.
Get the inner object.
Safety
Behavior is undefined if this is not called in the object’s origin thread and the object is !Send
.
Try to get a reference to the inner type. This errors if it is not in the origin thread.
Try to get a reference to the inner type, using a thread key.
Get a reference to the inner type. This panics if it is not called in the origin thread.
Get a reference to the inner type, using a thread key.
Get a reference to the inner type without checking for thread safety.
Safety
Behavior is undefined if this is not called in the origin thread and if T
is !Sync
.
Try to get a mutable reference to the inner type. This errors if it is not in the origin thread.
Try to get a mutable reference to the inner type, using a thread key.
Get a mutable reference to the inner type. This panics if it is not called in the origin thread.
Get a mutable reference to the inner type, using a thread key.
Get a mutable reference to the inner type without checking for thread safety.
Safety
Behavior is undefined if this is not called in the origin thread and if T
is !Send
.
Try to clone this value. This errors if we are not in the origin thread.
Try to clone this value, using a thread key.
Clone this value, using a thread key.
Trait Implementations
Clone this value. This panics if it takes place outside of the origin thread.
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl<T: ?Sized> RefUnwindSafe for ThreadSafe<T> where
T: RefUnwindSafe,
impl<T: ?Sized> Unpin for ThreadSafe<T> where
T: Unpin,
impl<T: ?Sized> UnwindSafe for ThreadSafe<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more