Skip to main content

Slab

Struct Slab 

Source
pub struct Slab<T> { /* private fields */ }
Expand description

Values addressed by a small stable number.

Implementations§

Source§

impl<T: Bytes> Slab<T>

Source

pub fn new() -> Slab<T>

An empty slab, which has not allocated anything.

Source

pub fn with_capacity(n: usize) -> Slab<T>

An empty slab with room for n values before it grows.

Source

pub fn len(&self) -> usize

How many values are in it.

Source

pub fn is_empty(&self) -> bool

Whether there are none.

Source

pub fn insert(&mut self, value: T) -> u32

Put value in and answer where it went.

A free slot if there is one, and the end otherwise. Nothing moves, so every number handed out before stays good.

§Panics

If there are already MAX_SLOTS slots. That is four billion values of one type in one database, which is not a number a caller can reach by accident, and the alternative is a Result on the hot path of every SADD against a key that does not exist yet.

Source

pub fn get(&self, at: u32) -> Option<&T>

The value at at, or None if that slot is free or does not exist.

Source

pub fn get_mut(&mut self, at: u32) -> Option<&mut T>

The value at at, to be changed in place.

This is the only way to change a value, which is what lets the running total be a total rather than a guess: whatever the caller does with the reference, the slab already knows it has to ask this slot again.

Source

pub fn remove(&mut self, at: u32) -> Option<T>

Take the value at at out and free the slot.

Answers None if the slot was already free, without touching the free list, so freeing twice is inert rather than a loop.

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Every value in it, in no order a caller should lean on.

This is for counting bytes and for saving, both of which want all of them and neither of which cares which came first.

Source

pub fn clear(&mut self)

Drop everything and hand the memory back.

This is FLUSHALL, where keeping a vector of four million free slots around for a database the client just emptied would be the wrong answer.

Source

pub fn slot_bytes(&self) -> usize

What the slots themselves cost, not counting what the values point at.

Source

pub fn memory_bytes(&self) -> usize

👎Deprecated since 0.3.8:

renamed to slot_bytes

The old name for Slab::slot_bytes, which answered the same number.

Kept because a patch release does not take a public item away, and there are now three ways to ask a slab what it costs rather than one, so the name that does not say which of the three it means had to go. It goes at the next minor.

Source

pub fn value_bytes(&self) -> usize

What the values are holding, asked of every one of them.

The honest walk, for the places that want the number exactly and do not care what it costs to get: INFO memory, MEMORY USAGE and the tests. It does not touch the running total and does not need it to be on.

Source

pub fn settled_bytes(&mut self) -> usize

The same number, asked only of the values that could have changed.

With tracking on this asks the slots touched since the last call and no others, which is what makes a maxmemory server able to afford the question once a batch. With tracking off it is Slab::value_bytes.

Source

pub fn track_bytes(&mut self, on: bool)

Start or stop keeping the running total.

Starting costs one walk, because a total has to start from somewhere and the slab may already be holding a million sets when the client sets the limit. Stopping costs nothing and gives the two lists back.

Setting it to what it already is does nothing at all, which matters because CONFIG SET maxmemory on a server that already had one would otherwise pay for that walk every time.

Trait Implementations§

Source§

impl<T: Debug> Debug for Slab<T>

Source§

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

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

impl<T: Bytes> Default for Slab<T>

Source§

fn default() -> Slab<T>

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

Auto Trait Implementations§

§

impl<T> Freeze for Slab<T>
where Vec<Slot<T>>: Freeze,

§

impl<T> RefUnwindSafe for Slab<T>
where Vec<Slot<T>>: RefUnwindSafe,

§

impl<T> Send for Slab<T>
where Vec<Slot<T>>: Send,

§

impl<T> Sync for Slab<T>
where Vec<Slot<T>>: Sync,

§

impl<T> Unpin for Slab<T>
where Vec<Slot<T>>: Unpin,

§

impl<T> UnsafeUnpin for Slab<T>
where Vec<Slot<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for Slab<T>
where Vec<Slot<T>>: 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.