[][src]Struct wired::Queue

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

a First-In-First-Out Database

A Queue 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 Queue?

In computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and removal from the other end of the sequence. By convention, the end of the sequence at which elements are added is called the back, tail, or rear of the queue and the end at which elements are removed is called the head or front of the queue, analogously to the words used when people line up to wait for goods or services.

  • The operation of adding an element to the rear of the queue is known as enqueue,
  • and the operation of removing an element from the front is known as dequeue.

-- 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 queue = wired::Queue::<Example>::new(file)?;

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

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

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

Implementations

impl<T> Queue<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

Queue for strings:

let queue = wired::Queue::<String>::new(file)?;

Queue for structs:

use serde::{Deserialize, Serialize};

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

let queue = wired::Queue::<Example>::new(file)?;

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

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

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

insert a new item in front of the queue and persist to disk

Examples

let mut queue = wired::Queue::<String>::new(file)?;
let item = String::from("some item");
queue.enqueue(item)?;

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

remove the item at the back of the queue, persist to disk and return the item

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

Examples

let mut queue = wired::Queue::<String>::new(file)?;
queue.enqueue(String::from("some item"))?;
let item = queue.dequeue()?;

Trait Implementations

impl<T> Iterator for Queue<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 Queue<T> where
    T: RefUnwindSafe

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

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

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

impl<T> UnwindSafe for Queue<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>,