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