Struct Owned

Source
pub struct Owned<T, A>(/* private fields */)
where
    T: ?Sized,
    A: TrMalloc + Clone;
Expand description

A smart pointer with specified allocator.

Implementations§

Source§

impl<T, A> Owned<T, A>
where A: TrMalloc + Clone,

Source

pub fn new(data: T, alloc: A) -> Self

Source

pub fn pin(data: T, alloc: A) -> Pin<Self>

Source§

impl<T, A> Owned<[T], A>
where A: TrMalloc + Clone,

Source

pub fn new_slice( len: usize, init_each: impl FnMut(usize) -> T, alloc: A, ) -> Self

Source§

impl<T, A> Owned<[MaybeUninit<T>], A>
where A: TrMalloc + Clone,

Source

pub fn new_uninit_slice(len: usize, alloc: A) -> Self

Source§

impl<'a, T, A> Owned<T, A>
where T: ?Sized, A: 'a + TrMalloc + Clone,

Source

pub fn leak(owned: Owned<T, A>) -> &'a mut T

Source§

impl<T, A> Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source

pub unsafe fn from_raw(raw: *mut T) -> Owned<T, A>

Constructs an Owned<T, A> from a raw resource.

After calling this function, the raw resource is owned by the resulting Owned<T, A>. Specifically, the Owned destructor will call the destructor of T and free the allocated memory. For this to be safe, the memory must have been allocated in accordance with the memory layout used by Owned<T, A> .

§Safety

This function is unsafe because improper use may lead to memory problems. For example, a double-free may occur if the function is called twice on the same raw pointer.

§Examples
use core_malloc::CoreAlloc;
use mm_ptr::{Owned, XtMallocOwned};

let x = CoreAlloc::default().owned("hello".to_owned());
let x_ptr = Owned::leak(x);

unsafe {
    // Convert back to an `Owned` to prevent leak.
    let x = Owned::<String, CoreAlloc>::from_raw(x_ptr);
    assert_eq!(&*x, "hello");

    // Further calls to `Owned::from_raw(x_ptr)` would be memory-unsafe.
}

// The memory was freed when `x` went out of scope above, so `x_ptr` is
// now dangling!
Source

pub const fn malloc(&self) -> &A

Source

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

Trait Implementations§

Source§

impl<T, A> Borrow<T> for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T, A> BorrowMut<T> for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T, A> Debug for Owned<T, A>
where T: ?Sized + Debug, A: TrMalloc + Clone + Debug,

Source§

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

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

impl<T, A> Deref for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T, A> DerefMut for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

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

Mutably dereferences the value.
Source§

impl<T, A> Drop for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T, A> PartialEq for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

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

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

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

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

impl<T, A> Pointer for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

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

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

impl<T, A> TrBoxed for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

type Malloc = A

Source§

fn malloc(&self) -> &Self::Malloc

Source§

impl<T, A> Eq for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Source§

impl<T, A> Send for Owned<T, A>
where T: ?Sized + Send, A: TrMalloc + Clone,

Source§

impl<T, A> Sync for Owned<T, A>
where T: ?Sized + Sync, A: TrMalloc + Clone,

Source§

impl<T, A> TrUnique for Owned<T, A>
where T: ?Sized, A: TrMalloc + Clone,

Auto Trait Implementations§

§

impl<T, A> Freeze for Owned<T, A>
where T: ?Sized,

§

impl<T, A> RefUnwindSafe for Owned<T, A>

§

impl<T, A> Unpin for Owned<T, A>
where T: ?Sized,

§

impl<T, A> UnwindSafe for Owned<T, A>

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