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::{DecodeError, EncodeError, Serialize};
7
8pub trait Read {
9    fn read<T: Serialize>(&mut self) -> Result<T, DecodeError>;
10}
11
12pub trait Write {
13    fn write<T: Serialize>(&mut self, value: &T) -> Result<(), EncodeError>;
14}
15
16impl Read for Tape<'_, u8> {
17    fn read<T: Serialize>(&mut self) -> Result<T, DecodeError> {
18        T::decode(self)
19    }
20}
21
22impl Write for Vec<u8> {
23    fn write<T: Serialize>(&mut self, value: &T) -> Result<(), EncodeError> {
24        T::encode(value, self)
25    }
26}