untrusted/
no_panic.rs

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
/// A wrapper around a slice that exposes no functions that can panic.
///
/// Intentionally avoids implementing `Debug`, `Eq`, and `PartialEq` to avoid
/// creating a side channel that would leak information about the value.
#[derive(Clone, Copy)]
pub struct Slice<'a> {
    bytes: &'a [u8],
}

impl<'a> Slice<'a> {
    #[inline]
    pub const fn new(bytes: &'a [u8]) -> Self {
        Self { bytes }
    }

    #[inline]
    pub fn get(&self, i: usize) -> Option<&u8> {
        self.bytes.get(i)
    }

    #[inline]
    pub fn subslice(&self, r: core::ops::Range<usize>) -> Option<Self> {
        self.bytes.get(r).map(|bytes| Self { bytes })
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.bytes.is_empty()
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.bytes.len()
    }

    #[inline]
    pub fn as_slice_less_safe(&self) -> &'a [u8] {
        self.bytes
    }
}