Struct Shared

Source
pub struct Shared<T: ?Sized> { /* private fields */ }
Expand description

*mut T but non-zero and covariant.

This is often the correct thing to use when building data structures using raw pointers, but is ultimately more dangerous to use because of its additional properties. If you’re not sure if you should use Shared<T>, just use *mut T!

Unlike *mut T, the pointer must always be non-null, even if the pointer is never dereferenced. This is so that enums may use this forbidden value as a discriminant – Option<Shared<T>> has the same size as Shared<T>. However the pointer may still dangle if it isn’t dereferenced.

Unlike *mut T, Shared<T> is covariant over T. If this is incorrect for your use case, you should include some PhantomData in your type to provide invariance, such as PhantomData<Cell<T>> or PhantomData<&'a mut T>. Usually this won’t be necessary; covariance is correct for most safe abstractions, such as Box, Rc, Arc, Vec, and LinkedList. This is the case because they provide a public API that follows the normal shared XOR mutable rules of Rust.

Implementations§

Source§

impl<T: Sized> Shared<T>

Source

pub const fn empty() -> Self

Creates a new Shared that is dangling, but well-aligned.

This is useful for initializing types which lazily allocate, like Vec::new does.

Source§

impl<T: ?Sized> Shared<T>

Source

pub const unsafe fn new_unchecked(ptr: *mut T) -> Self

Creates a new Shared.

§Safety

ptr must be non-null.

Source

pub fn new(ptr: *mut T) -> Option<Self>

Creates a new Shared if ptr is non-null.

Source

pub const fn as_ptr(self) -> NonNull<T>

Acquires the underlying *mut pointer.

Source

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

Dereferences the content.

The resulting lifetime is bound to self so this behaves “as if” it were actually an instance of T that is getting borrowed. If a longer (unbound) lifetime is needed, use &*my_ptr.ptr().

Source

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

Mutably dereferences the content.

The resulting lifetime is bound to self so this behaves “as if” it were actually an instance of T that is getting borrowed. If a longer (unbound) lifetime is needed, use &mut *my_ptr.ptr_mut().

Trait Implementations§

Source§

impl<T: ?Sized> Clone for Shared<T>

Source§

fn clone(&self) -> Self

Returns a duplicate 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<'a, T: ?Sized> From<&'a T> for Shared<T>

Source§

fn from(reference: &'a T) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: ?Sized> From<&'a mut T> for Shared<T>

Source§

fn from(reference: &'a mut T) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized> From<NonNull<T>> for Shared<T>

Source§

fn from(ptr: NonNull<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized> From<Unique<T>> for Shared<T>

Source§

fn from(unique: Unique<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized> Pointer for Shared<T>

Source§

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

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

impl<S: ?Sized + Unsize<T>, T: ?Sized> CoerceUnsized<Shared<T>> for Shared<S>

Source§

impl<T: ?Sized> Copy for Shared<T>

Auto Trait Implementations§

§

impl<T> Freeze for Shared<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for Shared<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> !Send for Shared<T>

§

impl<T> !Sync for Shared<T>

§

impl<T> Unpin for Shared<T>
where T: ?Sized,

§

impl<T> UnwindSafe for Shared<T>
where T: RefUnwindSafe + ?Sized,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.