| 1 | //! Array-backed buffer for BER bytes. |
| 2 | |
| 3 | /// Array-backed buffer for storing BER computed at compile-time. |
| 4 | #[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)] |
| 5 | pub struct Buffer<const SIZE: usize> { |
| 6 | /// Length in bytes |
| 7 | pub(crate) length: u8, |
| 8 | |
| 9 | /// Array containing BER/DER-serialized bytes (no header) |
| 10 | pub(crate) bytes: [u8; SIZE], |
| 11 | } |
| 12 | |
| 13 | impl<const SIZE: usize> Buffer<SIZE> { |
| 14 | /// Borrow the inner byte slice. |
| 15 | pub const fn as_bytes(&self) -> &[u8] { |
| 16 | self.bytes.split_at(self.length as usize).0 |
| 17 | } |
| 18 | |
| 19 | /// Get the length of the BER message. |
| 20 | pub const fn len(&self) -> usize { |
| 21 | self.length as usize |
| 22 | } |
| 23 | |
| 24 | /// Const comparison of two buffers. |
| 25 | pub const fn eq(&self, rhs: &Self) -> bool { |
| 26 | if self.length != rhs.length { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | let mut i = 0usize; |
| 31 | |
| 32 | while i < self.len() { |
| 33 | if self.bytes[i] != rhs.bytes[i] { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | // Won't overflow due to `i < self.len()` check above |
| 38 | #[allow(clippy::arithmetic_side_effects)] |
| 39 | { |
| 40 | i += 1; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | true |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl<const SIZE: usize> AsRef<[u8]> for Buffer<SIZE> { |
| 49 | fn as_ref(&self) -> &[u8] { |
| 50 | self.as_bytes() |
| 51 | } |
| 52 | } |