Skip to main content

uts_core/codec/
imp.rs

1use crate::{alloc::vec::Vec, codec::*};
2
3mod alloy;
4#[cfg(feature = "bytes")]
5mod bytes;
6mod primitives;
7mod std_io;
8
9pub use std_io::{Reader, Writer};
10
11impl<A: Allocator> Encoder for Vec<u8, A> {
12    fn encode_byte(&mut self, byte: u8) -> Result<(), EncodeError> {
13        self.push(byte);
14        Ok(())
15    }
16
17    fn write_all(&mut self, buf: impl AsRef<[u8]>) -> Result<(), EncodeError> {
18        self.extend_from_slice(buf.as_ref());
19        Ok(())
20    }
21}
22
23impl Encoder for std::vec::Vec<u8> {
24    fn encode_byte(&mut self, byte: u8) -> Result<(), EncodeError> {
25        self.push(byte);
26        Ok(())
27    }
28
29    fn write_all(&mut self, buf: impl AsRef<[u8]>) -> Result<(), EncodeError> {
30        self.extend_from_slice(buf.as_ref());
31        Ok(())
32    }
33}
34
35impl Decoder for &[u8] {
36    fn decode_byte(&mut self) -> Result<u8, DecodeError> {
37        let Some((a, b)) = self.split_at_checked(1) else {
38            return Err(DecodeError::UnexpectedEof);
39        };
40        *self = b;
41        Ok(a[0])
42    }
43
44    fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), DecodeError> {
45        let len = buf.len();
46        let Some((a, b)) = self.split_at_checked(len) else {
47            return Err(DecodeError::UnexpectedEof);
48        };
49        buf.copy_from_slice(a);
50        *self = b;
51        Ok(())
52    }
53}