[][src]Struct wired::Queue

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

a First-In-First-Out data structure

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("/path/to/file.queue")?;

// 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

Methods

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

pub fn new(path: &str) -> 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("/path/to/file.queue")?;

Queue for structs:

use serde::{Deserialize, Serialize};

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

let queue = wired::Queue::<Example>::new("/path/to/file.queue")?;

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

get the amount of items currently in the queue

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

check if the queue is empty

pub fn wasted_file_space(&self) -> f64[src]

get the ratio of bytes marked for deletion

will return a value between 0.0 (optimal) and 1.0 (highly fragmented)

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("/path/to/file.queue")?;
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

Examples

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

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

defragment the database into a pristine state

This will rebuild the database file under the hood and swap out/delete the current one. This operation is quite expensive but frees up all unused disk space, so decide for yourself when you want to do this.

Examples

let mut queue = wired::Queue::<String>::new("/path/to/file.queue")?;
queue.compact()?;

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>

impl<T> !Send for Queue<T>

impl<T> !Sync for Queue<T>

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

impl<T> !UnwindSafe for Queue<T>

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>,