pub struct TestBox<T, A>where
A: GlobalAlloc,{ /* private fields */ }
Expand description
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§
source§impl<T, A> TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> TestBox<T, A>where A: GlobalAlloc,
sourcepub fn new(x: T, alloc: A) -> Self
pub fn new(x: T, alloc: A) -> Self
Creates a new instance.
Examples
use gharial::{GAlloc, TestBox};
let alloc = GAlloc::default();
let _box = TestBox::new(5, alloc);
sourcepub unsafe fn from_raw_alloc(ptr: *mut T, alloc: A) -> Self
pub unsafe fn from_raw_alloc(ptr: *mut T, alloc: A) -> Self
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) };
source§impl<T, A> TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> TestBox<T, A>where A: GlobalAlloc,
sourcepub fn leak<'a>(tb: Self) -> &'a mut Twhere
T: 'a,
pub fn leak<'a>(tb: Self) -> &'a mut Twhere 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) };
sourcepub fn into_raw(tb: Self) -> *mut T
pub fn into_raw(tb: Self) -> *mut T
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§
source§impl<T, A> AsMut<T> for TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> AsMut<T> for TestBox<T, A>where A: GlobalAlloc,
source§impl<T, A> AsRef<T> for TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> AsRef<T> for TestBox<T, A>where A: GlobalAlloc,
source§impl<T, A> Borrow<T> for TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> Borrow<T> for TestBox<T, A>where A: GlobalAlloc,
source§impl<T, A> BorrowMut<T> for TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> BorrowMut<T> for TestBox<T, A>where A: GlobalAlloc,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T, A> Deref for TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> Deref for TestBox<T, A>where A: GlobalAlloc,
source§impl<T, A> DerefMut for TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> DerefMut for TestBox<T, A>where A: GlobalAlloc,
source§impl<T, A> Drop for TestBox<T, A>where
A: GlobalAlloc,
impl<T, A> Drop for TestBox<T, A>where A: GlobalAlloc,
source§impl<T, A> Ord for TestBox<T, A>where
T: Ord,
A: GlobalAlloc,
impl<T, A> Ord for TestBox<T, A>where T: Ord, A: GlobalAlloc,
source§impl<T, A> PartialOrd<TestBox<T, A>> for TestBox<T, A>where
T: PartialOrd,
A: GlobalAlloc,
impl<T, A> PartialOrd<TestBox<T, A>> for TestBox<T, A>where T: PartialOrd, A: GlobalAlloc,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more