steel_utils/serial/
mod.rs1use std::io::{Cursor, Result, Write};
3
4pub mod prefixed_read;
6pub mod prefixed_write;
8pub mod read;
10pub mod write;
12
13pub use write::OptionalNbt;
14
15const DEFAULT_BOUND: usize = i16::MAX as _;
16
17pub trait ReadFrom: Sized {
19 fn read(data: &mut Cursor<&[u8]>) -> Result<Self>;
21}
22
23pub trait WriteTo {
25 fn write(&self, writer: &mut impl Write) -> Result<()>;
27}
28
29pub trait PrefixedRead: Sized {
31 fn read_prefixed_bound<P: TryInto<usize> + ReadFrom>(
33 data: &mut Cursor<&[u8]>,
34 bound: usize,
35 ) -> Result<Self>;
36
37 fn read_prefixed<P: TryInto<usize> + ReadFrom>(data: &mut Cursor<&[u8]>) -> Result<Self> {
39 Self::read_prefixed_bound::<P>(data, DEFAULT_BOUND)
40 }
41}
42
43pub trait PrefixedWrite {
45 fn write_prefixed_bound<P: TryFrom<usize> + WriteTo>(
47 &self,
48 writer: &mut impl Write,
49 bound: usize,
50 ) -> Result<()>;
51
52 fn write_prefixed<P: TryFrom<usize> + WriteTo>(&self, writer: &mut impl Write) -> Result<()> {
54 self.write_prefixed_bound::<P>(writer, DEFAULT_BOUND)
55 }
56}