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>
impl<T> ThreadCell<T>
sourcepub const fn new_disowned(data: T) -> Self
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.
sourcepub fn acquire(&self)
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.
sourcepub fn try_acquire(&self) -> bool
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.
sourcepub fn try_acquire_once(&self) -> bool
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()).
sourcepub fn acquire_get(&self) -> &T
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.
sourcepub fn try_acquire_get(&self) -> Option<&T>
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.
sourcepub fn acquire_get_mut(&mut self) -> &mut T
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.
sourcepub fn try_acquire_get_mut(&mut self) -> Option<&mut T>
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.
sourcepub fn acquire_guard(&self) -> Guard<'_, T>
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.
sourcepub fn try_acquire_guard(&self) -> Option<Guard<'_, T>>
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.
sourcepub fn acquire_guard_mut(&mut self) -> GuardMut<'_, T>
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.
sourcepub fn try_acquire_guard_mut(&mut self) -> Option<GuardMut<'_, T>>
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.
sourcepub fn with<R, F: FnOnce(&T) -> R>(&self, f: F) -> R
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.
sourcepub fn with_mut<R, F: FnOnce(&mut T) -> R>(&mut self, f: F) -> R
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.
sourcepub fn try_with<R, F: FnOnce(&T) -> R>(&self, f: F) -> Option<R>
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.
sourcepub fn try_with_mut<R, F: FnOnce(&mut T) -> R>(&mut self, f: F) -> Option<R>
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.
sourcepub unsafe fn steal(&self) -> &Self
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.
sourcepub fn release(&self)
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.
sourcepub fn try_release(&self) -> bool
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.
sourcepub fn is_disowned(&self) -> bool
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.
sourcepub fn is_acquired(&self) -> bool
pub fn is_acquired(&self) -> bool
Returns true when the current thread owns this cell by acquire.
sourcepub fn is_guarded(&self) -> bool
pub fn is_guarded(&self) -> bool
Returns true when the current thread holds a guard on this cell.
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
sourcepub fn try_get(&self) -> Option<&T>
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.
sourcepub fn try_get_mut(&mut self) -> Option<&mut T>
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.
sourcepub unsafe fn get_unchecked(&self) -> &T
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.
sourcepub unsafe fn get_mut_unchecked(&mut self) -> &mut T
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>
impl<T: Clone> Clone for ThreadCell<T>
source§fn clone(&self) -> ThreadCell<T>
fn clone(&self) -> ThreadCell<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl<T: Debug> Debug for ThreadCell<T>
impl<T: Debug> Debug for ThreadCell<T>
Debug information of a ThreadCell.
Prints “<ThreadCell>” when the current thread does not own the cell.
source§impl<T: Default> Default for ThreadCell<T>
impl<T: Default> Default for ThreadCell<T>
Creates a new owned ThreadCell with the default constructed target value.
source§fn default() -> ThreadCell<T>
fn default() -> ThreadCell<T>
source§impl<T: Display> Display for ThreadCell<T>
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§impl<T> Drop for ThreadCell<T>
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§impl<T> From<T> for ThreadCell<T>
impl<T> From<T> for ThreadCell<T>
Creates a new owned ThreadCell from the given value.
source§fn from(t: T) -> ThreadCell<T>
fn from(t: T) -> ThreadCell<T>
source§impl<T: Ord> Ord for ThreadCell<T>
impl<T: Ord> Ord for ThreadCell<T>
source§fn cmp(&self, other: &ThreadCell<T>) -> Ordering
fn cmp(&self, other: &ThreadCell<T>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
source§impl<T: PartialEq> PartialEq<ThreadCell<T>> for ThreadCell<T>
impl<T: PartialEq> PartialEq<ThreadCell<T>> for ThreadCell<T>
source§fn eq(&self, other: &ThreadCell<T>) -> bool
fn eq(&self, other: &ThreadCell<T>) -> bool
self and other values to be equal, and is used
by ==.source§impl<T: PartialOrd> PartialOrd<ThreadCell<T>> for ThreadCell<T>
impl<T: PartialOrd> PartialOrd<ThreadCell<T>> for ThreadCell<T>
source§fn partial_cmp(&self, other: &ThreadCell<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &ThreadCell<T>) -> Option<Ordering>
source§fn lt(&self, other: &ThreadCell<T>) -> bool
fn lt(&self, other: &ThreadCell<T>) -> bool
source§fn le(&self, other: &ThreadCell<T>) -> bool
fn le(&self, other: &ThreadCell<T>) -> bool
self and other) and is used by the <=
operator. Read more