Coverage Report

Created: 2025-12-14 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/der-0.7.10/src/bytes_ref.rs
Line
Count
Source
1
//! Common handling for types backed by byte slices with enforcement of a
2
//! library-level length limitation i.e. `Length::max()`.
3
4
use crate::{
5
    DecodeValue, DerOrd, EncodeValue, Error, Header, Length, Reader, Result, StrRef, Writer,
6
};
7
use core::cmp::Ordering;
8
9
#[cfg(feature = "alloc")]
10
use crate::StrOwned;
11
12
/// Byte slice newtype which respects the `Length::max()` limit.
13
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
14
pub(crate) struct BytesRef<'a> {
15
    /// Precomputed `Length` (avoids possible panicking conversions)
16
    pub length: Length,
17
18
    /// Inner value
19
    pub inner: &'a [u8],
20
}
21
22
impl<'a> BytesRef<'a> {
23
    /// Constant value representing an empty byte slice.
24
    pub const EMPTY: Self = Self {
25
        length: Length::ZERO,
26
        inner: &[],
27
    };
28
29
    /// Create a new [`BytesRef`], ensuring that the provided `slice` value
30
    /// is shorter than `Length::max()`.
31
0
    pub fn new(slice: &'a [u8]) -> Result<Self> {
32
        Ok(Self {
33
0
            length: Length::try_from(slice.len())?,
34
0
            inner: slice,
35
        })
36
0
    }
37
38
    /// Borrow the inner byte slice
39
0
    pub fn as_slice(&self) -> &'a [u8] {
40
0
        self.inner
41
0
    }
42
43
    /// Get the [`Length`] of this [`BytesRef`]
44
0
    pub fn len(self) -> Length {
45
0
        self.length
46
0
    }
47
48
    /// Is this [`BytesRef`] empty?
49
0
    pub fn is_empty(self) -> bool {
50
0
        self.len() == Length::ZERO
51
0
    }
52
}
53
54
impl AsRef<[u8]> for BytesRef<'_> {
55
0
    fn as_ref(&self) -> &[u8] {
56
0
        self.as_slice()
57
0
    }
58
}
59
60
impl<'a> DecodeValue<'a> for BytesRef<'a> {
61
0
    fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self> {
62
0
        reader.read_slice(header.length).and_then(Self::new)
63
0
    }
64
}
65
66
impl EncodeValue for BytesRef<'_> {
67
0
    fn value_len(&self) -> Result<Length> {
68
0
        Ok(self.length)
69
0
    }
70
71
0
    fn encode_value(&self, writer: &mut impl Writer) -> Result<()> {
72
0
        writer.write(self.as_ref())
73
0
    }
74
}
75
76
impl Default for BytesRef<'_> {
77
0
    fn default() -> Self {
78
0
        Self {
79
0
            length: Length::ZERO,
80
0
            inner: &[],
81
0
        }
82
0
    }
83
}
84
85
impl DerOrd for BytesRef<'_> {
86
0
    fn der_cmp(&self, other: &Self) -> Result<Ordering> {
87
0
        Ok(self.as_slice().cmp(other.as_slice()))
88
0
    }
89
}
90
91
impl<'a> From<StrRef<'a>> for BytesRef<'a> {
92
0
    fn from(s: StrRef<'a>) -> BytesRef<'a> {
93
0
        let bytes = s.as_bytes();
94
0
        debug_assert_eq!(bytes.len(), usize::try_from(s.length).expect("overflow"));
95
96
0
        BytesRef {
97
0
            inner: bytes,
98
0
            length: s.length,
99
0
        }
100
0
    }
101
}
102
103
#[cfg(feature = "alloc")]
104
impl<'a> From<&'a StrOwned> for BytesRef<'a> {
105
0
    fn from(s: &'a StrOwned) -> BytesRef<'a> {
106
0
        let bytes = s.as_bytes();
107
0
        debug_assert_eq!(bytes.len(), usize::try_from(s.length).expect("overflow"));
108
109
0
        BytesRef {
110
0
            inner: bytes,
111
0
            length: s.length,
112
0
        }
113
0
    }
114
}
115
116
impl<'a> TryFrom<&'a [u8]> for BytesRef<'a> {
117
    type Error = Error;
118
119
0
    fn try_from(slice: &'a [u8]) -> Result<Self> {
120
0
        Self::new(slice)
121
0
    }
122
}
123
124
// Implement by hand because the derive would create invalid values.
125
// Make sure the length and the inner.len matches.
126
#[cfg(feature = "arbitrary")]
127
impl<'a> arbitrary::Arbitrary<'a> for BytesRef<'a> {
128
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
129
        let length = u.arbitrary()?;
130
        Ok(Self {
131
            length,
132
            inner: u.bytes(u32::from(length) as usize)?,
133
        })
134
    }
135
136
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
137
        arbitrary::size_hint::and(Length::size_hint(depth), (0, None))
138
    }
139
}
140
141
#[cfg(feature = "alloc")]
142
mod allocating {
143
    use super::BytesRef;
144
    use crate::{referenced::RefToOwned, BytesOwned};
145
146
    impl<'a> RefToOwned<'a> for BytesRef<'a> {
147
        type Owned = BytesOwned;
148
0
        fn ref_to_owned(&self) -> Self::Owned {
149
0
            BytesOwned::from(*self)
150
0
        }
151
    }
152
}