Skip to main content

serde_with/ser/
duplicates.rs

1use super::impls::macros::{foreach_map, foreach_set};
2use crate::prelude::*;
3#[cfg(feature = "hashbrown_0_14")]
4use hashbrown_0_14::{HashMap as HashbrownMap014, HashSet as HashbrownSet014};
5#[cfg(feature = "hashbrown_0_15")]
6use hashbrown_0_15::{HashMap as HashbrownMap015, HashSet as HashbrownSet015};
7#[cfg(feature = "hashbrown_0_16")]
8use hashbrown_0_16::{HashMap as HashbrownMap016, HashSet as HashbrownSet016};
9#[cfg(feature = "hashbrown_0_17")]
10use hashbrown_0_17::{HashMap as HashbrownMap017, HashSet as HashbrownSet017};
11#[cfg(feature = "indexmap_1")]
12use indexmap_1::{IndexMap, IndexSet};
13#[cfg(feature = "indexmap_2")]
14use indexmap_2::{IndexMap as IndexMap2, IndexSet as IndexSet2};
15
16macro_rules! set_duplicate_handling {
17    ($tyorig:ident < T $(, $typaram:ident : $bound:ident)* >) => {
18        impl<T, TAs $(, $typaram)*> SerializeAs<$tyorig<T $(, $typaram)*>> for SetPreventDuplicates<TAs>
19        where
20            TAs: SerializeAs<T>,
21            $($typaram: ?Sized + $bound,)*
22        {
23            fn serialize_as<S>(value: &$tyorig<T $(, $typaram)*>, serializer: S) -> Result<S::Ok, S::Error>
24            where
25                S: Serializer,
26            {
27                <$tyorig<TAs $(, $typaram)*>>::serialize_as(value, serializer)
28            }
29        }
30
31        impl<T, TAs $(, $typaram)*> SerializeAs<$tyorig<T $(, $typaram)*>> for SetLastValueWins<TAs>
32        where
33            TAs: SerializeAs<T>,
34            $($typaram: ?Sized + $bound,)*
35        {
36            fn serialize_as<S>(value: &$tyorig<T $(, $typaram)*>, serializer: S) -> Result<S::Ok, S::Error>
37            where
38                S: Serializer,
39            {
40                <$tyorig<TAs $(, $typaram)*>>::serialize_as(value, serializer)
41            }
42        }
43    }
44}
45foreach_set!(set_duplicate_handling);
46
47macro_rules! map_duplicate_handling {
48    ($tyorig:ident < K, V $(, $typaram:ident : $bound:ident)* >) => {
49        impl<K, KAs, V, VAs $(, $typaram)*> SerializeAs<$tyorig<K, V $(, $typaram)*>> for MapPreventDuplicates<KAs, VAs>
50        where
51            KAs: SerializeAs<K>,
52            VAs: SerializeAs<V>,
53            $($typaram: ?Sized + $bound,)*
54        {
55            fn serialize_as<S>(value: &$tyorig<K, V $(, $typaram)*>, serializer: S) -> Result<S::Ok, S::Error>
56            where
57                S: Serializer,
58            {
59                <$tyorig<KAs, VAs $(, $typaram)*>>::serialize_as(value, serializer)
60            }
61        }
62
63        impl<K, KAs, V, VAs $(, $typaram)*> SerializeAs<$tyorig<K, V $(, $typaram)*>> for MapFirstKeyWins<KAs, VAs>
64        where
65            KAs: SerializeAs<K>,
66            VAs: SerializeAs<V>,
67            $($typaram: ?Sized + $bound,)*
68        {
69            fn serialize_as<S>(value: &$tyorig<K, V $(, $typaram)*>, serializer: S) -> Result<S::Ok, S::Error>
70            where
71                S: Serializer,
72            {
73                <$tyorig<KAs, VAs $(, $typaram)*>>::serialize_as(value, serializer)
74            }
75        }
76    }
77}
78foreach_map!(map_duplicate_handling);