Struct metrics_util::AtomicBucket[][src]

pub struct AtomicBucket<T> { /* fields omitted */ }
Expand description

A lock-free bucket with snapshot capabilities.

This bucket is implemented as a singly-linked list of blocks, where each block is a small buffer that can hold a handful of elements. There is no limit to how many elements can be in the bucket at a time. Blocks are dynamically allocated as elements are pushed into the bucket.

Unlike a queue, buckets cannot be drained element by element: callers must iterate the whole structure. Reading the bucket happens in a quasi-reverse fashion, to allow writers to make forward progress without affecting the iteration of the previously written values.

For example, in a scenario where an internal block can hold 4 elements, and the caller has written 10 elements to the bucket, you would expect to see the values in this order when iterating:

[6 7 8 9] [2 3 4 5] [0 1]

Block sizes are dependent on the target architecture, where each block can hold N items, and N is the number of bits in the target architecture’s pointer width.

Implementations

impl<T> AtomicBucket<T>[src]

pub fn new() -> Self[src]

Creates a new, empty bucket.

pub fn is_empty(&self) -> bool[src]

Checks whether or not this bucket is empty.

pub fn push(&self, value: T)[src]

Pushes an element into the bucket.

pub fn data(&self) -> Vec<T> where
    T: Clone
[src]

Collects all of the elements written to the bucket.

This operation can be slow as it involves allocating enough space to hold all of the elements within the bucket. Consider data_with to incrementally iterate the internal blocks within the bucket.

Elements are in partial reverse order: blocks are iterated in reverse order, but the elements within them will appear in their original order.

pub fn data_with<F>(&self, f: F) where
    F: FnMut(&[T]), 
[src]

Iterates all of the elements written to the bucket, invoking f for each block.

Elements are in partial reverse order: blocks are iterated in reverse order, but the elements within them will appear in their original order.

pub fn clear(&self)[src]

Clears the bucket.

Deallocation of the internal blocks happens only when all readers have finished, and so will not necessarily occur during or immediately preceding this method.

Note

This method will not affect reads that are already in progress.

pub fn clear_with<F>(&self, f: F) where
    F: FnMut(&[T]), 
[src]

Clears the bucket, invoking f for every block that will be cleared.

Deallocation of the internal blocks happens only when all readers have finished, and so will not necessarily occur during or immediately preceding this method.

This method is useful for accumulating values and then observing them, in a way that allows the caller to avoid visiting the same values again the next time.

This method allows a pattern of observing values before they’re cleared, with a clear demarcation. A similar pattern used in the wild would be to have some data structure, like a vector, which is continuously filled, and then eventually swapped out with a new, empty vector, allowing the caller to read all of the old values while new values are being written, over and over again.

Note

This method will not affect reads that are already in progress.

Trait Implementations

impl<T: Debug> Debug for AtomicBucket<T>[src]

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

Formats the value using the given formatter. Read more

impl<T> Default for AtomicBucket<T>[src]

fn default() -> Self[src]

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

Auto Trait Implementations

impl<T> !RefUnwindSafe for AtomicBucket<T>

impl<T> Send for AtomicBucket<T> where
    T: Send + Sync

impl<T> Sync for AtomicBucket<T> where
    T: Send + Sync

impl<T> Unpin for AtomicBucket<T>

impl<T> !UnwindSafe for AtomicBucket<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Pointable for T[src]

pub const ALIGN: usize[src]

The alignment of pointer.

type Init = T

The type for initializers.

pub unsafe fn init(init: <T as Pointable>::Init) -> usize[src]

Initializes a with the given initializer. Read more

pub unsafe fn deref<'a>(ptr: usize) -> &'a T[src]

Dereferences the given pointer. Read more

pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T[src]

Mutably dereferences the given pointer. Read more

pub unsafe fn drop(ptr: usize)[src]

Drops the object pointed to by the given pointer. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.