1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use core::convert::TryFrom;
use crate::{Length, Result};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Slice<'a> {
inner: &'a [u8],
length: Length,
}
impl<'a> Slice<'a> {
pub fn new(slice: &'a [u8]) -> Result<Self> {
Ok(Self {
inner: slice,
length: Length::try_from(slice.len())?,
})
}
pub fn as_bytes(&self) -> &'a [u8] {
self.inner
}
pub fn length(self) -> Length {
self.length
}
pub fn is_empty(self) -> bool {
self.length() == Length::zero()
}
}
impl AsRef<[u8]> for Slice<'_> {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}