Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Struct UnsafePinned

Source
pub struct UnsafePinned<T>
where T: ?Sized,
{ /* private fields */ }
๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned #125735)
Expand description

This type provides a way to opt-out of typical aliasing rules; specifically, &mut UnsafePinned<T> is not guaranteed to be a unique pointer.

However, even if you define your type like pub struct Wrapper(UnsafePinned<...>), it is still very risky to have an &mut Wrapper that aliases anything else. Many functions that work generically on &mut T assume that the memory that stores T is uniquely owned (such as mem::swap). In other words, while having aliasing with &mut Wrapper is not immediate Undefined Behavior, it is still unsound to expose such a mutable reference to code you do not control! Techniques such as pinning via Pin are needed to ensure soundness.

Similar to UnsafeCell, UnsafePinned will not usually show up in the public API of a library. It is an internal implementation detail of libraries that need to support aliasing mutable references.

Further note that this does not lift the requirement that shared references must be read-only! Use UnsafeCell for that.

This type blocks niches the same way UnsafeCell does.

Implementationsยง

Sourceยง

impl<T> UnsafePinned<T>

Source

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

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned #125735)

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

All access to the inner value through &UnsafePinned<T> or &mut UnsafePinned<T> or Pin<&mut UnsafePinned<T>> requires unsafe code.

Source

pub const fn into_inner(self) -> T

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned #125735)

Unwraps the value, consuming this UnsafePinned.

Sourceยง

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

Source

pub const fn get_mut_pinned(self: Pin<&mut UnsafePinned<T>>) -> *mut T

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned #125735)

Get read-write access to the contents of a pinned UnsafePinned.

Source

pub const fn get_mut_unchecked(&mut self) -> *mut T

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned #125735)

Get read-write access to the contents of an UnsafePinned.

You should usually be using get_mut_pinned instead to explicitly track the fact that this memory is โ€œpinnedโ€ due to there being aliases.

Source

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

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned #125735)

Get read-only access to the contents of a shared UnsafePinned.

Note that &UnsafePinned<T> is read-only if &T is read-only. This means that if there is mutation of the T, future reads from the *const T returned here are UB! Use UnsafeCell if you also need interior mutability.

#![feature(unsafe_pinned)]
use std::pin::UnsafePinned;

unsafe {
    let mut x = UnsafePinned::new(0);
    let ptr = x.get(); // read-only pointer, assumes immutability
    x.get_mut_unchecked().write(1);
    ptr.read(); // UB!
}
Source

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

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned #125735)

Gets an immutable 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.

Source

pub const fn raw_get_mut(this: *mut UnsafePinned<T>) -> *mut T

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned #125735)

Gets a mutable pointer to the wrapped value.

The difference from get_mut_pinned and get_mut_unchecked is that this function accepts a raw pointer, which is useful to avoid the creation of temporary references.

Trait Implementationsยง

Sourceยง

impl<T> Clone for UnsafePinned<T>
where T: Copy,

Sourceยง

fn clone(&self) -> UnsafePinned<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 for UnsafePinned<T>
where T: ?Sized,

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl<T> Default for UnsafePinned<T>
where T: Default,

Sourceยง

fn default() -> UnsafePinned<T>

Creates an UnsafePinned, with the Default value for T.

Sourceยง

impl<T> From<T> for UnsafePinned<T>

Sourceยง

fn from(value: T) -> UnsafePinned<T>

Creates a new UnsafePinned<T> containing the given value.

Sourceยง

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

Sourceยง

impl<T> Copy for UnsafePinned<T>
where T: Copy,

The type is Copy when T is to avoid people assuming that Copy implies there is no UnsafePinned anywhere. (This is an issue with UnsafeCell: people use Copy bounds to mean Freeze.) Given that there is no unsafe impl Copy for ..., this is also the option that leaves the user more choices (as they can always wrap this in a !Copy type).

Sourceยง

impl<T, U> DispatchFromDyn<UnsafePinned<U>> for UnsafePinned<T>
where T: DispatchFromDyn<U>,

Sourceยง

impl<T> PointerLike for UnsafePinned<T>
where T: PointerLike,

Sourceยง

impl<T> !Unpin for UnsafePinned<T>
where T: ?Sized,

When this type is used, that almost certainly means safe APIs need to use pinning to avoid the aliases from becoming invalidated. Therefore letโ€™s mark this as !Unpin. You can always opt back in to Unpin with an impl block, provided your API is still sound while unpinned.

Auto Trait Implementationsยง

ยง

impl<T> Freeze for UnsafePinned<T>
where T: Freeze + ?Sized,

ยง

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

ยง

impl<T> Send for UnsafePinned<T>
where T: Send + ?Sized,

ยง

impl<T> Sync for UnsafePinned<T>
where T: Sync + ?Sized,

ยง

impl<T> UnwindSafe for UnsafePinned<T>
where T: UnwindSafe + ?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 #126799)
Performs copy-assignment from self to dest. 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 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> ToOwned for T
where T: Clone,

Sourceยง

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