/rust/registry/src/index.crates.io-1949cf8c6b5b557f/const-oid-0.10.2/src/buffer.rs
Line | Count | Source |
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 | 0 | pub const fn as_bytes(&self) -> &[u8] { |
16 | 0 | self.bytes.split_at(self.length as usize).0 |
17 | 0 | } |
18 | | |
19 | | /// Get the length of the BER message. |
20 | 0 | pub const fn len(&self) -> usize { |
21 | 0 | self.length as usize |
22 | 0 | } |
23 | | |
24 | | /// Const comparison of two buffers. |
25 | 0 | pub const fn eq(&self, rhs: &Self) -> bool { |
26 | 0 | if self.length != rhs.length { |
27 | 0 | return false; |
28 | 0 | } |
29 | | |
30 | 0 | let mut i = 0usize; |
31 | | |
32 | 0 | while i < self.len() { |
33 | 0 | if self.bytes[i] != rhs.bytes[i] { |
34 | 0 | return false; |
35 | 0 | } |
36 | | |
37 | | // Won't overflow due to `i < self.len()` check above |
38 | | #[allow(clippy::arithmetic_side_effects)] |
39 | 0 | { |
40 | 0 | i += 1; |
41 | 0 | } |
42 | | } |
43 | | |
44 | 0 | true |
45 | 0 | } |
46 | | } |
47 | | |
48 | | impl<const SIZE: usize> AsRef<[u8]> for Buffer<SIZE> { |
49 | 0 | fn as_ref(&self) -> &[u8] { |
50 | 0 | self.as_bytes() |
51 | 0 | } |
52 | | } |