[][src]Struct gharial::TestBox

pub struct TestBox<T, A> where
    A: GlobalAlloc
{ /* fields omitted */ }

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]

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]

pub fn leak<'a>(tb: Self) -> &'a mut T where
    T: 'a, 
[src]

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]

impl<T, A> AsRef<T> for TestBox<T, A> where
    A: GlobalAlloc
[src]

impl<T, A> Borrow<T> for TestBox<T, A> where
    A: GlobalAlloc
[src]

impl<T, A> BorrowMut<T> for TestBox<T, A> where
    A: GlobalAlloc
[src]

impl<T, A> Clone for TestBox<T, A> where
    T: Clone,
    A: Clone + GlobalAlloc
[src]

impl<T: Debug, A: Debug> Debug for TestBox<T, A> where
    A: GlobalAlloc
[src]

impl<T, A> Default for TestBox<T, A> where
    T: Default,
    A: Default + GlobalAlloc
[src]

impl<T, A> Deref for TestBox<T, A> where
    A: GlobalAlloc
[src]

type Target = T

The resulting type after dereferencing.

impl<T, A> DerefMut for TestBox<T, A> where
    A: GlobalAlloc
[src]

impl<T, A> Drop for TestBox<T, A> where
    A: GlobalAlloc
[src]

impl<T, A> Eq for TestBox<T, A> where
    T: Eq,
    A: GlobalAlloc
[src]

impl<T, A> From<T> for TestBox<T, A> where
    A: Default + GlobalAlloc
[src]

impl<T, A> Hash for TestBox<T, A> where
    T: Hash,
    A: GlobalAlloc
[src]

impl<T, A> Ord for TestBox<T, A> where
    T: Ord,
    A: GlobalAlloc
[src]

impl<T, A> PartialEq<TestBox<T, A>> for TestBox<T, A> where
    T: PartialEq,
    A: GlobalAlloc
[src]

impl<T, A> PartialOrd<TestBox<T, A>> for TestBox<T, A> where
    T: PartialOrd,
    A: GlobalAlloc
[src]

Auto Trait Implementations

impl<T, A> RefUnwindSafe for TestBox<T, A> where
    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

impl<T, A> UnwindSafe for TestBox<T, A> where
    A: UnwindSafe,
    T: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[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]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 
[src]