pub struct ThreadSafe<T: ?Sized> { /* private fields */ }
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§
Source§impl<T> ThreadSafe<T>
impl<T> ThreadSafe<T>
Sourcepub fn new(inner: T) -> ThreadSafe<T>
pub fn new(inner: T) -> ThreadSafe<T>
Create a new instance of a ThreadSafe
.
§Example
use thread_safe::ThreadSafe;
let t = ThreadSafe::new(0i32);
Sourcepub fn try_into_inner(self) -> Result<T, ThreadSafe<T>>
pub fn try_into_inner(self) -> Result<T, ThreadSafe<T>>
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();
Sourcepub fn try_into_inner_with_key(self, key: ThreadKey) -> Result<T, ThreadSafe<T>>
pub fn try_into_inner_with_key(self, key: ThreadKey) -> Result<T, ThreadSafe<T>>
Attempt to convert to the inner type, using a thread key.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Attempt to convert to the inner type. This panics if it is not in the origin thread.
Sourcepub fn into_inner_with_key(self, key: ThreadKey) -> T
pub fn into_inner_with_key(self, key: ThreadKey) -> T
Attempt to convert to the inner type, using a thread key.
Sourcepub unsafe fn into_inner_unchecked(self) -> T
pub unsafe fn into_inner_unchecked(self) -> T
Get the inner object.
§Safety
Behavior is undefined if this is not called in the object’s origin thread and the object is !Send
.
Source§impl<T: ?Sized> ThreadSafe<T>
impl<T: ?Sized> ThreadSafe<T>
Sourcepub fn try_get_ref(&self) -> Result<&T, NotInOriginThread>
pub fn try_get_ref(&self) -> Result<&T, NotInOriginThread>
Try to get a reference to the inner type. This errors if it is not in the origin thread.
Sourcepub fn try_get_ref_with_key(
&self,
key: ThreadKey,
) -> Result<&T, NotInOriginThread>
pub fn try_get_ref_with_key( &self, key: ThreadKey, ) -> Result<&T, NotInOriginThread>
Try to get a reference to the inner type, using a thread key.
Sourcepub fn get_ref(&self) -> &T
pub fn get_ref(&self) -> &T
Get a reference to the inner type. This panics if it is not called in the origin thread.
Sourcepub fn get_ref_with_key(&self, key: ThreadKey) -> &T
pub fn get_ref_with_key(&self, key: ThreadKey) -> &T
Get a reference to the inner type, using a thread key.
Sourcepub unsafe fn get_ref_unchecked(&self) -> &T
pub unsafe fn get_ref_unchecked(&self) -> &T
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
.
Sourcepub fn try_get_mut(&mut self) -> Result<&mut T, NotInOriginThread>
pub fn try_get_mut(&mut self) -> Result<&mut T, NotInOriginThread>
Try to get a mutable reference to the inner type. This errors if it is not in the origin thread.
Sourcepub fn try_get_mut_with_key(
&mut self,
key: ThreadKey,
) -> Result<&mut T, NotInOriginThread>
pub fn try_get_mut_with_key( &mut self, key: ThreadKey, ) -> Result<&mut T, NotInOriginThread>
Try to get a mutable reference to the inner type, using a thread key.
Sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Get a mutable reference to the inner type. This panics if it is not called in the origin thread.
Sourcepub fn get_mut_with_key(&mut self, key: ThreadKey) -> &mut T
pub fn get_mut_with_key(&mut self, key: ThreadKey) -> &mut T
Get a mutable reference to the inner type, using a thread key.
Sourcepub unsafe fn get_mut_unchecked(&mut self) -> &mut T
pub unsafe fn get_mut_unchecked(&mut self) -> &mut T
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
.
Source§impl<T: Clone> ThreadSafe<T>
impl<T: Clone> ThreadSafe<T>
Sourcepub fn try_clone(&self) -> Result<ThreadSafe<T>, NotInOriginThread>
pub fn try_clone(&self) -> Result<ThreadSafe<T>, NotInOriginThread>
Try to clone this value. This errors if we are not in the origin thread.
Sourcepub fn try_clone_with_key(
&self,
key: ThreadKey,
) -> Result<ThreadSafe<T>, NotInOriginThread>
pub fn try_clone_with_key( &self, key: ThreadKey, ) -> Result<ThreadSafe<T>, NotInOriginThread>
Try to clone this value, using a thread key.
Sourcepub fn clone_with_key(&self, key: ThreadKey) -> ThreadSafe<T>
pub fn clone_with_key(&self, key: ThreadKey) -> ThreadSafe<T>
Clone this value, using a thread key.
Trait Implementations§
Source§impl<T: Clone> Clone for ThreadSafe<T>
impl<T: Clone> Clone for ThreadSafe<T>
Source§fn clone(&self) -> ThreadSafe<T>
fn clone(&self) -> ThreadSafe<T>
Clone this value. This panics if it takes place outside of the origin thread.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more