cbor_no_std/
io.rs

1#![cfg(feature = "no_std")]
2use alloc::vec::Vec;
3
4pub trait Writer {
5    fn write_byte(&mut self, byte: u8);
6    fn write_bytes(&mut self, bytes: Vec<u8>);
7}
8
9pub struct VecWriter<'a> {
10    pub output: &'a mut Vec<u8>
11}
12
13impl<'a> Writer for VecWriter<'a> {
14    fn write_byte(&mut self, byte: u8) {
15        self.write_bytes(vec![byte])
16    }
17
18    fn write_bytes(&mut self, mut bytes: Vec<u8>) {
19        self.output.append(&mut bytes)
20    }
21}
22
23pub trait Reader {
24    fn read_byte(&mut self) -> u8;
25    fn skip_byte(&mut self);
26    fn peek_byte(&mut self) -> u8;
27    fn read_n_bytes(&mut self, n: usize) -> Vec<u8>;
28}
29
30pub struct VecReader {
31    pub input: Vec<u8>
32}
33
34impl Reader for VecReader {
35    fn read_byte(&mut self) -> u8 {
36        match self.input.clone().split_first() {
37            Some((byte, rest)) => {
38                self.input = rest.to_vec();
39                *byte
40            }
41            None => 0
42        }
43    }
44
45    fn skip_byte(&mut self) {
46        let input = self.input.clone();
47        match input.split_first() {
48            Some((_, rest)) => {
49                self.input = rest.to_vec();
50            }
51            None => ()
52        };
53    }
54
55    fn peek_byte(&mut self) -> u8 {
56        match self.input.first() {
57            Some(byte) => *byte,
58            None => 0
59        }
60    }
61
62    fn read_n_bytes(&mut self, n: usize) -> Vec<u8> {
63        let input = self.input.clone();
64        let (bytes, rest) = input.split_at(n);
65        self.input = rest.to_vec();
66        bytes.to_vec()
67    }
68}