Skip to main content

CovariantUnsafeCell

Struct CovariantUnsafeCell 

Source
pub struct CovariantUnsafeCell<T>(/* private fields */)
where
    T: ?Sized;
🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)
Expand description

Covariant version of UnsafeCell.

Implementations§

Source§

impl<T> CovariantUnsafeCell<T>

Source

pub const fn new(value: T) -> CovariantUnsafeCell<T>

🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)

Constructs a new instance of CovariantUnsafeCell which will wrap the specified value.

All access to the inner value through &CovariantUnsafeCell<T> requires unsafe code.

§Examples
#![feature(covariant_unsafe_cell)]
use std::cell::CovariantUnsafeCell;

let uc = CovariantUnsafeCell::new(5);
Source

pub const fn into_inner(self) -> T

🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)

Unwraps the value, consuming the cell.

§Examples
#![feature(covariant_unsafe_cell)]
use std::cell::CovariantUnsafeCell;

let uc = CovariantUnsafeCell::new(5);

let five = uc.into_inner();
Source§

impl<T> CovariantUnsafeCell<T>
where T: ?Sized,

Source

pub const fn get(&self) -> NonNull<T>

🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)

Gets a mutable non-null pointer to the wrapped value.

This can be cast to a pointer of any kind. When creating (shared or mutable) references, you must uphold the aliasing rules; see the UnsafeCell type-level docs for more discussion and caveats.

§Examples
#![feature(covariant_unsafe_cell)]
use std::cell::CovariantUnsafeCell;
use std::ptr::NonNull;

let uc = CovariantUnsafeCell::new(5);

let ptr: NonNull<i32> = uc.get();
Source

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

🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)

Returns a mutable reference to the underlying data.

This call borrows the CovariantUnsafeCell mutably (at compile-time) which guarantees that we possess the only reference.

§Examples
#![feature(covariant_unsafe_cell)]
use std::cell::CovariantUnsafeCell;

let mut c = CovariantUnsafeCell::new(5);
*c.get_mut() += 1;

assert_eq!(*c.get_mut(), 6);
Source

pub const fn raw_get(this: *const CovariantUnsafeCell<T>) -> *mut T

🔬This is a nightly-only experimental API. (covariant_unsafe_cell #159735)

Gets a mutable pointer to the wrapped value. The difference from get is that this function accepts a raw pointer, which is useful to avoid the creation of temporary references.

This can be cast to a pointer of any kind. When creating (shared or mutable) references, you must uphold the aliasing rules; see [the UnsafeCell type-level docs] for more discussion and caveats.

§Examples

Gradual initialization of an CovariantUnsafeCell requires raw_get, as calling get would require creating a reference to uninitialized data:

#![feature(covariant_unsafe_cell)]
use std::cell::CovariantUnsafeCell;
use std::mem::MaybeUninit;

let m = MaybeUninit::<CovariantUnsafeCell<i32>>::uninit();
unsafe { CovariantUnsafeCell::raw_get(m.as_ptr()).write(5); }
// avoid below which references to uninitialized data
// unsafe { CovariantUnsafeCell::get(&*m.as_ptr()).write(5); }
let uc = unsafe { m.assume_init() };

assert_eq!(uc.into_inner(), 5);

Trait Implementations§

Source§

impl<T> !Sync for CovariantUnsafeCell<T>
where T: ?Sized,

Source§

impl<T, U> CoerceUnsized<CovariantUnsafeCell<U>> for CovariantUnsafeCell<T>
where T: CoerceUnsized<U>,

Source§

impl<T> Debug for CovariantUnsafeCell<T>
where T: ?Sized,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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.