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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Implementation of the [Bytes] type

use super::RmpRead;
use crate::decode::RmpReadErr;
use core::fmt::{Display, Formatter};

/// Indicates that an error occurred reading from [Bytes]
#[derive(Debug)]
#[non_exhaustive]
// NOTE: We can't use thiserror because of no_std :(
pub enum BytesReadError {
    /// Indicates that there were not enough bytes.
    InsufficientBytes {
        expected: usize,
        actual: usize,
        position: u64,
    },
}

impl Display for BytesReadError {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match *self {
            BytesReadError::InsufficientBytes { expected, actual, position } => {
                write!(f, "Expected at least bytes {expected}, but only got {actual} (pos {position})")
            }
        }
    }
}
#[cfg(feature = "std")]
impl std::error::Error for BytesReadError {}
impl RmpReadErr for BytesReadError {}

/// A wrapper around `&[u8]` to read more efficiently.
///
/// This has a specialized implementation of `RmpWrite`
/// and has error type [Infallible](core::convert::Infallible).
///
/// This has the additional benefit of working on `#[no_std]` (unlike the builtin Read trait)
///
/// See also [serde_bytes::Bytes](https://docs.rs/serde_bytes/0.11/serde_bytes/struct.Bytes.html)
///
/// Unlike a plain `&[u8]` this also tracks an internal offset in the input (See [`Self::position`]).
///
/// This is used for (limited) compatibility with [`std::io::Cursor`]. Unlike a [Cursor](std::io::Cursor) it does
/// not support mark/reset.
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Bytes<'a> {
    /// The internal position of the input buffer.
    ///
    /// This is not required for correctness.
    /// It is only used for error reporting (and to implement [`Self::position`])
    current_position: u64,
    bytes: &'a [u8],
}
impl<'a> Bytes<'a> {
    /// Wrap an existing bytes slice.
    ///
    /// This sets the internal position to zero.
    #[inline]
    #[must_use]
    pub fn new(bytes: &'a [u8]) -> Self {
        Bytes { bytes, current_position: 0 }
    }
    /// Get a reference to the remaining bytes in the buffer.
    #[inline]
    #[must_use]
    pub fn remaining_slice(&self) -> &'a [u8] {
        self.bytes
    }
    /// Return the position of the input buffer.
    ///
    /// This is not required for correctness, it only exists to help mimic
    /// [`Cursor::position`](std::io::Cursor::position)
    #[inline]
    #[must_use]
    pub fn position(&self) -> u64 {
        self.current_position
    }
}
impl<'a> From<&'a [u8]> for Bytes<'a> {
    #[inline]
    fn from(bytes: &'a [u8]) -> Self {
        Bytes { bytes, current_position: 0 }
    }
}

impl RmpRead for Bytes<'_> {
    type Error = BytesReadError;

    #[inline]
    fn read_u8(&mut self) -> Result<u8, Self::Error> {
        if let Some((&first, newly_remaining)) = self.bytes.split_first() {
            self.bytes = newly_remaining;
            self.current_position += 1;
            Ok(first)
        } else {
            Err(BytesReadError::InsufficientBytes {
                expected: 1,
                actual: 0,
                position: self.current_position,
            })
        }
    }

    #[inline]
    fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
        let to_read = buf.len();
        if to_read <= self.bytes.len() {
            let (src, newly_remaining) = self.bytes.split_at(to_read);
            self.bytes = newly_remaining;
            self.current_position += to_read as u64;
            buf.copy_from_slice(src);
            Ok(())
        } else {
            Err(BytesReadError::InsufficientBytes {
                expected: to_read,
                actual: self.bytes.len(),
                position: self.current_position,
            })
        }
    }
}

#[cfg(not(feature = "std"))]
impl<'a> RmpRead for &'a [u8] {
    type Error = BytesReadError;

    fn read_u8(&mut self) -> Result<u8, Self::Error> {
        if let Some((&first, newly_remaining)) = self.split_first() {
            *self = newly_remaining;
            Ok(first)
        } else {
            Err(BytesReadError::InsufficientBytes {
                expected: 1,
                actual: 0,
                position: 0,
            })
        }
    }

    fn read_exact_buf(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> {
        let to_read = buf.len();
        if to_read <= self.len() {
            let (src, newly_remaining) = self.split_at(to_read);
            *self = newly_remaining;
            buf.copy_from_slice(src);
            Ok(())
        } else {
            Err(BytesReadError::InsufficientBytes {
                expected: to_read,
                actual: self.len(),
                position: 0,
            })
        }
    }
}