Skip to main content

layer_crypto/
deque_buffer.rs

1//! A deque-like growable buffer that supports efficient prepend.
2
3use std::ops::{Index, IndexMut};
4use std::slice::SliceIndex;
5
6/// Growable byte buffer that supports efficient front-extension.
7#[derive(Clone, Debug)]
8pub struct DequeBuffer {
9    buf: Vec<u8>,
10    head: usize,
11    default_head: usize,
12}
13
14impl DequeBuffer {
15    /// Create with reserved space for `back` bytes in the back and `front` in the front.
16    pub fn with_capacity(back: usize, front: usize) -> Self {
17        let mut buf = Vec::with_capacity(front + back);
18        buf.resize(front, 0);
19        Self {
20            buf,
21            head: front,
22            default_head: front,
23        }
24    }
25
26    /// Reset the buffer to empty (but keep allocation).
27    pub fn clear(&mut self) {
28        self.buf.truncate(self.default_head);
29        self.buf[..self.head].fill(0);
30        self.head = self.default_head;
31    }
32
33    /// Prepend `slice` to the front.
34    pub fn extend_front(&mut self, slice: &[u8]) {
35        if self.head >= slice.len() {
36            self.head -= slice.len();
37        } else {
38            let shift = slice.len() - self.head;
39            self.buf.extend(std::iter::repeat_n(0, shift));
40            self.buf.rotate_right(shift);
41            self.head = 0;
42        }
43        self.buf[self.head..self.head + slice.len()].copy_from_slice(slice);
44    }
45
46    /// Number of bytes in the buffer.
47    pub fn len(&self) -> usize {
48        self.buf.len() - self.head
49    }
50
51    /// True if empty.
52    pub fn is_empty(&self) -> bool {
53        self.head == self.buf.len()
54    }
55}
56
57impl AsRef<[u8]> for DequeBuffer {
58    fn as_ref(&self) -> &[u8] {
59        &self.buf[self.head..]
60    }
61}
62impl AsMut<[u8]> for DequeBuffer {
63    fn as_mut(&mut self) -> &mut [u8] {
64        &mut self.buf[self.head..]
65    }
66}
67impl<I: SliceIndex<[u8]>> Index<I> for DequeBuffer {
68    type Output = I::Output;
69    fn index(&self, i: I) -> &Self::Output {
70        self.as_ref().index(i)
71    }
72}
73impl<I: SliceIndex<[u8]>> IndexMut<I> for DequeBuffer {
74    fn index_mut(&mut self, i: I) -> &mut Self::Output {
75        self.as_mut().index_mut(i)
76    }
77}
78impl Extend<u8> for DequeBuffer {
79    fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
80        self.buf.extend(iter);
81    }
82}
83impl<'a> Extend<&'a u8> for DequeBuffer {
84    fn extend<T: IntoIterator<Item = &'a u8>>(&mut self, iter: T) {
85        self.buf.extend(iter);
86    }
87}