arraydeque/
behavior.rs

1//! Behavior semantics for `ArrayDeque`.
2//!
3//! `ArrayDeque` provides two different behaviors, `Saturating` and `Wrapping`,
4//! determining whether to remove existing element automatically when pushing
5//! to a full deque.
6//!
7//! The behavior is indicated by a marker type parameter of `ArrayDeque`,
8//! which defaults to `Saturating`.
9//!
10//! ## Saturating
11//!
12//! Pushing any element when `ArrayDeque` is full will directly return an `Err(CapacityError)`
13//! containing the element attempting to push, leaving the `ArrayDeque` unchanged.
14//!
15//! ```
16//! use arraydeque::{ArrayDeque, Saturating, CapacityError};
17//!
18//! let mut tester: ArrayDeque<_, 2, Saturating> = ArrayDeque::new();
19//!
20//! assert_eq!(tester.push_back(1), Ok(()));
21//! assert_eq!(tester.push_back(2), Ok(()));
22//! assert_eq!(tester.push_back(3), Err(CapacityError { element: 3 }));
23//! ```
24//!
25//! ## Wrapping
26//!
27//! Pushing any element when `ArrayDeque` is full will pop an element at
28//! the other side to spare room.
29//!
30//! ```
31//! use arraydeque::{ArrayDeque, Wrapping};
32//!
33//! let mut tester: ArrayDeque<_, 2, Wrapping> = ArrayDeque::new();
34//!
35//! assert_eq!(tester.push_back(1), None);
36//! assert_eq!(tester.push_back(2), None);
37//! assert_eq!(tester.push_back(3), Some(1));
38//! ```
39
40/// Marker trait for indicating behaviors of `ArrayDeque`.
41pub trait Behavior {}
42
43/// Behavior for `ArrayDeque` that specifies saturating write semantics.
44pub struct Saturating;
45
46impl Behavior for Saturating {}
47
48/// Behavior for `ArrayDeque` that specifies wrapping write semantics.
49pub struct Wrapping;
50
51impl Behavior for Wrapping {}