IdHashMapAsMap

Struct IdHashMapAsMap 

Source
pub struct IdHashMapAsMap<T, S = DefaultHashBuilder, A: Allocator = Global> { /* private fields */ }
Expand description

Marker type for IdHashMap serialized as a map, for use with serde’s with attribute.

§Examples

Use with serde’s with attribute:

use iddqd::{
    IdHashItem, IdHashMap, id_hash_map::IdHashMapAsMap, id_upcast,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Item {
    id: u32,
    name: String,
}

impl IdHashItem for Item {
    type Key<'a> = u32;
    fn key(&self) -> Self::Key<'_> {
        self.id
    }
    id_upcast!();
}

#[derive(Serialize, Deserialize)]
struct Config {
    #[serde(with = "IdHashMapAsMap")]
    items: IdHashMap<Item>,
}

§Requirements

  • For serialization, the key type must implement Serialize.
  • For JSON serialization, the key should be string-like or convertible to a string key.

Implementations§

Source§

impl<T, S, A> IdHashMapAsMap<T, S, A>
where S: Clone + BuildHasher, A: Allocator,

Source

pub fn serialize<'a, Ser>( map: &IdHashMap<T, S, A>, serializer: Ser, ) -> Result<Ser::Ok, Ser::Error>
where T: 'a + IdHashItem + Serialize, T::Key<'a>: Serialize, Ser: Serializer,

Serializes an IdHashMap as a JSON object/map using key() as keys.

Source

pub fn deserialize<'de, D>( deserializer: D, ) -> Result<IdHashMap<T, S, A>, D::Error>
where T: IdHashItem + Deserialize<'de> + Debug, S: Default, A: Clone + Default, D: Deserializer<'de>,

Deserializes an IdHashMap from a JSON object/map.

Auto Trait Implementations§

§

impl<T, S, A> Freeze for IdHashMapAsMap<T, S, A>

§

impl<T, S, A> RefUnwindSafe for IdHashMapAsMap<T, S, A>

§

impl<T, S, A> Send for IdHashMapAsMap<T, S, A>

§

impl<T, S, A> Sync for IdHashMapAsMap<T, S, A>

§

impl<T, S, A> Unpin for IdHashMapAsMap<T, S, A>

§

impl<T, S, A> UnwindSafe for IdHashMapAsMap<T, S, A>

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.