array_deque/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![deny(missing_docs)]
3//! A fixed-capacity circular buffer (ring buffer) implementation.
4//!
5//! This crate provides [`ArrayDeque`], a double-ended queue with a fixed capacity
6//! that uses a circular buffer for efficient operations at both ends. Unlike
7//! [`std::collections::VecDeque`], this implementation has a compile-time fixed
8//! capacity and will overwrite old elements when full.
9//!
10//! # Examples
11//!
12//! ```
13//! use array_deque::ArrayDeque;
14//!
15//! let mut deque = ArrayDeque::new(3);
16//! deque.push_back(1);
17//! deque.push_back(2);
18//! deque.push_back(3);
19//!
20//! assert_eq!(deque.pop_front(), Some(1));
21//! assert_eq!(deque.pop_back(), Some(3));
22//! ```
23//!
24//! # Features
25//!
26//! - **serde**: Enable serialization and deserialization support with serde.
27
28use core::fmt;
29
30mod array_deque;
31mod stack_array_deque;
32
33pub use array_deque::ArrayDeque;
34pub use stack_array_deque::StackArrayDeque;
35
36/// Error returned when converting into a fixed-capacity deque would exceed capacity.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct CapacityError {
39 /// Number of elements provided by the input collection.
40 pub len: usize,
41 /// Maximum capacity of the target deque.
42 pub capacity: usize,
43}
44
45impl fmt::Display for CapacityError {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 write!(
48 f,
49 "input length {} exceeds target capacity {}",
50 self.len, self.capacity
51 )
52 }
53}
54
55#[cfg(feature = "std")]
56impl std::error::Error for CapacityError {}