Trait breez_sdk_core::bitcoin::util::key::secp256k1::serde::ser::SerializeMap

source ·
pub trait SerializeMap {
    type Ok;
    type Error: Error;

    // Required methods
    fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
       where T: Serialize + ?Sized;
    fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
       where T: Serialize + ?Sized;
    fn end(self) -> Result<Self::Ok, Self::Error>;

    // Provided method
    fn serialize_entry<K, V>(
        &mut self,
        key: &K,
        value: &V,
    ) -> Result<(), Self::Error>
       where K: Serialize + ?Sized,
             V: Serialize + ?Sized { ... }
}
Expand description

Returned from Serializer::serialize_map.

§Example use

use serde::ser::{Serialize, SerializeMap, Serializer};

impl<K, V> Serialize for HashMap<K, V>
where
    K: Serialize,
    V: Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(Some(self.len()))?;
        for (k, v) in self {
            map.serialize_entry(k, v)?;
        }
        map.end()
    }
}

§Example implementation

The example data format presented on the website demonstrates an implementation of SerializeMap for a basic JSON data format.

Required Associated Types§

source

type Ok

Must match the Ok type of our Serializer.

source

type Error: Error

Must match the Error type of our Serializer.

Required Methods§

source

fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
where T: Serialize + ?Sized,

Serialize a map key.

If possible, Serialize implementations are encouraged to use serialize_entry instead as it may be implemented more efficiently in some formats compared to a pair of calls to serialize_key and serialize_value.

source

fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
where T: Serialize + ?Sized,

Serialize a map value.

§Panics

Calling serialize_value before serialize_key is incorrect and is allowed to panic or produce bogus results.

source

fn end(self) -> Result<Self::Ok, Self::Error>

Finish serializing a map.

Provided Methods§

source

fn serialize_entry<K, V>( &mut self, key: &K, value: &V, ) -> Result<(), Self::Error>
where K: Serialize + ?Sized, V: Serialize + ?Sized,

Serialize a map entry consisting of a key and a value.

Some Serialize types are not able to hold a key and value in memory at the same time so SerializeMap implementations are required to support serialize_key and serialize_value individually. The serialize_entry method allows serializers to optimize for the case where key and value are both available. Serialize implementations are encouraged to use serialize_entry if possible.

The default implementation delegates to serialize_key and serialize_value. This is appropriate for serializers that do not care about performance or are not able to optimize serialize_entry any better than this.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

§

impl<'a, 'se> SerializeMap for &'a mut Serializer<'se>

§

type Ok = usize

§

type Error = Asn1DerError

§

fn serialize_key<T>(&mut self, _key: &T) -> Result<(), Asn1DerError>
where T: Serialize + ?Sized,

§

fn serialize_value<T>(&mut self, _value: &T) -> Result<(), Asn1DerError>
where T: Serialize + ?Sized,

§

fn end( self, ) -> Result<<&'a mut Serializer<'se> as SerializeMap>::Ok, Asn1DerError>

§

impl<'a, 'se> SerializeMap for &'a mut Serializer<'se>

§

type Ok = usize

§

type Error = Asn1DerError

§

fn serialize_key<T>(&mut self, _key: &T) -> Result<(), Asn1DerError>
where T: Serialize + ?Sized,

§

fn serialize_value<T>(&mut self, _value: &T) -> Result<(), Asn1DerError>
where T: Serialize + ?Sized,

§

fn end( self, ) -> Result<<&'a mut Serializer<'se> as SerializeMap>::Ok, Asn1DerError>

§

impl<'input, 'output, Target> SerializeMap for MapSerializer<'input, 'output, Target>
where Target: 'output + Target,

§

type Ok = &'output mut Serializer<'input, Target>

§

type Error = Error

§

fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Error>
where K: Serialize + ?Sized, V: Serialize + ?Sized,

§

fn serialize_key<T>(&mut self, key: &T) -> Result<(), Error>
where T: Serialize + ?Sized,

§

fn serialize_value<T>(&mut self, value: &T) -> Result<(), Error>
where T: Serialize + ?Sized,

§

fn end( self, ) -> Result<<MapSerializer<'input, 'output, Target> as SerializeMap>::Ok, Error>

Implementors§

source§

impl<Ok, Error> SerializeMap for Impossible<Ok, Error>
where Error: Error,

source§

type Ok = Ok

source§

type Error = Error