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

Formats the value using the given formatter. Read more

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

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.