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