ThreadOwnedLock

Struct ThreadOwnedLock 

Source
pub struct ThreadOwnedLock<T: ?Sized, P: ThreadIdProvider> { /* private fields */ }
Expand description

A mutual exclusion primitive similar to Mutex but it only allows the owning thread to access the data.

Lock ownership can be transferred to another thread if the data implements Send with the rebind function.

Attempting to lock more than one time on the same thread will result in an error.

Implementations§

Source§

impl<T, P: ThreadIdProvider> ThreadOwnedLock<T, P>

Source

pub fn new(value: T) -> Self

Create a new instance of ThreadOwnedLock and bind it to the current thread.

Source

pub fn rebind(self) -> Self

Transfer ownership of the lock to another thread.

§Example
use thread_owned_lock::StdThreadOwnedLock;
let lock = StdThreadOwnedLock::new(10);
std::thread::spawn(move|| {
    let lock = lock.rebind();
    // lock can now be accessed on this thread.
    let guard = lock.lock();
});
Source§

impl<T: ?Sized, P: ThreadIdProvider> ThreadOwnedLock<T, P>

Source

pub fn lock(&self) -> ThreadOwnedLockGuard<'_, T, P>

Acquires the mutex, returning an RAII style guard which allows access to the data.

§Panics

This call will panic if this method is called from a thread other than the owning thread or if the lock has already been acquired.

Source

pub fn try_lock( &self, ) -> Result<ThreadOwnedLockGuard<'_, T, P>, ThreadOwnedMutexError>

Try to acquire the mutex. If one of the following conditions fails, an error will be returned:

  • The thread accessing the lock must be the bound thread.
  • The lock can only be acquired on time.

Trait Implementations§

Source§

impl<T: Default, P: ThreadIdProvider> Default for ThreadOwnedLock<T, P>

Source§

fn default() -> Self

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

impl<T, P: ThreadIdProvider> From<T> for ThreadOwnedLock<T, P>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized + Send, P: ThreadIdProvider> Send for ThreadOwnedLock<T, P>

Source§

impl<T: ?Sized + Send, P: ThreadIdProvider> Sync for ThreadOwnedLock<T, P>

Auto Trait Implementations§

§

impl<T, P> !Freeze for ThreadOwnedLock<T, P>

§

impl<T, P> !RefUnwindSafe for ThreadOwnedLock<T, P>

§

impl<T, P> Unpin for ThreadOwnedLock<T, P>
where <P as ThreadIdProvider>::Id: Unpin, T: Unpin + ?Sized,

§

impl<T, P> UnwindSafe for ThreadOwnedLock<T, P>

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> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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, 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.