TriHashMapAsMap

Struct TriHashMapAsMap 

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

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

The key type Self::K1 is used as the map key.

§Examples

Use with serde’s with attribute:

use iddqd::{
    TriHashItem, TriHashMap, tri_hash_map::TriHashMapAsMap, tri_upcast,
};
use serde::{Deserialize, Serialize};

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

impl TriHashItem for Item {
    type K1<'a> = u32;
    type K2<'a> = &'a str;
    type K3<'a> = &'a str;
    fn key1(&self) -> Self::K1<'_> {
        self.id
    }
    fn key2(&self) -> Self::K2<'_> {
        &self.name
    }
    fn key3(&self) -> Self::K3<'_> {
        &self.email
    }
    tri_upcast!();
}

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

§Requirements

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

Implementations§

Source§

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

Source

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

Serializes a TriHashMap as a JSON object/map using key1() as keys.

Source

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

Deserializes a TriHashMap from a JSON object/map.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, S, A> UnwindSafe for TriHashMapAsMap<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.