steel_utils/serial/
mod.rs1use std::io::{Cursor, Result, Write};
3
4pub mod prefixed_read;
6pub mod prefixed_write;
8pub mod read;
10pub mod vec_as_map;
11pub mod write;
13
14pub use write::OptionalNbt;
15
16const DEFAULT_BOUND: usize = i16::MAX as _;
17
18pub trait ReadFrom: Sized {
20 fn read(data: &mut Cursor<&[u8]>) -> Result<Self>;
22}
23
24pub trait WriteTo {
26 fn write(&self, writer: &mut impl Write) -> Result<()>;
28
29 fn write_slice(values: &[Self], writer: &mut impl Write) -> Result<()>
31 where
32 Self: Sized,
33 {
34 for value in values {
35 value.write(writer)?;
36 }
37
38 Ok(())
39 }
40}
41
42pub trait PrefixedRead: Sized {
44 fn read_prefixed_bound<P: TryInto<usize> + ReadFrom>(
46 data: &mut Cursor<&[u8]>,
47 bound: usize,
48 ) -> Result<Self>;
49
50 fn read_prefixed<P: TryInto<usize> + ReadFrom>(data: &mut Cursor<&[u8]>) -> Result<Self> {
52 Self::read_prefixed_bound::<P>(data, DEFAULT_BOUND)
53 }
54}
55
56pub trait PrefixedWrite {
58 fn write_prefixed_bound<P: TryFrom<usize> + WriteTo>(
60 &self,
61 writer: &mut impl Write,
62 bound: usize,
63 ) -> Result<()>;
64
65 fn write_prefixed<P: TryFrom<usize> + WriteTo>(&self, writer: &mut impl Write) -> Result<()> {
67 self.write_prefixed_bound::<P>(writer, DEFAULT_BOUND)
68 }
69}