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