1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::borrow::Cow;
use super::*;
use std::collections::{BTreeMap, HashMap};
impl<T, As, const N: usize> SerializeAs<[T; N]> for [As; N]
where
As: SerializeAs<T>,
{
fn serialize_as<S>(array: &[T; N], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde::ser::SerializeTuple;
let mut arr = serializer.serialize_tuple(N)?;
for elem in array {
arr.serialize_element(&SerializeAsWrap::<T, As>::new(elem))?;
}
arr.end()
}
}
macro_rules! tuple_seq_as_map_impl_intern {
($tyorig:ty, $ty:ident <K, V>) => {
#[allow(clippy::implicit_hasher)]
impl<K, KAs, V, VAs, const N: usize> SerializeAs<$tyorig> for $ty<KAs, VAs>
where
KAs: SerializeAs<K>,
VAs: SerializeAs<V>,
{
fn serialize_as<S>(source: &$tyorig, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_map(source.iter().map(|(k, v)| {
(
SerializeAsWrap::<K, KAs>::new(k),
SerializeAsWrap::<V, VAs>::new(v),
)
}))
}
}
};
}
tuple_seq_as_map_impl_intern!([(K, V); N], BTreeMap<K, V>);
tuple_seq_as_map_impl_intern!([(K, V); N], HashMap<K, V>);
impl<'a, const N: usize> SerializeAs<[u8; N]> for Bytes {
fn serialize_as<S>(bytes: &[u8; N], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(bytes)
}
}
impl<'a, const N: usize> SerializeAs<&[u8; N]> for Bytes {
fn serialize_as<S>(bytes: &&[u8; N], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(*bytes)
}
}
impl<'a, const N: usize> SerializeAs<Box<[u8; N]>> for Bytes {
fn serialize_as<S>(bytes: &Box<[u8; N]>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(&**bytes)
}
}
impl<'a, const N: usize> SerializeAs<Cow<'a, [u8; N]>> for Bytes {
fn serialize_as<S>(bytes: &Cow<'a, [u8; N]>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(bytes.as_ref())
}
}
impl<'a, const N: usize> SerializeAs<Cow<'a, [u8; N]>> for BorrowCow {
fn serialize_as<S>(value: &Cow<'a, [u8; N]>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_seq(value.iter())
}
}