Struct RacyCell

Source
pub struct RacyCell<T>(/* private fields */);
Expand description

Cell type that should be preferred over a static mut is better to use in a static

Based on @bstrie comment [issue-53639-bstrie]: https://github.com/rust-lang/rust/issues/53639#issuecomment-888435728

§Safety

The idea here being that callers of RacyCell::get could only turn that *mut into a reference, if you can guarantee the usual reference safety invariants as demonstrated in this example for UnsafeCell::get, with the added rub that you also have to uphold those invariants while taking threads into account, which means that almost nobody can actually safely cast this *mut to a reference (which helps to illustrate the problem with static mut here), so you’re better off just working with the raw pointer (ideally by wrapping it in your own synchronization logic).

Implementations§

Source§

impl<T> RacyCell<T>

Source

pub const fn new(value: T) -> Self

Create a new RacyCell

Source

pub fn get(&self) -> *mut T

Gets a mutable pointer to the wrapped value.

Trait Implementations§

Source§

impl<T: Sync> Sync for RacyCell<T>

Auto Trait Implementations§

§

impl<T> !Freeze for RacyCell<T>

§

impl<T> !RefUnwindSafe for RacyCell<T>

§

impl<T> Send for RacyCell<T>
where T: Send,

§

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

§

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

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<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> Same for T

Source§

type Output = T

Should always be Self
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.