ThriftAdapter

Trait ThriftAdapter 

Source
pub trait ThriftAdapter {
    type StandardType;
    type AdaptedType: Clone + Debug + PartialEq + Send + Sync;
    type Error: Into<Error> + Debug;

    // Required methods
    fn from_thrift(
        value: Self::StandardType,
    ) -> Result<Self::AdaptedType, Self::Error>;
    fn to_thrift(value: &Self::AdaptedType) -> Self::StandardType;

    // Provided methods
    fn from_thrift_field<T: ThriftAnnotations>(
        value: Self::StandardType,
        _field_id: i16,
    ) -> Result<Self::AdaptedType, Self::Error> { ... }
    fn to_thrift_field<T: ThriftAnnotations>(
        value: &Self::AdaptedType,
        _field_id: i16,
    ) -> Self::StandardType { ... }
    fn from_thrift_default<T: ThriftAnnotations>(
        value: Self::StandardType,
        field_id: i16,
    ) -> Self::AdaptedType { ... }
}

Required Associated Types§

Source

type StandardType

Aka the “from” type.

Source

type AdaptedType: Clone + Debug + PartialEq + Send + Sync

Aka the “to” type.

Source

type Error: Into<Error> + Debug

The error type thrown if from_thrift fails during deserialization.

Required Methods§

Source

fn from_thrift( value: Self::StandardType, ) -> Result<Self::AdaptedType, Self::Error>

Converts an instance of StandardType to AdaptedType during deserialization.

The Err returned by this method will be propagated as a deserialization error.

Source

fn to_thrift(value: &Self::AdaptedType) -> Self::StandardType

Converts from the given AdaptedType back to the given StandardType during serialization.

This must be an infallible operation as serialize is infallible.

WARNING: you should be particularly cautious when using .unwrap() or any other panic-able methods in this method. If this method panics, it will be at serialization time and not at the Thrift struct creation time, meaning it will be extremely difficult to debug what the true “source” of the panic.

If your AdaptedType -> StandardType conversion is truly fallible, you probably shouldn’t use an adapter to begin with.

Provided Methods§

Source

fn from_thrift_field<T: ThriftAnnotations>( value: Self::StandardType, _field_id: i16, ) -> Result<Self::AdaptedType, Self::Error>

Method called when this adapter is used on a Thrift struct’s field. Provides information about the specific field ID in field_id. The type of the struct that owns this field is passed in as T.

Defaults to calling from_thrift.

Source

fn to_thrift_field<T: ThriftAnnotations>( value: &Self::AdaptedType, _field_id: i16, ) -> Self::StandardType

Method called when this adapter is used on a Thrift struct’s field. Provides information about the specific field ID in field_id. The type of the struct that owns this field is passed in as T.

Defaults to calling to_thrift.

Source

fn from_thrift_default<T: ThriftAnnotations>( value: Self::StandardType, field_id: i16, ) -> Self::AdaptedType

Method called when the adapted type is not present in a field during deserialization or is populated with ..Default::default(). The value passed here is the default original type value for the field. This can be used to record that the field was not present inside of your adapted type.

This method must be infallible, as it will be called when Default::default() is used (which is infallible).

WARNING: This defaults to calling from_thrift_field and assumes from_thrift_field will not return an Err for the default value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<A> ThriftAdapter for ListMapAdapter<A>
where A: ThriftAdapter,

Source§

impl<A> ThriftAdapter for SetMapAdapter<A>

Source§

impl<Adapter, StandardType, AdaptedType> ThriftAdapter for Adapter
where Adapter: NewTypeAdapter<StandardType = StandardType, AdaptedType = AdaptedType>, AdaptedType: From<StandardType> + Into<StandardType> + Clone + Debug + Send + Sync + PartialEq,

Source§

impl<Fst, Snd> ThriftAdapter for LayeredThriftAdapter<Fst, Snd>

Source§

impl<KA, KV> ThriftAdapter for MapMapAdapter<KA, KV>

Source§

impl<T> ThriftAdapter for IdentityAdapter<T>
where T: Clone + Debug + Send + Sync + PartialEq,