Skip to main content

ValueBytesCodecDescriptor

Struct ValueBytesCodecDescriptor 

Source
pub struct ValueBytesCodecDescriptor { /* private fields */ }
Expand description

An immutable, safely erased bidirectional bytes codec for one value type.

Each operation creates a fresh default codec; no codec state is retained. Type identities are process-local and names are diagnostic, not stable IDs.

§Examples

use core::convert::Infallible;

use qubit_codec::ValueBytesCodecDescriptor;
use qubit_codec::ValueDecoder;
use qubit_codec::ValueEncoder;

#[derive(Default)]
struct Bytes;
impl ValueEncoder<Vec<u8>> for Bytes {
    type Error = Infallible;
    type Output = Vec<u8>;
    fn encode(&mut self, value: &Vec<u8>) -> Result<Vec<u8>, Infallible> {
        Ok(value.clone())
    }
}
impl ValueDecoder<[u8]> for Bytes {
    type Error = Infallible;
    type Output = Vec<u8>;
    fn decode(&mut self, input: &[u8]) -> Result<Vec<u8>, Infallible> {
        Ok(input.to_vec())
    }
}
let descriptor = ValueBytesCodecDescriptor::of::<Bytes, Vec<u8>>();
let bytes = descriptor.encode(&vec![1_u8, 2]).unwrap();
let decoded = descriptor.decode(&bytes).unwrap();
assert_eq!(*decoded.downcast::<Vec<u8>>().unwrap(), vec![1, 2]);

Implementations§

Source§

impl ValueBytesCodecDescriptor

Source

pub const fn of<C, V>() -> Self
where C: Default + ValueEncoder<V, Output = Vec<u8>> + ValueDecoder<[u8], Output = V> + 'static, <C as ValueEncoder<V>>::Error: Error + Send + Sync + 'static, <C as ValueDecoder<[u8]>>::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 bytes encoder/decoder with owned errors.
  • V: Owned value type accepted and produced by C.
§Returns

Returns immutable function metadata without constructing a codec.

Source

pub fn codec_type_id(&self) -> TypeId

Returns the process-local codec type identity.

§Returns

Returns metadata for the type selected by Self::of, without allocation.

Source

pub fn codec_type_name(&self) -> &'static str

Returns the diagnostic codec type name.

§Returns

Returns metadata for the type selected by Self::of, without allocation.

Source

pub fn value_type_id(&self) -> TypeId

Returns the process-local value type identity.

§Returns

Returns metadata for the type selected by Self::of, without allocation.

Source

pub fn value_type_name(&self) -> &'static str

Returns the diagnostic value type name.

§Returns

Returns metadata for the type selected by Self::of, without allocation.

Source

pub fn encode( &self, value: &dyn Any, ) -> Result<Vec<u8>, ValueCodecExecutionError>

Encodes one erased value after checking its concrete type.

§Parameters
  • value: Borrowed value whose concrete type must match the descriptor.
§Returns

Returns owned bytes produced by a fresh default codec.

§Errors

Returns a type mismatch or the typed encoder source error.

Source

pub fn decode( &self, input: &[u8], ) -> Result<Box<dyn Any>, ValueCodecExecutionError>

Decodes one byte slice into a safely erased value of the declared type.

§Parameters
  • input: Bytes borrowed for one decoding operation.
§Returns

Returns a boxed owned value that can be downcast to the declared type.

§Errors

Returns the typed decoder source error.

Trait Implementations§

Source§

impl Clone for ValueBytesCodecDescriptor

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for ValueBytesCodecDescriptor

Source§

impl Debug for ValueBytesCodecDescriptor

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult

Formats descriptor metadata without invoking either codec function.

§Parameters
  • formatter: Destination receiving the diagnostic type names.
§Returns

Returns success after both names have been formatted.

§Errors

Returns the formatting error if the destination rejects output.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.