Struct threadcell::ThreadCell

source ·
pub struct ThreadCell<T> { /* private fields */ }
Expand description

A cell that can be owned by a single thread or none at all.

Implementations§

source§

impl<T> ThreadCell<T>

source

pub const fn new_disowned(data: T) -> Self

Creates a ThreadCell that is not owned by any thread. This is a const fn which allows static construction of ThreadCells.

source

pub fn new_owned(data: T) -> Self

Creates a ThreadCell that is owned by the current thread.

source

pub fn acquire(&self)

Takes the ownership of a cell.

Panics

When the cell is already owned by this thread or it is owned by another thread.

source

pub fn try_acquire(&self) -> bool

Tries to take the ownership of a cell. Returns true when the ownership could be obtained or the cell was already owned by the current thread and false when the cell is owned by another thread.

source

pub fn try_acquire_once(&self) -> bool

Tries to take the ownership of a cell. Returns true when the ownership could be obtained and false when the cell is already owned or owned by another thread. Note that this fails when the cell is already owned (unlike try_acquire()).

source

pub fn acquire_get(&self) -> &T

Takes the ownership of a cell and returns a reference to its value.

Panics

When the cell is owned by another thread.

source

pub fn try_acquire_get(&self) -> Option<&T>

Tries to take the ownership of a cell and returns a reference to its value. Will return ‘None’ when the cell is owned by another thread.

source

pub fn acquire_get_mut(&mut self) -> &mut T

Takes the ownership of a cell and returns a mutable reference to its value.

Panics

When the cell is owned by another thread.

source

pub fn try_acquire_get_mut(&mut self) -> Option<&mut T>

Tries to take the ownership of a cell and returns a mutable reference to its value. Will return ‘None’ when the cell is owned by another thread.

source

pub fn acquire_guard(&self) -> Guard<'_, T>

Acquires a ThreadCell returning a Guard that releases it when becoming dropped.

Panics

When the cell is owned by another thread.

source

pub fn try_acquire_guard(&self) -> Option<Guard<'_, T>>

Acquires a ThreadCell returning a Option<Guard> that releases it when becoming dropped. Returns None when self is owned by another thread.

source

pub fn acquire_guard_mut(&mut self) -> GuardMut<'_, T>

Acquires a ThreadCell returning a GuardMut that releases it when becoming dropped.

Panics

When the cell is owned by another thread.

source

pub fn try_acquire_guard_mut(&mut self) -> Option<GuardMut<'_, T>>

Acquires a ThreadCell returning a Option<GuardMut> that releases it when becoming dropped. Returns None when self is owned by another thread.

source

pub fn with<R, F: FnOnce(&T) -> R>(&self, f: F) -> R

Runs a closure on a ThreadCell with acquire/release.

Panics

When the cell is already owned by the current thread or is owned by another thread.

source

pub fn with_mut<R, F: FnOnce(&mut T) -> R>(&mut self, f: F) -> R

Runs a closure on a mutable ThreadCell with acquire/release.

Panics

When the cell is already owned by the current thread or is owned by another thread.

source

pub fn try_with<R, F: FnOnce(&T) -> R>(&self, f: F) -> Option<R>

Tries to run a closure on a ThreadCell with acquire/release. Returns Some(Result) when the cell could be acquired and None when it is owned by another thread.

source

pub fn try_with_mut<R, F: FnOnce(&mut T) -> R>(&mut self, f: F) -> Option<R>

Tries to run a closure on a mutable ThreadCell with acquire/release. Returns Some(Result) when the cell could be acquired and None when it is owned by another thread.

source

pub unsafe fn steal(&self) -> &Self

Takes the ownership of a cell unconditionally. This is a no-op when the cell is already owned by the current thread. Returns ‘self’ thus it can be chained with .release().

Safety

This method does not check if the cell is owned by another thread. The owning thread may operate on the content, thus a data race/UB will happen when the accessed value is not Sync. The previous owning thread may panic when it expects owning the cell. The only safe way to use this method is to recover a cell that is owned by a thread that finished without releasing it (e.g after a panic). Attention should be paid to the fact that the value protected by the ThreadCell might be in a undefined state.

Panics

The ThreadCell has a Guard on it. steal() can only be used with acquire/release semantics.

source

pub fn release(&self)

Sets a ThreadCell which is owned by the current thread into the disowned state.

Panics

The current thread does not own the cell.

source

pub fn try_release(&self) -> bool

Tries to set a ThreadCell which is owned by the current thread into the disowned state. Returns true on success and false when the current thread does not own the cell.

source

pub fn is_owned(&self) -> bool

Returns true when the current thread owns this cell.

source

pub fn is_disowned(&self) -> bool

Returns true when this ThreadCell is not owned by any thread. As this can change at any time by another taking ownership of this ThreadCell the result of this function may be inexact and racy. Use this only when only a hint is required or access to the ThreadCell is synchronized by some other means.

source

pub fn is_acquired(&self) -> bool

Returns true when the current thread owns this cell by acquire.

source

pub fn is_guarded(&self) -> bool

Returns true when the current thread holds a guard on this cell.

source

pub fn into_inner(self) -> T

Consumes a owned cell and returns its content.

Panics

The current thread does not own the cell.

source

pub fn get(&self) -> &T

Gets an immutable reference to the cells content.

Panics

The current thread does not own the cell.

source

pub fn get_mut(&mut self) -> &mut T

Gets a mutable reference to the cells content.

Panics

The current thread does not own the cell.

source

pub fn try_get(&self) -> Option<&T>

Tries to get an immutable reference to the cells content. Returns ‘None’ when the thread does not own the cell.

source

pub fn try_get_mut(&mut self) -> Option<&mut T>

Tries to get a mutable reference to the cells content. Returns ‘None’ when the thread does not own the cell.

source

pub unsafe fn get_unchecked(&self) -> &T

Gets an immutable reference to the cells content without checking for ownership.

Safety

This is always safe when the thread owns the cell, for example after a acquire() call. When the current thread does not own the cell then it is only safe when T is a Sync type.

source

pub unsafe fn get_mut_unchecked(&mut self) -> &mut T

Gets an mutable reference to the cells content without checking for ownership.

Safety

This is always safe when the thread owns the cell, for example after a acquire() call. When the current thread does not own the cell then it is only safe when T is a Sync type.

Trait Implementations§

source§

impl<T: Clone> Clone for ThreadCell<T>

Clones a owned ThreadCell.

Panics

Another thread owns the cell.

source§

fn clone(&self) -> ThreadCell<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for ThreadCell<T>

Debug information of a ThreadCell. Prints “<ThreadCell>” when the current thread does not own the cell.

source§

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

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

impl<T: Default> Default for ThreadCell<T>

Creates a new owned ThreadCell with the default constructed target value.

source§

fn default() -> ThreadCell<T>

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

impl<T: Display> Display for ThreadCell<T>

Formatted output of the value inside a ThreadCell.

Panics

The cell is not owned by the current thread.

source§

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

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

impl<T> Drop for ThreadCell<T>

Destroys a ThreadCell. The cell must be either owned by the current thread or disowned.

Panics

Another thread owns the cell.

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> From<T> for ThreadCell<T>

Creates a new owned ThreadCell from the given value.

source§

fn from(t: T) -> ThreadCell<T>

Converts to this type from the input type.
source§

impl<T: Ord> Ord for ThreadCell<T>

Compare two ThreadCells.

Panics

Either cell is not owned by the current thread.

source§

fn cmp(&self, other: &ThreadCell<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq> PartialEq<ThreadCell<T>> for ThreadCell<T>

Check two ThreadCells for partial equality.

Panics

Either cell is not owned by the current thread.

source§

fn eq(&self, other: &ThreadCell<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd> PartialOrd<ThreadCell<T>> for ThreadCell<T>

Comparison functions between ThreadCells.

Panics

Either cell is not owned by the current thread.

source§

fn partial_cmp(&self, other: &ThreadCell<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
source§

fn lt(&self, other: &ThreadCell<T>) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
source§

fn le(&self, other: &ThreadCell<T>) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
source§

fn gt(&self, other: &ThreadCell<T>) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
source§

fn ge(&self, other: &ThreadCell<T>) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Eq> Eq for ThreadCell<T>

source§

impl<T: Send> Send for ThreadCell<T>

source§

impl<T: Send> Sync for ThreadCell<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for ThreadCell<T>where T: RefUnwindSafe,

§

impl<T> Unpin for ThreadCell<T>where T: Unpin,

§

impl<T> UnwindSafe for ThreadCell<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.