Crate holodeque[][src]

Expand description

Array- and slice-backed double-ended queues in 100% safe Rust.

This crate provides ArrayDeque and SliceDeque, fixed-size ring buffers with interfaces similar to the standard library’s VecDeque.

holodeque makes use of the unstable array_map feature to provide Default initialization of arbitrarily-sized arrays. As a result, a nightly compiler is required until this feature is stabilized. See the tracking issue for its current status.

Example

use holodeque::{ArrayDeque, CapacityError};

const NUM_TASKS: usize = 8;

#[derive(Debug, Default, PartialEq, Eq)]
struct Task(&'static str);

enum Priority {
    Low,
    High,
}

fn add_task(
    deque: &mut ArrayDeque<Task, NUM_TASKS>,
    task: Task,
    priority: Priority,
) -> Result<(), CapacityError<Task>> {
    match priority {
        Priority::Low => deque.push_back(task),
        Priority::High => deque.push_front(task),
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut tasks: ArrayDeque<Task, NUM_TASKS> = ArrayDeque::new();

    add_task(&mut tasks, Task("take hike"), Priority::Low)?;
    add_task(&mut tasks, Task("call mom"), Priority::High)?;
    add_task(&mut tasks, Task("eat pizza"), Priority::Low)?;

    assert_eq!(tasks.pop_front(), Some(Task("call mom")));
    assert_eq!(tasks.pop_front(), Some(Task("take hike")));
    assert_eq!(tasks.pop_front(), Some(Task("eat pizza")));
    assert_eq!(tasks.pop_front(), None);

    Ok(())
}

Features

Safe initialization mechanism

The containers provided by holodeque use the Default implementation of an element type to safely initialize unused space. This contrasts with other collection types such as arrayvec::ArrayVec, which represent unused space using MaybeUninit. This mechanism is borrowed from tinyvec.

Re-exports

pub use crate::array_deque::ArrayDeque;
pub use crate::slice_deque::SliceDeque;

Modules

A double-ended queue with fixed capacity, backed by an array.

A double-ended queue with fixed capacity, backed by a slice.

Structs

An error that occurs when attempting to add an item to a deque which is already full.