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<[MaybeUninit<T>], A>
impl<T, A> Owned<[MaybeUninit<T>], A>
pub fn new_uninit_slice(len: usize, alloc: A) -> Self
Source§impl<T, A> Owned<T, A>
impl<T, A> Owned<T, A>
Sourcepub unsafe fn from_raw(raw: *mut T) -> Owned<T, A>
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!pub const fn malloc(&self) -> &A
pub const fn as_ptr(&self) -> *const T
Trait Implementations§
Source§impl<T, A> BorrowMut<T> for Owned<T, A>
impl<T, A> BorrowMut<T> for Owned<T, A>
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, A> Eq for Owned<T, A>
impl<T, A> Send for Owned<T, A>
impl<T, A> Sync for Owned<T, A>
impl<T, A> TrUnique for Owned<T, A>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more