Struct Buckets

Source
pub struct Buckets<T, const BUCKETS: usize> { /* private fields */ }
Expand description

The low-level primitive behind Vec: a lazily-initialized array implemented as a sequence of buckets with sizes of increasing powers of two.

The BUCKETS generic parameter controls the maximum capacity, and the inline size, of the type. See buckets_for_index_bits for a convenient way to calculate its desired value.

Implementations§

Source§

impl<T, const BUCKETS: usize> Buckets<T, BUCKETS>

Source

pub const fn new() -> Self

Construct a new, empty, Buckets.

Source

pub fn get(&self, index: Index<BUCKETS>) -> Option<&T>

Retrieve the value at the specified index, or None if it has not been allocated yet.

§Examples
let mut buckets = <Buckets<u8, 5>>::new();
let index = buckets::Index::new(20).unwrap();

assert_eq!(buckets.get(index), None);
buckets.get_or_alloc(index);
assert_eq!(*buckets.get(index).unwrap(), 0);
Source

pub fn get_mut(&mut self, index: Index<BUCKETS>) -> Option<&mut T>

Retrieve a unique reference to the value at the specified index, or None if it has not been allocated yet.

§Examples
let mut buckets = <Buckets<u8, 5>>::new();
let index = buckets::Index::new(20).unwrap();

assert_eq!(buckets.get_mut(index), None);
buckets.get_or_alloc(index);
assert_eq!(*buckets.get_mut(index).unwrap(), 0);
Source

pub unsafe fn get_unchecked(&self, index: Index<BUCKETS>) -> &T

Retrieve the value at the specified index, without performing bounds checking.

§Safety

The element must be allocated.

§Examples
let mut buckets = <Buckets<u8, 5>>::new();
let index = buckets::Index::new(20).unwrap();
buckets.reserve_mut(index);
assert_eq!(*unsafe { buckets.get_unchecked(index) }, 0);
Source

pub unsafe fn get_unchecked_mut(&mut self, index: Index<BUCKETS>) -> &mut T

Retrieve unique reference to the value at the specified index, without performing bounds checking.

§Safety

The element must be allocated.

§Examples
let mut buckets = <Buckets<u8, 5>>::new();
let index = buckets::Index::new(20).unwrap();
buckets.reserve_mut(index);
assert_eq!(*unsafe { buckets.get_unchecked_mut(index) }, 0);
Source

pub fn get_or_alloc(&self, index: Index<BUCKETS>) -> &T
where T: MaybeZeroable,

Retrieve the value at the specified index, or allocate the bucket if it hasn’t been allocated yet.

§Example
let mut buckets = <Buckets<u8, 5>>::new();

let index = buckets::Index::new(48).unwrap();
assert_eq!(*buckets.get_or_alloc(index), 0);

*buckets.get_mut(index).unwrap() += 3;
assert_eq!(*buckets.get_or_alloc(index), 3);

// Prior indices are not necessary allocated.
assert_eq!(buckets.get(buckets::Index::new(0).unwrap()), None);
§Panics

May panic if index is too large or allocation fails.

Source

pub fn get_or_alloc_mut(&mut self, index: Index<BUCKETS>) -> &mut T
where T: MaybeZeroable,

Retrieve a unique reference to the value at the specified index, or allocate the bucket if it hasn’t been allocated yet.

§Example
let mut buckets = <Buckets<u8, 5>>::new();

let index = buckets::Index::new(48).unwrap();
assert_eq!(*buckets.get_or_alloc_mut(index), 0);

*buckets.get_or_alloc_mut(index) += 3;
assert_eq!(*buckets.get(index).unwrap(), 3);

// Prior indices are not necessary allocated.
assert_eq!(buckets.get(buckets::Index::new(0).unwrap()), None);
§Panics

May panic if index is too large or allocation fails.

Source

pub fn reserve(&self, index: Index<BUCKETS>)
where T: MaybeZeroable,

Reserve capacity up to and including the provided index.

After calling this method, .get_or_alloc(n) is guaranteed not to allocate.

§Examples
let buckets = <Buckets<u8, 5>>::new();
let index = buckets::Index::new(20).unwrap();
assert_eq!(buckets.get(index), None);

buckets.reserve(index);
assert_eq!(*buckets.get(index).unwrap(), 0);
§Panics

May panic if index is too large or allocation fails.

Source

pub fn reserve_mut(&mut self, index: Index<BUCKETS>)
where T: MaybeZeroable,

Reserve capacity up to and including the provided index.

Unlike reserve, this method takes a mutable reference to the Buckets, avoiding synchronization.

§Examples
let mut buckets = <Buckets<u8, 5>>::new();
let index = buckets::Index::new(20).unwrap();

buckets.reserve_mut(index);
assert_eq!(*buckets.get(index).unwrap(), 0);
§Panics

May panic if index is too large or allocation fails.

Source

pub fn truncate(&mut self, n: Index<BUCKETS>)

Truncate the Buckets, keeping at least the first n elements.

This method truncates to the smallest capacity that preserves any items before n, but may include subsequent elements due to the bucket layout.

§Examples
let mut buckets = <Buckets<u8, 16>>::new();

// We reserve capacity for 1000 elements, so all elements will be present.
buckets.reserve_mut(buckets::Index::new(1000).unwrap());
assert_eq!(*buckets.get(buckets::Index::new(12).unwrap()).unwrap(), 0);
assert_eq!(*buckets.get(buckets::Index::new(1000).unwrap()).unwrap(), 0);

// If we truncate to a smaller capacity, the later elements will not be present.
buckets.truncate(buckets::Index::new(13).unwrap());
assert_eq!(*buckets.get(buckets::Index::new(12).unwrap()).unwrap(), 0);
assert_eq!(buckets.get(buckets::Index::new(1000).unwrap()), None);
Source

pub fn iter(&self) -> Iter<'_, T, BUCKETS>

Iterate over (index, &value) pairs of the Buckets.

§Examples
let mut buckets = <Buckets<u8, 5>>::new();
buckets.reserve_mut(buckets::Index::new(25).unwrap());
assert!(25 < buckets.iter().count());
Source

pub fn iter_mut(&mut self) -> IterMut<'_, T, BUCKETS>

Iterate over (index, &mut value) pairs of the Buckets.

§Examples
let mut buckets = <Buckets<u8, 5>>::new();
buckets.reserve_mut(buckets::Index::new(25).unwrap());
assert!(25 < buckets.iter_mut().count());

for (i, (_, val)) in buckets.iter_mut().enumerate() {
    *val = i as u8;
}
assert_eq!(*buckets.get(buckets::Index::new(10).unwrap()).unwrap(), 10);

Trait Implementations§

Source§

impl<T: Debug, const BUCKETS: usize> Debug for Buckets<T, BUCKETS>

Source§

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

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

impl<T, const BUCKETS: usize> Default for Buckets<T, BUCKETS>

Source§

fn default() -> Self

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

impl<T, const BUCKETS: usize> Drop for Buckets<T, BUCKETS>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T, const BUCKETS: usize> IntoIterator for &'a Buckets<T, BUCKETS>

Source§

type Item = (Index<BUCKETS>, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, BUCKETS>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T, const BUCKETS: usize> IntoIterator for &'a mut Buckets<T, BUCKETS>

Source§

type Item = (Index<BUCKETS>, &'a mut T)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T, BUCKETS>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, const BUCKETS: usize> IntoIterator for Buckets<T, BUCKETS>

Source§

type Item = (Index<BUCKETS>, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, BUCKETS>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: RefUnwindSafe, const BUCKETS: usize> RefUnwindSafe for Buckets<T, BUCKETS>

Source§

impl<T: Send, const BUCKETS: usize> Send for Buckets<T, BUCKETS>

Source§

impl<T: Sync, const BUCKETS: usize> Sync for Buckets<T, BUCKETS>

Source§

impl<T: UnwindSafe, const BUCKETS: usize> UnwindSafe for Buckets<T, BUCKETS>

Auto Trait Implementations§

§

impl<T, const BUCKETS: usize> !Freeze for Buckets<T, BUCKETS>

§

impl<T, const BUCKETS: usize> Unpin for Buckets<T, BUCKETS>

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.