testnumbat_codec/
nested_de_input_owned.rs1use alloc::boxed::Box;
2
3use crate::{DecodeError, NestedDecodeInput};
4
5pub struct OwnedBytesNestedDecodeInput {
7 bytes: Box<[u8]>,
8 decode_index: usize,
9}
10
11impl OwnedBytesNestedDecodeInput {
12 pub fn new(bytes: Box<[u8]>) -> Self {
13 OwnedBytesNestedDecodeInput {
14 bytes,
15 decode_index: 0,
16 }
17 }
18
19 fn perform_read_into(&mut self, into: &mut [u8]) {
20 let len = into.len();
21 into.copy_from_slice(&self.bytes[self.decode_index..self.decode_index + len]);
22 self.decode_index += len;
23 }
24}
25
26impl NestedDecodeInput for OwnedBytesNestedDecodeInput {
27 fn remaining_len(&self) -> usize {
28 self.bytes.len() - self.decode_index
29 }
30
31 fn read_into(&mut self, into: &mut [u8]) -> Result<(), DecodeError> {
32 if into.len() > self.remaining_len() {
33 return Err(DecodeError::INPUT_TOO_SHORT);
34 }
35 self.perform_read_into(into);
36 Ok(())
37 }
38
39 fn read_into_or_exit<ExitCtx: Clone>(
40 &mut self,
41 into: &mut [u8],
42 c: ExitCtx,
43 exit: fn(ExitCtx, DecodeError) -> !,
44 ) {
45 if into.len() > self.remaining_len() {
46 exit(c, DecodeError::INPUT_TOO_SHORT);
47 }
48 self.perform_read_into(into);
49 }
50}