pub trait ContextualSerialize<Context> {
    // Required method
    fn contextual_serialize<S>(
        &self,
        serializer: S,
        context: &Context
    ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
       where S: Serializer;

    // Provided methods
    fn serialize<S, TContext>(
        &self,
        serializer: S,
        context: TContext
    ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
       where S: Serializer,
             TContext: Into<Context> { ... }
    fn serializable<TContext, 'a, 'b>(
        &'a self,
        context: TContext
    ) -> ContextSerializable<'a, Self, Context>
       where TContext: Into<Context> { ... }
}
Expand description

This trait is used where context is required to correctly serialize a value.

Typically, this is due to needing to know the current network to display addresses. Other forms of Context are also possible.

The Context used should typically just be a wrapper type around references, and so be a small, cheap, ephemeral value on the stack (if it’s not just optimized away entirely). It is therefore recommended that the Context implement Copy, to make it very easy to pass around and re-use.

Required Methods§

fn contextual_serialize<S>( &self, serializer: S, context: &Context ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serializes the value to the given serde::Serializer, making use of the provided context. See also serialize, which is typically easier to use, as it takes an Into<Context> instead of a &Context.

Any custom errors during serialization will need mapping into a custom serde error, which basically wraps a String, via: serde::ser::Error::custom(error: Displayable).

Provided Methods§

fn serialize<S, TContext>( &self, serializer: S, context: TContext ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer, TContext: Into<Context>,

Serializes the value to the given serde::Serializer, making use of the provided context. See also contextual_serialize, which takes a &Context instead of an Into<Context>.

Alternatively, the serializable method can be used to create an object that directly implements serde::Serialize, for passing to serde functions.

fn serializable<TContext, 'a, 'b>( &'a self, context: TContext ) -> ContextSerializable<'a, Self, Context>
where TContext: Into<Context>,

Returns an object implementing serde::Serialize, which can be passed to serde functions.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'a, 'a2, 't, 'de, 's1, 's2, E> ContextualSerialize<SerializationContext<'s2, 'a2, E>> for SerializableType<'a, 't, 'de, 's1, 's2, E>

source§

impl<'s, 'a, 'b, E> ContextualSerialize<SerializationParameters<'s, 'a, E>> for RawPayload<'b, E>

source§

impl<'s, 'a, 'b, E> ContextualSerialize<SerializationParameters<'s, 'a, E>> for RawValue<'b, E>

source§

impl<'t, 'de, 's1, 's2, 's, 'a, E> ContextualSerialize<SerializationContext<'s, 'a, E>> for SerializableFields<'t, 'de, 's1, 's2, E>

source§

impl<'t, 'de, 's1, 's, 'a, E> ContextualSerialize<SerializationContext<'s, 'a, E>> for SerializableArrayElements<'t, 'de, 's1, E>

source§

impl<'t, 'de, 's1, 's, 'a, E> ContextualSerialize<SerializationContext<'s, 'a, E>> for SerializableMapElements<'t, 'de, 's1, E>

source§

impl<'t, 'de, 's1, 's, 'a, E> ContextualSerialize<SerializationContext<'s, 'a, E>> for SerializableMapEntry<'t, 'de, 's1, E>