Struct gharial::TestBox

source ·
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,

source

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);
source

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,

source

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) };
source

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,

source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
source§

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

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

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

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug, A> Debug for TestBox<T, A>where A: GlobalAlloc + Debug,

source§

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

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

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

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

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

source§

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

Mutably dereferences the value.
source§

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

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

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

source§

fn from(val: T) -> Self

Converts to this type from the input type.
source§

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

source§

fn hash<H>(&self, state: &mut H)where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

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

source§

fn cmp(&self, rh: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

fn partial_cmp(&self, rh: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

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

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

const: unstable · source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V