pub struct Slots<IT, const N: usize> { /* private fields */ }
Expand description

Slots object that provides strict access control for the stored data.

The struct has two type parameters:

  • IT is the type of the stored data
  • N is the number of slots.

For more information, see the module level documentation

Implementations

Creates a new, empty Slots object.

Returns a read-only iterator. The iterator can be used to read data from all occupied slots.

Note: Do not rely on the order in which the elements are returned.

slots.store(2).unwrap();
slots.store(4).unwrap();
slots.store(6).unwrap();

assert_eq!(true, slots.iter().any(|&x| x < 3));

Returns the number of slots

let slots: Slots<f32, 4> = Slots::new();

assert_eq!(4, slots.capacity());

Returns the number of occupied slots

let mut slots: Slots<_, 4> = Slots::new();

assert_eq!(0, slots.count());

slots.store(3).unwrap();
slots.store(6).unwrap();

assert_eq!(2, slots.count());

Returns whether all the slots are occupied and the next store() will fail.

let mut slots: Slots<_, 4> = Slots::new();

slots.store(3).unwrap();
slots.store(4).unwrap();
slots.store(5).unwrap();

assert_eq!(false, slots.is_full());

slots.store(6).unwrap();

assert_eq!(true, slots.is_full());

Store an element in a free slot and return the key to access it.

Storing a variable takes ownership over it. If the storage is full, the inserted data is returned in the return value.

Remove and return the element that belongs to the key.

Read the element that belongs to the key.

This operation does not move ownership so the function callback must be used to access the stored element. The callback may return arbitrary derivative of the element.


let k = slots.store(3).unwrap();

assert_eq!(4, slots.read(&k, |elem| {
    elem + 1
}));

Read the element that belongs to a particular index. Since the index may point to a free slot or outside the collection, this operation may return None without invoking the callback.

This operation does not move ownership so the function callback must be used to access the stored element. The callback may return arbitrary derivative of the element.

This operation is fallible. If index addresses a free slot, None is returned.


let k = slots.store(3).unwrap();
let idx = k.index();

assert_eq!(Some(4), slots.try_read(idx, |elem| {
    elem + 1
}));

slots.take(k);

assert_eq!(None, slots.try_read(idx, |elem| {
    elem + 1
}));

Access the element that belongs to the key for modification.

This operation does not move ownership so the function callback must be used to access the stored element. The callback may return arbitrary derivative of the element.


let k = slots.store(3).unwrap();

assert_eq!("found", slots.modify(&k, |elem| {
    *elem = *elem + 1;

    "found"
}));

// Assert that the stored data was modified
assert_eq!(4, slots.take(k));

Trait Implementations

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.