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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Types that can be used to indirectly call a function of a serializer using
//! `Serialize`.
//!
//! These types are required to assure that the right serializer function is
//! being called (e.g. in case of `serialize_bytes`) and can also be used to
//! forward a serializer function-call to another serializer.

use std::cell::Cell;
use std::fmt::Display;

use serde;


/// A type that serializes the enclosed `u8` slice using `serialize_bytes`.
pub struct Bytes<'a>(pub &'a [u8]);

impl<'a> serde::Serialize for Bytes<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_bytes(self.0)
    }
}


/// A type that serializes using `serialize_none`.
pub struct None;

impl serde::Serialize for None {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_none()
    }
}


/// A type that serializes the enclosed value using `serialize_some`.
pub struct Some<'a, V: ?Sized + 'a>(pub &'a V);

impl<'a, V: serde::Serialize + ?Sized + 'a> serde::Serialize for Some<'a, V> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_some(self.0)
    }
}


/// A type that serializes using `serialize_unit`.
pub struct Unit;

impl serde::Serialize for Unit {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_unit()
    }
}


/// A type that serializes using `serialize_unit_struct`.
pub struct UnitStruct(pub &'static str);

impl serde::Serialize for UnitStruct {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_unit_struct(self.0)
    }
}


/// A type that serializes using `serialize_unit_variant`.
pub struct UnitVariant(pub &'static str, pub u32, pub &'static str);

impl serde::Serialize for UnitVariant {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_unit_variant(self.0, self.1, self.2)
    }
}


/// A type that serializes using `serialize_newtype_struct`.
pub struct NewtypeStruct<'a, V: ?Sized + 'a>(pub &'static str, pub &'a V);

impl<'a, V: serde::Serialize + ?Sized + 'a> serde::Serialize for NewtypeStruct<'a, V> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_newtype_struct(self.0, self.1)
    }
}


/// A type that serializes using `serialize_newtype_variant`.
pub struct NewtypeVariant<'a, V: ?Sized + 'a>(
    pub &'static str,
    pub u32,
    pub &'static str,
    pub &'a V,
);

impl<'a, V: serde::Serialize + ?Sized + 'a> serde::Serialize for NewtypeVariant<'a, V> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_newtype_variant(self.0, self.1, self.2, self.3)
    }
}


/// A type that serializes using `collect_seq`.
///
/// Forwards an owned sequence collected during serialization.
///
/// This struct will take ownership of the sequence and pass it on to the
/// serializer in the first call to serialize. Thus calling serialize on an
/// object of this type more than once is illegal and will result in a panic.
pub struct CollectSeq<I>(Cell<Option<I>>);

impl<I> CollectSeq<I> {
    pub fn new(iter: I) -> Self {
        CollectSeq(Cell::new(Option::Some(iter)))
    }
}

impl<I> serde::Serialize for CollectSeq<I>
where
    I: IntoIterator,
    <I as IntoIterator>::Item: serde::Serialize,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.collect_seq(self.0.take().unwrap())
    }
}


/// A type that serializes using `collect_map`.
///
/// Forwards an owned key-value sequence collected during serialization.
///
/// This struct will take ownership of the sequence and pass it on to the
/// serializer in the first call to serialize. Thus calling serialize on an
/// object of this type more than once is illegal and will result in a panic.
pub struct CollectMap<I>(Cell<Option<I>>);

impl<I> CollectMap<I> {
    pub fn new(iter: I) -> Self {
        CollectMap(Cell::new(Option::Some(iter)))
    }
}

impl<I, K, V> serde::Serialize for CollectMap<I>
where
    K: serde::Serialize,
    V: serde::Serialize,
    I: IntoIterator<Item = (K, V)>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.collect_map(self.0.take().unwrap())
    }
}


/// A type that serializes using `collect_str`.
pub struct CollectStr<'a, D: ?Sized + 'a>(pub &'a D);

impl<'a, D: ?Sized> serde::Serialize for CollectStr<'a, D>
where
    D: Display,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.collect_str(self.0)
    }
}