opentdf_protocol/binary/traits.rs
1//! Traits for binary serialization and deserialization
2
3use std::io::{self, Read, Write};
4
5/// Trait for types that can be read from binary format
6pub trait BinaryRead: Sized {
7 /// Read this type from a binary reader
8 fn read_from<R: Read>(reader: &mut R) -> io::Result<Self>;
9}
10
11/// Trait for types that can be written to binary format
12pub trait BinaryWrite {
13 /// Write this type to a binary writer
14 fn write_to<W: Write>(&self, writer: &mut W) -> io::Result<()>;
15
16 /// Get the size in bytes when serialized
17 fn serialized_size(&self) -> usize;
18}
19
20/// Trait for types that support both reading and writing
21pub trait BinarySerialize: BinaryRead + BinaryWrite {}
22
23// Blanket implementation
24impl<T: BinaryRead + BinaryWrite> BinarySerialize for T {}