macro_rules! register_value_bytes_codec {
(id = $id:expr, codec = $codec:ty, value = $value:ty $(,)?) => { ... };
}Expand description
Registers a default-constructible bidirectional bytes codec.
The macro must be invoked at module scope; it defines the descriptor, a
registration function, and an inventory submission in an anonymous const.
The id must be a constant expression. Use a literal, constant, or
const fn result; a runtime function call cannot define a static ID.
The encoder and decoder error types must implement Error + Send + Sync + 'static, as required by crate::ValueBytesCodecDescriptor::of.
§Examples
A registry-only example with a local codec:
use core::convert::Infallible;
use qubit_codec::{ValueBytesCodecRegistry, ValueDecoder, ValueEncoder};
use qubit_codec::register_value_bytes_codec;
#[derive(Default)]
struct BytesCodec;
impl ValueEncoder<Vec<u8>> for BytesCodec {
type Output = Vec<u8>;
type Error = Infallible;
fn encode(&mut self, value: &Vec<u8>) -> Result<Vec<u8>, Infallible> { Ok(value.clone()) }
}
impl ValueDecoder<[u8]> for BytesCodec {
type Output = Vec<u8>;
type Error = Infallible;
fn decode(&mut self, input: &[u8]) -> Result<Vec<u8>, Infallible> { Ok(input.to_vec()) }
}
register_value_bytes_codec!(id = "example.bytes", codec = BytesCodec, value = Vec<u8>);
let registry = ValueBytesCodecRegistry::try_global().unwrap();
let descriptor = registry.get("example.bytes").unwrap().descriptor();
let encoded = descriptor.encode(&vec![1_u8, 2]).unwrap();
assert_eq!(vec![1_u8, 2], encoded);
let decoded = descriptor.decode(&encoded).unwrap();
assert_eq!(vec![1_u8, 2], *decoded.downcast::<Vec<u8>>().unwrap());An invalid ID fails during macro expansion:
ⓘ
register_value_bytes_codec!(id = "invalid-id", codec = BytesCodec, value = Vec<u8>);