ringo_buff/
hybrid_array.rs

1use crate::{CircularBuffer, Storage};
2use ::hybrid_array::Array;
3
4pub use ::hybrid_array::ArraySize;
5
6impl<N: ArraySize> crate::sealed::StorageBase for Array<u8, N> {}
7
8impl<N: ArraySize> Storage for Array<u8, N> {
9    fn len(&self) -> usize {
10        self.as_slice().len()
11    }
12
13    fn as_slice(&self) -> &[u8] {
14        self.as_slice()
15    }
16
17    fn as_mut_slice(&mut self) -> &mut [u8] {
18        self.as_mut_slice()
19    }
20
21    fn split_at(&self, offset: usize) -> (&[u8], &[u8]) {
22        self.as_slice().split_at(offset)
23    }
24
25    fn split_at_mut(&mut self, offset: usize) -> (&mut [u8], &mut [u8]) {
26        self.as_mut_slice().split_at_mut(offset)
27    }
28}
29
30/// A circular buffer backed by `hybrid_array::Array`.
31///
32/// This implementation is used when the `hybrid-array` feature is **enabled**.
33/// It allows for type-level sizes (e.g., `U10`) compatible with `hybrid-array` & `typenum`.
34pub type StackBuffer<N> = CircularBuffer<Array<u8, N>>;
35
36impl<N: ArraySize> StackBuffer<N> {
37    /// Creates a new buffer.
38    ///
39    /// # Panics
40    ///
41    /// Panics if array size is 0.
42    ///
43    /// # Examples
44    ///
45    /// ```
46    /// use ringo_buff::{StackBuffer, ArraySize};
47    /// use hybrid_array::sizes::U128;
48    ///
49    /// let buf: StackBuffer<U128> = StackBuffer::new();
50    /// assert_eq!(buf.capacity(), 128);
51    /// ```
52    pub fn new() -> Self {
53        assert!(N::to_usize() > 0);
54        Self::_new_with_storage(Array::default())
55    }
56}