[][src]Struct wired::Stack

pub struct Stack<T> { /* fields omitted */ }

a Last-In-First-Out Database

A Stack is backed by a memory-mapped file, so the full content does not reside in RAM when not needed. It is type-safe over a generic type that can be serialized through serde.

What exactly is a Stack?

In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations:

  • push, which adds an element to the collection, and
  • pop, which removes the most recently added element that was not yet removed.

The order in which elements come off a stack gives rise to its alternative name, LIFO (last in, first out). Additionally, a peek operation may give access to the top without modifying the stack. The name "stack" for this type of structure comes from the analogy to a set of physical items stacked on top of each other, which makes it easy to take an item off the top of the stack, while getting to an item deeper in the stack may require taking off multiple other items first.

-- Wikipedia

Examples

// any datatype that can be serialized by serde works
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Example {
    num: i32,
}

// create a new db
let mut stack = wired::Stack::<Example>::new(file)?;

// insert an item
let item = Example { num: 42 };
stack.push(item)?;

// retrieve an item
let item = stack.pop()?;
dbg!(item); // Some(Example { num: 42 })

// try retrieve an item from the now-empty queue
let item = stack.pop()?;
dbg!(item); // None

Implementations

impl<T> Stack<T> where
    T: Serialize,
    T: Deserialize<'de>, 
[src]

pub fn new(file: File) -> Result<Self, Box<dyn Error>>[src]

Create a new database or open an existing one for the given location. The database is generic over a serializable datatype.

Examples

Stack for strings:

let stack = wired::Stack::<String>::new(file)?;

Stack for structs:

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Example {
    count: i32,
}

let stack = wired::Stack::<Example>::new(file)?;

pub fn len(&self) -> usize[src]

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

pub fn push(&mut self, data: T) -> Result<(), Box<dyn Error>>[src]

insert a new item at the end of the stack and persist to disk

Examples

let mut stack = wired::Stack::<String>::new(file)?;
let item = String::from("some item");
stack.push(item)?;

pub fn pop(&mut self) -> Result<Option<T>, Box<dyn Error>>[src]

remove the item at the and of the stack, persist to disk and return the item

Note: if you discard the popped item it will be lost permanently!

Examples

let mut stack = wired::Stack::<String>::new(file)?;
stack.push(String::from("some item"))?;
let item = stack.pop()?;

Trait Implementations

impl<T> Iterator for Stack<T> where
    T: Serialize,
    T: Deserialize<'de>, 
[src]

type Item = T

The type of the elements being iterated over.

Auto Trait Implementations

impl<T> RefUnwindSafe for Stack<T> where
    T: RefUnwindSafe

impl<T> Send for Stack<T> where
    T: Send

impl<T> Sync for Stack<T> where
    T: Sync

impl<T> Unpin for Stack<T> where
    T: Unpin

impl<T> UnwindSafe for Stack<T> where
    T: UnwindSafe

Blanket Implementations

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

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

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

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

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

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<I> IteratorRandom for I where
    I: Iterator
[src]

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.

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.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,