serde_yaml_ng::with::singleton_map

Struct SingletonMap

Source
pub struct SingletonMap<D> { /* private fields */ }
Expand description
This is an new API which is not present in the original serde-yaml. This API is not garanteed to be present in serde-yaml-ng version 1.0. However, this API won't be removed without an acceptable alternative solution for the problem it is trying to solve.

Can be used for serialing/deserializing an enum using a YAML map containing one entry in which the key identifies the variant name.

Unlike #[serde(with = "singleton_map") this struct can be used from within a custom Serialize implementation or from within another with module.

§Example

use std::sync::Mutex;
use serde_derive::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use serde_yaml_ng::with::singleton_map::SingletonMap;

#[derive(Serialize, Deserialize)]
pub enum Bar {
    A(u32),
    B(f32)
}
#[derive(Serialize, Deserialize)]
pub struct Foo {
    #[serde(with = "bar_serializer")]
    // Because it has a Mutex, we can't simply derive Serialize/Deserialize here, nor can
    // we use `#[serde(with = "serde_yaml_ng::with::singleton_map")]`.  We need a custom
    // `with` module to deal with the Mutex.
    bar: Mutex<Bar>
}

mod bar_serializer {
    use serde::{de::Deserializer, Serializer};
    use super::*;

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Mutex<Bar>, D::Error>
        where D: Deserializer<'de>
    {
        Bar::deserialize(SingletonMap::new(deserializer))
        .map(Mutex::new)
    }
    pub fn serialize<S>(bar: &Mutex<Bar>, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer
    {
        let guard = bar.try_lock().unwrap();
        (*guard).serialize(SingletonMap::new(serializer))
    }
}

Implementations§

Source§

impl<D> SingletonMap<D>

Source

pub fn new(delegate: D) -> Self

Create a new SingletonMap, wrapping either a Serializer or a Deserializer.

Trait Implementations§

Source§

impl<'de, D> Deserializer<'de> for SingletonMap<D>
where D: Deserializer<'de>,

Source§

type Error = <D as Deserializer<'de>>::Error

The error type that can be returned if some error occurs during deserialization.
Source§

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Require the Deserializer to figure out how to drive the visitor based on what data type is in the input. Read more
Source§

fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a bool value.
Source§

fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i8 value.
Source§

fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i16 value.
Source§

fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i32 value.
Source§

fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i64 value.
Source§

fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i128 value. Read more
Source§

fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u8 value.
Source§

fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u16 value.
Source§

fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u32 value.
Source§

fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u64 value.
Source§

fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an u128 value. Read more
Source§

fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a f32 value.
Source§

fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a f64 value.
Source§

fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a char value.
Source§

fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a string value and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a string value and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a byte array and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a byte array and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an optional value. Read more
Source§

fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a unit value.
Source§

fn deserialize_unit_struct<V>( self, name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a unit struct with a particular name.
Source§

fn deserialize_newtype_struct<V>( self, name: &'static str, visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a newtype struct with a particular name.
Source§

fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a sequence of values.
Source§

fn deserialize_tuple<V>( self, len: usize, visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a sequence of values and knows how many values there are without looking at the serialized data.
Source§

fn deserialize_tuple_struct<V>( self, name: &'static str, len: usize, visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a tuple struct with a particular name and number of fields.
Source§

fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a map of key-value pairs.
Source§

fn deserialize_struct<V>( self, name: &'static str, fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a struct with a particular name and fields.
Source§

fn deserialize_enum<V>( self, name: &'static str, _variants: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an enum value with a particular name and possible variants.
Source§

fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting the name of a struct field or the discriminant of an enum variant.
Source§

fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where V: Visitor<'de>,

Hint that the Deserialize type needs to deserialize a value whose type doesn’t matter because it is ignored. Read more
Source§

fn is_human_readable(&self) -> bool

Determine whether Deserialize implementations should expect to deserialize their human-readable form. Read more
Source§

impl<D> Serialize for SingletonMap<D>
where D: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<D> Serializer for SingletonMap<D>
where D: Serializer,

Source§

type Ok = <D as Serializer>::Ok

The output type produced by this Serializer during successful serialization. Most serializers that produce text or binary output should set Ok = () and serialize into an io::Write or buffer contained within the Serializer instance. Serializers that build in-memory data structures may be simplified by using Ok to propagate the data structure around.
Source§

type Error = <D as Serializer>::Error

The error type when some error occurs during serialization.
Source§

type SerializeSeq = <D as Serializer>::SerializeSeq

Type returned from serialize_seq for serializing the content of the sequence.
Source§

type SerializeTuple = <D as Serializer>::SerializeTuple

Type returned from serialize_tuple for serializing the content of the tuple.
Source§

type SerializeTupleStruct = <D as Serializer>::SerializeTupleStruct

Type returned from serialize_tuple_struct for serializing the content of the tuple struct.
Source§

type SerializeTupleVariant = SerializeTupleVariantAsSingletonMap<<D as Serializer>::SerializeMap>

Type returned from serialize_tuple_variant for serializing the content of the tuple variant.
Source§

type SerializeMap = <D as Serializer>::SerializeMap

Type returned from serialize_map for serializing the content of the map.
Source§

type SerializeStruct = <D as Serializer>::SerializeStruct

Type returned from serialize_struct for serializing the content of the struct.
Source§

type SerializeStructVariant = SerializeStructVariantAsSingletonMap<<D as Serializer>::SerializeMap>

Type returned from serialize_struct_variant for serializing the content of the struct variant.
Source§

fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error>

Serialize a bool value. Read more
Source§

fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error>

Serialize an i8 value. Read more
Source§

fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error>

Serialize an i16 value. Read more
Source§

fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error>

Serialize an i32 value. Read more
Source§

fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error>

Serialize an i64 value. Read more
Source§

fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error>

Serialize an i128 value. Read more
Source§

fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error>

Serialize a u8 value. Read more
Source§

fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error>

Serialize a u16 value. Read more
Source§

fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error>

Serialize a u32 value. Read more
Source§

fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error>

Serialize a u64 value. Read more
Source§

fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error>

Serialize a u128 value. Read more
Source§

fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error>

Serialize an f32 value. Read more
Source§

fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error>

Serialize an f64 value. Read more
Source§

fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error>

Serialize a character. Read more
Source§

fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error>

Serialize a &str. Read more
Source§

fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error>

Serialize a chunk of raw byte data. Read more
Source§

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

Serialize a () value. Read more
Source§

fn serialize_unit_struct( self, name: &'static str, ) -> Result<Self::Ok, Self::Error>

Serialize a unit struct like struct Unit or PhantomData<T>. Read more
Source§

fn serialize_unit_variant( self, name: &'static str, variant_index: u32, variant: &'static str, ) -> Result<Self::Ok, Self::Error>

Serialize a unit variant like E::A in enum E { A, B }. Read more
Source§

fn serialize_newtype_struct<T>( self, name: &'static str, value: &T, ) -> Result<Self::Ok, Self::Error>
where T: ?Sized + Serialize,

Serialize a newtype struct like struct Millimeters(u8). Read more
Source§

fn serialize_newtype_variant<T>( self, _name: &'static str, _variant_index: u32, variant: &'static str, value: &T, ) -> Result<Self::Ok, Self::Error>
where T: ?Sized + Serialize,

Serialize a newtype variant like E::N in enum E { N(u8) }. Read more
Source§

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

Serialize a None value. Read more
Source§

fn serialize_some<V>(self, value: &V) -> Result<Self::Ok, Self::Error>
where V: ?Sized + Serialize,

Serialize a Some(T) value. Read more
Source§

fn serialize_seq( self, len: Option<usize>, ) -> Result<Self::SerializeSeq, Self::Error>

Begin to serialize a variably sized sequence. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Source§

fn serialize_tuple( self, len: usize, ) -> Result<Self::SerializeTuple, Self::Error>

Begin to serialize a statically sized sequence whose length will be known at deserialization time without looking at the serialized data. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
Source§

fn serialize_tuple_struct( self, name: &'static str, len: usize, ) -> Result<Self::SerializeTupleStruct, Self::Error>

Begin to serialize a tuple struct like struct Rgb(u8, u8, u8). This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Source§

fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize, ) -> Result<Self::SerializeTupleVariant, Self::Error>

Begin to serialize a tuple variant like E::T in enum E { T(u8, u8) }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Source§

fn serialize_map( self, len: Option<usize>, ) -> Result<Self::SerializeMap, Self::Error>

Begin to serialize a map. This call must be followed by zero or more calls to serialize_key and serialize_value, then a call to end. Read more
Source§

fn serialize_struct( self, name: &'static str, len: usize, ) -> Result<Self::SerializeStruct, Self::Error>

Begin to serialize a struct like struct Rgb { r: u8, g: u8, b: u8 }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Source§

fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, variant: &'static str, len: usize, ) -> Result<Self::SerializeStructVariant, Self::Error>

Begin to serialize a struct variant like E::S in enum E { S { r: u8, g: u8, b: u8 } }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
Source§

fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
where T: ?Sized + Display,

Serialize a string produced by an implementation of Display. Read more
Source§

fn is_human_readable(&self) -> bool

Determine whether Serialize implementations should serialize in human-readable form. Read more
Source§

fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>

Collect an iterator as a sequence. Read more
Source§

fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
where K: Serialize, V: Serialize, I: IntoIterator<Item = (K, V)>,

Collect an iterator as a map. Read more

Auto Trait Implementations§

§

impl<D> Freeze for SingletonMap<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for SingletonMap<D>
where D: RefUnwindSafe,

§

impl<D> Send for SingletonMap<D>
where D: Send,

§

impl<D> Sync for SingletonMap<D>
where D: Sync,

§

impl<D> Unpin for SingletonMap<D>
where D: Unpin,

§

impl<D> UnwindSafe for SingletonMap<D>
where D: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.