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.
The 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.
Use Self::of_borrowed when the encoder accepts a borrowed view of
V instead of V itself.
§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 const fn of_borrowed<C, V, I>() -> Selfwhere
C: Default + ValueEncoder<I, Output = String> + ValueDecoder<str, Output = V> + 'static,
<C as ValueEncoder<I>>::Error: Error + Send + Sync + 'static,
<C as ValueDecoder<str>>::Error: Error + Send + Sync + 'static,
V: Borrow<I> + 'static,
I: ?Sized,
pub const fn of_borrowed<C, V, I>() -> Selfwhere
C: Default + ValueEncoder<I, Output = String> + ValueDecoder<str, Output = V> + 'static,
<C as ValueEncoder<I>>::Error: Error + Send + Sync + 'static,
<C as ValueDecoder<str>>::Error: Error + Send + Sync + 'static,
V: Borrow<I> + 'static,
I: ?Sized,
Creates a descriptor that encodes an owned value through a borrowed view.
The descriptor continues to accept and return the owned type V.
During each encode call it checks the erased value is exactly V, then
borrows an I view with Borrow for that call only. The view is
never stored. Each operation constructs a fresh C::default()
codec. Encoder and decoder errors must implement Error + Send + Sync + 'static; C and V themselves need not be Send or
Sync.
§Type Parameters
C: Default-constructible codec encodingIand decoding intoV.V: Owned value type erased by this descriptor and produced by decode.I: Borrowed input view accepted by the encoder; it may be unsized.
§Returns
Returns immutable function metadata without constructing a codec.
§Errors
Encoding returns a type mismatch if the erased value is not V, or
retains the codec’s source error. Decoding retains the decoder’s source
error.
§Examples
use std::io::Error;
use qubit_codec::{ValueDecoder, ValueEncoder, ValueStringCodecDescriptor};
#[derive(Default)]
struct Utf8;
impl ValueEncoder<[u8]> for Utf8 {
type Output = String;
type Error = Error;
fn encode(&mut self, input: &[u8]) -> Result<String, Error> {
String::from_utf8(input.to_vec()).map_err(Error::other)
}
}
impl ValueDecoder<str> for Utf8 {
type Output = Vec<u8>;
type Error = Error;
fn decode(&mut self, input: &str) -> Result<Vec<u8>, Error> {
Ok(input.as_bytes().to_vec())
}
}
let descriptor = ValueStringCodecDescriptor::of_borrowed::<Utf8, Vec<u8>, [u8]>();
let value = b"hello".to_vec();
assert_eq!(descriptor.encode(&value).unwrap(), "hello");
let decoded = descriptor.decode("hello").unwrap().downcast::<Vec<u8>>().unwrap();
assert_eq!(*decoded, value);The owned type must implement Borrow for the selected input view,
and the decoder output must be exactly V:
use std::io::Error;
use qubit_codec::{ValueDecoder, ValueEncoder, ValueStringCodecDescriptor};
#[derive(Default)] struct Codec;
impl ValueEncoder<[u8]> for Codec {
type Output = String; type Error = Error;
fn encode(&mut self, input: &[u8]) -> Result<String, Error> {
String::from_utf8(input.to_vec()).map_err(Error::other)
}
}
impl ValueDecoder<str> for Codec {
type Output = String; type Error = Error;
fn decode(&mut self, input: &str) -> Result<String, Error> { Ok(input.to_owned()) }
}
let _ = ValueStringCodecDescriptor::of_borrowed::<Codec, String, [u8]>();use std::io::Error;
use qubit_codec::{ValueDecoder, ValueEncoder, ValueStringCodecDescriptor};
#[derive(Default)] struct Codec;
impl ValueEncoder<[u8]> for Codec {
type Output = String; type Error = Error;
fn encode(&mut self, input: &[u8]) -> Result<String, Error> {
String::from_utf8(input.to_vec()).map_err(Error::other)
}
}
impl ValueDecoder<str> for Codec {
type Output = String; type Error = Error;
fn decode(&mut self, input: &str) -> Result<String, Error> { Ok(input.to_owned()) }
}
let _ = ValueStringCodecDescriptor::of_borrowed::<Codec, Vec<u8>, [u8]>();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.