Struct Unique

Source
pub struct Unique<'a, T: ?Sized, D: Deleter> { /* private fields */ }
Expand description

Smart pointer, that owns and manages object via its pointer.

On Drop it automatically disposes of pointer with provided deleter.

Useful in C FFI context.

§Safety

If you use Deleter that relies on type information, you must guarantee that object was created using the same type as pointer, which points to it.

Which means you must guarantee that specified pointer is valid one and points to existing memory storage, which is already initialized.

§Trait implementation

Due to assumption that Unique pointer always has valid value (you need to use unsafe code to create invalid one)

All trait implementations, except pointer specific one (e.g. fmt::Pointer), implements corresponding traits by delegating call to underlying value.

Implementations§

Source§

impl<T> Unique<'static, T, GlobalDeleter>

Source

pub fn boxed(val: T) -> Self

Creates new instance using global allocator

Source§

impl<T: ?Sized> Unique<'static, T, GlobalDeleter>

Source

pub fn into_boxed(self) -> Box<T>

Converts ptr to box

Source§

impl<'a, T: ?Sized, D: Deleter> Unique<'a, T, D>

Source

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

Creates new instance from raw pointer and Deleter instance

§Panics
  • If pointer is null
Source

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

Creates instance from raw pointer, checking if pointer is null.

Returns None if pointer is null.

Source

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

Creates instance from raw pointer, without checking if pointer is null.

User must ensure that pointer is non-null

Source

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

Gets underlying raw pointer.

Note that it is illegal to create multiple mutable references so care must be taken when converting raw pointer into mutable reference.

Source

pub fn as_ref(&self) -> &T

Gets reference to underlying data.

Source

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

Gets mutable reference to underlying data.

Source

pub fn cast<N>(&self) -> *mut N

Retrieves pointer as of type

Source

pub fn const_cast<N>(&self) -> *const N

Retrieves pointer as of type and const

Source

pub fn swap(&mut self, other: &mut Self)

Swaps underlying pointers between instances

Source

pub fn release(self) -> NonNull<T>

Releases the ownership and returns raw pointer, without dropping it.

Trait Implementations§

Source§

impl<'a, T: ?Sized + Debug, D: Deleter> Debug for Unique<'a, T, D>

Source§

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

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

impl<'a, T: ?Sized, D: Deleter> Deref for Unique<'a, T, D>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T: ?Sized, D: Deleter> DerefMut for Unique<'a, T, D>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, T: ?Sized + Display, D: Deleter> Display for Unique<'a, T, D>

Source§

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

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

impl<'a, T: ?Sized, D: Deleter> Drop for Unique<'a, T, D>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T: ?Sized> From<&'a mut T> for Unique<'a, T, ()>

Source§

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

Converts to this type from the input type.
Source§

impl<'a, T: ?Sized + Hash, D: Deleter> Hash for Unique<'a, T, D>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: ?Sized + PartialEq, D: Deleter> PartialEq for Unique<'a, T, D>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &Self) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: ?Sized + PartialOrd, D: Deleter> PartialOrd for Unique<'a, T, D>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T: ?Sized, D: Deleter> Pointer for Unique<'a, T, D>

Source§

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

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

impl<'a, T: ?Sized + Eq, D: Deleter> Eq for Unique<'a, T, D>

Source§

impl<'a, T: ?Sized + Send, D: Deleter> Send for Unique<'a, T, D>

Source§

impl<'a, T: ?Sized + Sync, D: Deleter> Sync for Unique<'a, T, D>

Source§

impl<'a, T: ?Sized + Unpin, D: Deleter> Unpin for Unique<'a, T, D>

Source§

impl<'a, T: ?Sized + RefUnwindSafe, D: Deleter> UnwindSafe for Unique<'a, T, D>

Auto Trait Implementations§

§

impl<'a, T, D> Freeze for Unique<'a, T, D>
where T: ?Sized,

§

impl<'a, T, D> RefUnwindSafe for Unique<'a, T, D>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.