der/
byte_slice.rs

1//! Common handling for types backed by byte slices with enforcement of a
2//! library-level length limitation i.e. `Length::max()`.
3
4use crate::{
5    str_slice::StrSlice, DecodeValue, Decoder, DerOrd, EncodeValue, Encoder, Error, Length, Result,
6};
7use core::cmp::Ordering;
8
9/// Byte slice newtype which respects the `Length::max()` limit.
10#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
11pub(crate) struct ByteSlice<'a> {
12    /// Precomputed `Length` (avoids possible panicking conversions)
13    length: Length,
14
15    /// Inner value
16    inner: &'a [u8],
17}
18
19impl<'a> ByteSlice<'a> {
20    /// Constant value representing an empty byte slice.
21    pub const EMPTY: Self = Self {
22        length: Length::ZERO,
23        inner: &[],
24    };
25
26    /// Create a new [`ByteSlice`], ensuring that the provided `slice` value
27    /// is shorter than `Length::max()`.
28    pub fn new(slice: &'a [u8]) -> Result<Self> {
29        Ok(Self {
30            length: Length::try_from(slice.len())?,
31            inner: slice,
32        })
33    }
34
35    /// Borrow the inner byte slice
36    pub fn as_bytes(&self) -> &'a [u8] {
37        self.inner
38    }
39
40    /// Get the [`Length`] of this [`ByteSlice`]
41    pub fn len(self) -> Length {
42        self.length
43    }
44
45    /// Is this [`ByteSlice`] empty?
46    pub fn is_empty(self) -> bool {
47        self.len() == Length::ZERO
48    }
49}
50
51impl AsRef<[u8]> for ByteSlice<'_> {
52    fn as_ref(&self) -> &[u8] {
53        self.as_bytes()
54    }
55}
56
57impl<'a> DecodeValue<'a> for ByteSlice<'a> {
58    fn decode_value(decoder: &mut Decoder<'a>, length: Length) -> Result<Self> {
59        decoder.bytes(length).and_then(Self::new)
60    }
61}
62
63impl EncodeValue for ByteSlice<'_> {
64    fn value_len(&self) -> Result<Length> {
65        Ok(self.length)
66    }
67
68    fn encode_value(&self, encoder: &mut Encoder<'_>) -> Result<()> {
69        encoder.bytes(self.as_ref())
70    }
71}
72
73impl Default for ByteSlice<'_> {
74    fn default() -> Self {
75        Self {
76            length: Length::ZERO,
77            inner: &[],
78        }
79    }
80}
81
82impl DerOrd for ByteSlice<'_> {
83    fn der_cmp(&self, other: &Self) -> Result<Ordering> {
84        Ok(self.as_bytes().cmp(other.as_bytes()))
85    }
86}
87
88impl<'a> From<&'a [u8; 1]> for ByteSlice<'a> {
89    fn from(byte: &'a [u8; 1]) -> ByteSlice<'a> {
90        Self {
91            length: Length::ONE,
92            inner: byte,
93        }
94    }
95}
96
97impl<'a> From<StrSlice<'a>> for ByteSlice<'a> {
98    fn from(s: StrSlice<'a>) -> ByteSlice<'a> {
99        let bytes = s.as_bytes();
100        debug_assert_eq!(bytes.len(), usize::try_from(s.length).expect("overflow"));
101
102        ByteSlice {
103            inner: bytes,
104            length: s.length,
105        }
106    }
107}
108
109impl<'a> TryFrom<&'a [u8]> for ByteSlice<'a> {
110    type Error = Error;
111
112    fn try_from(slice: &'a [u8]) -> Result<Self> {
113        Self::new(slice)
114    }
115}