Struct ThreadSafe

Source
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>

Source

pub fn new(inner: T) -> ThreadSafe<T>

Create a new instance of a ThreadSafe.

§Example
use thread_safe::ThreadSafe;
let t = ThreadSafe::new(0i32);
Source

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();
Source

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.

Source

pub fn into_inner(self) -> T

Attempt to convert to the inner type. This panics if it is not in the origin thread.

Source

pub fn into_inner_with_key(self, key: ThreadKey) -> T

Attempt to convert to the inner type, using a thread key.

Source

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>

Source

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.

Source

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.

Source

pub fn get_ref(&self) -> &T

Get a reference to the inner type. This panics if it is not called in the origin thread.

Source

pub fn get_ref_with_key(&self, key: ThreadKey) -> &T

Get a reference to the inner type, using a thread key.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_mut_with_key(&mut self, key: ThreadKey) -> &mut T

Get a mutable reference to the inner type, using a thread key.

Source

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>

Source

pub fn try_clone(&self) -> Result<ThreadSafe<T>, NotInOriginThread>

Try to clone this value. This errors if we are not in the origin thread.

Source

pub fn try_clone_with_key( &self, key: ThreadKey, ) -> Result<ThreadSafe<T>, NotInOriginThread>

Try to clone this value, using a thread key.

Source

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>

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + ?Sized> Debug for ThreadSafe<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default> Default for ThreadSafe<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ?Sized> Drop for ThreadSafe<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Send for ThreadSafe<T>

Source§

impl<T> Sync for ThreadSafe<T>

Auto Trait Implementations§

§

impl<T> Freeze for ThreadSafe<T>
where T: Freeze + ?Sized,

§

impl<T> RefUnwindSafe for ThreadSafe<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Unpin for ThreadSafe<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for ThreadSafe<T>
where T: UnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.