pub trait Codec<T> {
type Error;
// Required methods
fn encode(value: &T) -> Result<Vec<u8>, Self::Error>;
fn decode(value: &[u8]) -> Result<T, Self::Error>;
}Expand description
Optional conversion between application values and KV bytes.
Maps never apply a codec implicitly. The encoding is part of the application’s ledger format.
use ccf_app::Codec;
struct BigEndianU32;
impl Codec<u32> for BigEndianU32 {
type Error = std::array::TryFromSliceError;
fn encode(value: &u32) -> Result<Vec<u8>, Self::Error> {
Ok(value.to_be_bytes().to_vec())
}
fn decode(bytes: &[u8]) -> Result<u32, Self::Error> {
Ok(u32::from_be_bytes(bytes.try_into()?))
}
}
let bytes = BigEndianU32::encode(&42).unwrap();
assert_eq!(BigEndianU32::decode(&bytes).unwrap(), 42);
assert!(BigEndianU32::decode(&[42]).is_err());Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".