Struct Slab

Source
pub struct Slab<I>
where I: Id,
{ /* private fields */ }
Expand description

A slab-based id allocator which can deal with automatic reclamation as ids are freed.

§Examples

use idalloc::Slab;

let mut alloc = Slab::<u32>::new();

let mut alloc = Slab::<u32>::new();
assert_eq!(0, alloc.next());
assert_eq!(1, alloc.next());
alloc.free(0);
assert_eq!(0, alloc.next());
assert_eq!(2, alloc.next());
alloc.free(0);
alloc.free(0);
alloc.free(1);
assert_eq!(1, alloc.next());
assert_eq!(0, alloc.next());
assert_eq!(3, alloc.next());

Implementations§

Source§

impl<I> Slab<I>
where I: Id,

Source

pub fn new() -> Self

Construct a new slab allocator.

§Examples
use idalloc::Slab;

let mut alloc = Slab::<u32>::new();

let mut alloc = Slab::<u32>::new();
assert_eq!(0, alloc.next());
assert_eq!(1, alloc.next());
alloc.free(0);
assert_eq!(0, alloc.next());
Source

pub fn next(&mut self) -> I

Allocate the next id.

§Examples
let mut alloc = idalloc::Slab::<u32>::new();
assert_eq!(0u32, alloc.next());
assert_eq!(1u32, alloc.next());
Source

pub fn free(&mut self, index: I) -> bool

Free the specified id.

§Examples
let mut alloc = idalloc::Slab::<u32>::new();
let id = alloc.next();
assert!(!alloc.free(id + 1));
assert!(alloc.free(id));
assert!(!alloc.free(id));

Trait Implementations§

Source§

impl<I> Default for Slab<I>
where I: Id,

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<I> Freeze for Slab<I>
where I: Freeze,

§

impl<I> RefUnwindSafe for Slab<I>
where I: RefUnwindSafe,

§

impl<I> Send for Slab<I>
where I: Send,

§

impl<I> Sync for Slab<I>
where I: Sync,

§

impl<I> Unpin for Slab<I>
where I: Unpin,

§

impl<I> UnwindSafe for Slab<I>
where I: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.