Skip to main content

bincake_core/
read_write.rs

1//! Defines convenience traits and macros for serialization and deserialization,
2//! and implements them for source and destination types.
3
4use taped::Tape;
5
6use crate::{
7    error::{DecodeError, EncodeError},
8    serialize::Serialize,
9};
10
11pub trait Read {
12    fn read<T: Serialize>(&mut self) -> Result<T, DecodeError>;
13}
14
15pub trait Write {
16    fn write<T: Serialize>(&mut self, value: &T) -> Result<(), EncodeError>;
17}
18
19impl Read for Tape<'_, u8> {
20    fn read<T: Serialize>(&mut self) -> Result<T, DecodeError> {
21        T::decode(self)
22    }
23}
24
25impl Write for Vec<u8> {
26    fn write<T: Serialize>(&mut self, value: &T) -> Result<(), EncodeError> {
27        T::encode(value, self)
28    }
29}