pub struct ValueStringCodecDescriptor { /* private fields */ }Expand description
An immutable, safely erased bidirectional string codec for one value type.
Each operation creates a fresh default codec; no codec state is retained between calls. Type identities are process-local and names are diagnostic, not stable IDs.
§Examples
use core::convert::Infallible;
use qubit_codec::ValueDecoder;
use qubit_codec::ValueEncoder;
use qubit_codec::ValueStringCodecDescriptor;
#[derive(Default)]
struct Text;
impl ValueEncoder<u32> for Text {
type Output = String;
type Error = Infallible;
fn encode(&mut self, input: &u32) -> Result<String, Infallible> {
Ok(input.to_string())
}
}
impl ValueDecoder<str> for Text {
type Output = u32;
type Error = Infallible;
fn decode(&mut self, input: &str) -> Result<u32, Infallible> {
Ok(input.len() as u32)
}
}
let descriptor = ValueStringCodecDescriptor::of::<Text, u32>();
// `encode` takes an erased `&dyn Any`, so the value type must be named.
let value: u32 = 7;
assert_eq!(descriptor.encode(&value).unwrap(), "7");
let decoded = descriptor.decode("hello").unwrap();
assert_eq!(*decoded.downcast::<u32>().unwrap(), 5);Implementations§
Source§impl ValueStringCodecDescriptor
impl ValueStringCodecDescriptor
Sourcepub const fn of<C, V>() -> Selfwhere
C: Default + ValueEncoder<V, Output = String> + ValueDecoder<str, Output = V> + 'static,
<C as ValueEncoder<V>>::Error: Error + Send + Sync + 'static,
<C as ValueDecoder<str>>::Error: Error + Send + Sync + 'static,
V: 'static,
pub const fn of<C, V>() -> Selfwhere
C: Default + ValueEncoder<V, Output = String> + ValueDecoder<str, Output = V> + 'static,
<C as ValueEncoder<V>>::Error: Error + Send + Sync + 'static,
<C as ValueDecoder<str>>::Error: Error + Send + Sync + 'static,
V: 'static,
Creates a descriptor for codec C and value type V.
Both codec error types must implement Send + Sync so execution errors
can retain their source across thread boundaries. The codec and value
types themselves need not implement these traits; V only needs to be
'static because decoded values are returned through Any.
§Type Parameters
C: Default-constructible string encoder/decoder with owned errors.V: Owned value type accepted and produced byC.
§Returns
Returns immutable function metadata without constructing a codec.
Sourcepub fn codec_type_id(&self) -> TypeId
pub fn codec_type_id(&self) -> TypeId
Returns the process-local identity of the concrete codec.
§Returns
Returns the TypeId of the codec this descriptor was built for.
Sourcepub fn codec_type_name(&self) -> &'static str
pub fn codec_type_name(&self) -> &'static str
Returns the diagnostic name of the concrete codec.
§Returns
Returns the type_name of the codec, for diagnostics only.
Sourcepub fn value_type_id(&self) -> TypeId
pub fn value_type_id(&self) -> TypeId
Returns the process-local identity of the decoded value.
§Returns
Returns the TypeId of the value this descriptor accepts and produces.
Sourcepub fn value_type_name(&self) -> &'static str
pub fn value_type_name(&self) -> &'static str
Returns the diagnostic name of the decoded value.
§Returns
Returns the type_name of the value, for diagnostics only.