Struct UnsafePinned

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

This type provides a way to entirely opt-out of typical aliasing rules; specifically, &mut UnsafePinned<T> is not guaranteed to be a unique pointer. This also subsumes the effects of UnsafeCell, i.e., &UnsafePinned<T> may point to data that is being mutated.

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.

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)
Available on crate feature std only.

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)
Available on crate feature std only.

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)
Available on crate feature std only.

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)
Available on crate feature std only.

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) -> *mut T

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned)
Available on crate feature std only.

Get mutable access to the contents of a shared UnsafePinned.

This can be cast to a pointer of any kind. When creating references, you must uphold the aliasing rules; see UnsafeCell for more discussion and caveats.

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

unsafe {
    let mut x = UnsafePinned::new(0);
    let ptr = x.get();
    x.get_mut_unchecked().write(1);
    assert_eq!(ptr.read(), 1);
}
Source

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

๐Ÿ”ฌThis is a nightly-only experimental API. (unsafe_pinned)
Available on crate feature std only.

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)
Available on crate feature std only.

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> 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, U> DispatchFromDyn<UnsafePinned<U>> for UnsafePinned<T>
where T: DispatchFromDyn<U>,

Sourceยง

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

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>

ยง

impl<T> !RefUnwindSafe for UnsafePinned<T>

ยง

impl<T> Send for UnsafePinned<T>
where T: Send + ?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> ByteSized for T

Sourceยง

const BYTE_ALIGN: usize = _

The alignment of this type in bytes.
Sourceยง

const BYTE_SIZE: usize = _

The size of this type in bytes.
Sourceยง

fn byte_align(&self) -> usize

Returns the alignment of this type in bytes.
Sourceยง

fn byte_size(&self) -> usize

Returns the size of this type in bytes. Read more
Sourceยง

fn ptr_size_ratio(&self) -> [usize; 2]

Returns the size ratio between Ptr::BYTES and BYTE_SIZE. Read more
Sourceยง

impl<T, R> Chain<R> for T
where T: ?Sized,

Sourceยง

fn chain<F>(self, f: F) -> R
where F: FnOnce(Self) -> R, Self: Sized,

Chain a function which takes the parameter by value.
Sourceยง

fn chain_ref<F>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Chain a function which takes the parameter by shared reference.
Sourceยง

fn chain_mut<F>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Chain a function which takes the parameter by exclusive reference.
Sourceยง

impl<T> ExtAny for T
where T: Any + ?Sized,

Sourceยง

fn type_id() -> TypeId

Returns the TypeId of Self. Read more
Sourceยง

fn type_of(&self) -> TypeId

Returns the TypeId of self. Read more
Sourceยง

fn type_name(&self) -> &'static str โ“˜

Returns the type name of self. Read more
Sourceยง

fn type_is<T: 'static>(&self) -> bool

Returns true if Self is of type T. Read more
Sourceยง

fn type_hash(&self) -> u64

Returns a deterministic hash of the TypeId of Self.
Sourceยง

fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64

Returns a deterministic hash of the TypeId of Self using a custom hasher.
Sourceยง

fn as_any_ref(&self) -> &dyn Any
where Self: Sized,

Upcasts &self as &dyn Any. Read more
Sourceยง

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Upcasts &mut self as &mut dyn Any. Read more
Sourceยง

fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized,

Upcasts Box<self> as Box<dyn Any>. Read more
Sourceยง

fn downcast_ref<T: 'static>(&self) -> Option<&T> โ“˜

Available on crate feature unsafe_layout only.
Returns some shared reference to the inner value if it is of type T. Read more
Sourceยง

fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> โ“˜

Available on crate feature unsafe_layout only.
Returns some exclusive reference to the inner value if it is of type T. Read more
Sourceยง

impl<T> ExtMem for T
where T: ?Sized,

Sourceยง

const NEEDS_DROP: bool = _

Know whether dropping values of this type matters, in compile-time.
Sourceยง

fn mem_align_of<T>() -> usize

Returns the minimum alignment of the type in bytes. Read more
Sourceยง

fn mem_align_of_val(&self) -> usize

Returns the alignment of the pointed-to value in bytes. Read more
Sourceยง

fn mem_size_of<T>() -> usize

Returns the size of a type in bytes. Read more
Sourceยง

fn mem_size_of_val(&self) -> usize

Returns the size of the pointed-to value in bytes. Read more
Sourceยง

fn mem_copy(&self) -> Self
where Self: Copy,

Bitwise-copies a value. Read more
Sourceยง

fn mem_needs_drop(&self) -> bool

Returns true if dropping values of this type matters. Read more
Sourceยง

fn mem_drop(self)
where Self: Sized,

Drops self by running its destructor. Read more
Sourceยง

fn mem_forget(self)
where Self: Sized,

Forgets about self without running its destructor. Read more
Sourceยง

fn mem_replace(&mut self, other: Self) -> Self
where Self: Sized,

Replaces self with other, returning the previous value of self. Read more
Sourceยง

fn mem_take(&mut self) -> Self
where Self: Default,

Replaces self with its default value, returning the previous value of self. Read more
Sourceยง

fn mem_swap(&mut self, other: &mut Self)
where Self: Sized,

Swaps the value of self and other without deinitializing either one. Read more
Sourceยง

unsafe fn mem_zeroed<T>() -> T

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Sourceยง

unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Sourceยง

fn mem_as_bytes(&self) -> &[u8] โ“˜
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &[u8]. Read more
Sourceยง

fn mem_as_bytes_mut(&mut self) -> &mut [u8] โ“˜
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &mut [u8]. 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.

ยง

impl<S> FromSample<S> for S

ยง

fn from_sample_(s: S) -> S

Sourceยง

impl<T> Hook for T

Sourceยง

fn hook_ref<F>(self, f: F) -> Self
where F: FnOnce(&Self),

Applies a function which takes the parameter by shared reference, and then returns the (possibly) modified owned value. Read more
Sourceยง

fn hook_mut<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Applies a function which takes the parameter by exclusive reference, and then returns the (possibly) modified owned value. Read more
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> IntoEither for T

Sourceยง

fn into_either(self, into_left: bool) -> Either<Self, Self> โ“˜

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> โ“˜
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
ยง

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

ยง

fn into_sample(self) -> T

ยง

impl<T> Pointable for T

ยง

const ALIGN: usize

The alignment of pointer.
ยง

type Init = T

The type for initializers.
ยง

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
ยง

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
ยง

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
ยง

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
ยง

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

ยง

fn to_sample_(self) -> U

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.
ยง

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

ยง

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