BiHashMapAsMap

Struct BiHashMapAsMap 

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

Marker type for BiHashMap 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::{
    BiHashItem, BiHashMap, bi_hash_map::BiHashMapAsMap, bi_upcast,
};
use serde::{Deserialize, Serialize};

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

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

#[derive(Serialize, Deserialize)]
struct Config {
    #[serde(with = "BiHashMapAsMap")]
    items: BiHashMap<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> BiHashMapAsMap<T, S, A>
where S: Clone + BuildHasher, A: Allocator,

Source

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

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

Source

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

Deserializes a BiHashMap from a JSON object/map.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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