# Crate hipool

Expand description

## §Examples

``````use hipool::{ MemPool, Boxed};
let pool = MemPool::new(0);
let psize = Boxed::new_in(&pool, 100).unwrap();
assert_eq!(*psize, 100);``````

``````use core::ptr;
use hipool::{ MemPool, Boxed};
use core::mem::MaybeUninit;

let pool = MemPool::new(0);
struct Foo {
val: i32,
}
let foo = Boxed::new_then_in(&pool, | obj: &mut MaybeUninit<Foo> | {
let obj = obj.as_mut_ptr();
unsafe {
}
Ok(())
}).unwrap();
assert_eq!(foo.val, 100);``````

``````use hipool::{ BufPool, Boxed};
let mut buf = [0_u8; 100];
let mut pool = BufPool::new(&mut buf);
let psize = Boxed::new_in(&pool, 100).unwrap();
assert_eq!(*psize, 100);``````

``````use hipool::{ MemPool, Boxed };
use core::mem::MaybeUninit;

let mut pool = MemPool::new_boxed(0).unwrap();
let int_array = Boxed::new_slice_then_in(&*pool, 100, |_, ele: &mut MaybeUninit<i32>| {
ele.write(0);
Ok(())
}).unwrap();
int_array.iter().for_each(|n| assert_eq!(*n, 0));``````

``````use hipool::{ MemPool, Boxed };
use core::mem::MaybeUninit;
use core::alloc::Layout;

let pool = MemPool::new_boxed(0).unwrap();

let val = Boxed::new_in(&*pool, 1).unwrap();
assert_eq!(*val, 1);

let val = Boxed::new_in(&*pool, 0).unwrap();
assert_eq!(*val, 0);

let val = Boxed::new_then_in(&*pool, |val: &mut MaybeUninit<i32>| {
val.write(100);
Ok(())
}).unwrap();
assert_eq!(*val, 100);

let ivals = [0, 1, 2, 3];
let vals = Boxed::uninit_slice_in::<i32>(&*pool, ivals.len()).unwrap();
let vals = vals.write_slice_then(|n, uninit| {uninit.write(ivals[n]); Ok(())}).unwrap();
assert_eq!(vals[0], 0);
assert_eq!(vals[1], 1);
assert_eq!(vals[2], 2);
assert_eq!(vals[3], 3);

let vals = unsafe { Boxed::new_buf_in(&*pool,
Layout::array::<i32>(2).unwrap()).unwrap().cast_slice::<i32>(2) };
vals.iter().for_each(|val| println!(" {:?} ", val));

let vals = Boxed::new_slice_then_in(&*pool, 2, | n, val: &mut MaybeUninit<i32> | {
val.write(n as i32);
Ok(())
}).unwrap();
assert_eq!(vals[0], 0);
assert_eq!(vals[1], 1);``````