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 + 'static,
<C as ValueDecoder<str>>::Error: Error + '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 + 'static,
<C as ValueDecoder<str>>::Error: Error + 'static,
V: 'static,
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.
Trait Implementations§
Source§impl Clone for ValueStringCodecDescriptor
impl Clone for ValueStringCodecDescriptor
impl Copy for ValueStringCodecDescriptor
Auto Trait Implementations§
impl Freeze for ValueStringCodecDescriptor
impl RefUnwindSafe for ValueStringCodecDescriptor
impl Send for ValueStringCodecDescriptor
impl Sync for ValueStringCodecDescriptor
impl Unpin for ValueStringCodecDescriptor
impl UnsafeUnpin for ValueStringCodecDescriptor
impl UnwindSafe for ValueStringCodecDescriptor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more