[−][src]Struct gharial::TestBox
TestBox
behaves like std::boxed::Box
except for it owns a reference to a GlobalAlloc
.
If template parameter A
is GAlloc
, it causes assertion error if the instance is not
dropped or dropped twice.
See also GBox
, which is an alias to TestBox<T, GAlloc>
.
For example, it sometimes requires to allocate heap memory to implement container struct, and then the elements must be dropped manually. This struct helps the test.
Implementations
impl<T, A> TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
pub fn new(x: T, alloc: A) -> Self
[src]
Creates a new instance.
Examples
use gharial::{GAlloc, TestBox}; let alloc = GAlloc::default(); let _box = TestBox::new(5, alloc);
pub unsafe fn from_raw_alloc(ptr: *mut T, alloc: A) -> Self
[src]
Creates a new instance from raw pointer and a reference to allocator.
After calling this function, the raw pointer is owned by the resulting TestBox
.
Specifically, TestBox::drop
destructs the referenced object and free the pointer.
Safety
To use this function safe, the ptr should be allocated via alloc
and it should not be
freed anywhere else.
Examples
use gharial::{GAlloc, TestBox}; use std::alloc::{handle_alloc_error, GlobalAlloc, Layout}; let alloc = GAlloc::default(); let ptr = unsafe { let layout = Layout::new::<i32>(); let ptr = alloc.alloc(layout) as *mut i32; if ptr.is_null() { handle_alloc_error(layout); } *ptr = 5; ptr }; let _box = unsafe { TestBox::from_raw_alloc(ptr, alloc) };
impl<T, A> TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
pub fn leak<'a>(tb: Self) -> &'a mut T where
T: 'a,
[src]
T: 'a,
Consumes and leaks TestBox
.
Examples
use gharial::{GAlloc, TestBox}; let alloc = GAlloc::default(); let five = TestBox::new(5, alloc.clone()); let leaked = TestBox::leak(five); assert_eq!(5, *leaked); let five_ = unsafe { TestBox::from_raw_alloc(leaked, alloc) };
pub fn into_raw(tb: Self) -> *mut T
[src]
Consumes the TestBox
and returning a wrapped raw pointer.
Examples
use gharial::{GAlloc, TestBox}; let alloc = GAlloc::default(); let five = TestBox::new(5, alloc.clone()); let raw = TestBox::into_raw(five); assert_eq!(5, unsafe { *raw }); let five_ = unsafe { TestBox::from_raw_alloc(raw, alloc) };
Trait Implementations
impl<T, A> AsMut<T> for TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
impl<T, A> AsRef<T> for TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
impl<T, A> Borrow<T> for TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
impl<T, A> BorrowMut<T> for TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, A> Clone for TestBox<T, A> where
T: Clone,
A: Clone + GlobalAlloc,
[src]
T: Clone,
A: Clone + GlobalAlloc,
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T: Debug, A: Debug> Debug for TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
impl<T, A> Default for TestBox<T, A> where
T: Default,
A: Default + GlobalAlloc,
[src]
T: Default,
A: Default + GlobalAlloc,
impl<T, A> Deref for TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
impl<T, A> DerefMut for TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
impl<T, A> Drop for TestBox<T, A> where
A: GlobalAlloc,
[src]
A: GlobalAlloc,
impl<T, A> Eq for TestBox<T, A> where
T: Eq,
A: GlobalAlloc,
[src]
T: Eq,
A: GlobalAlloc,
impl<T, A> From<T> for TestBox<T, A> where
A: Default + GlobalAlloc,
[src]
A: Default + GlobalAlloc,
impl<T, A> Hash for TestBox<T, A> where
T: Hash,
A: GlobalAlloc,
[src]
T: Hash,
A: GlobalAlloc,
fn hash<H>(&self, state: &mut H) where
H: Hasher,
[src]
H: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T, A> Ord for TestBox<T, A> where
T: Ord,
A: GlobalAlloc,
[src]
T: Ord,
A: GlobalAlloc,
fn cmp(&self, rh: &Self) -> Ordering
[src]
#[must_use]fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]fn clamp(self, min: Self, max: Self) -> Self
[src]
impl<T, A> PartialEq<TestBox<T, A>> for TestBox<T, A> where
T: PartialEq,
A: GlobalAlloc,
[src]
T: PartialEq,
A: GlobalAlloc,
impl<T, A> PartialOrd<TestBox<T, A>> for TestBox<T, A> where
T: PartialOrd,
A: GlobalAlloc,
[src]
T: PartialOrd,
A: GlobalAlloc,
Auto Trait Implementations
impl<T, A> RefUnwindSafe for TestBox<T, A> where
A: RefUnwindSafe,
T: RefUnwindSafe,
A: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, A> !Send for TestBox<T, A>
impl<T, A> !Sync for TestBox<T, A>
impl<T, A> Unpin for TestBox<T, A> where
A: Unpin,
A: Unpin,
impl<T, A> UnwindSafe for TestBox<T, A> where
A: UnwindSafe,
T: RefUnwindSafe,
A: UnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
[src]
V: MultiLane<T>,