serde_tagged/util/ser/
forward.rs

1//! Types that can be used to indirectly call a function of a serializer using
2//! `Serialize`.
3//!
4//! These types are required to assure that the right serializer function is
5//! being called (e.g. in case of `serialize_bytes`) and can also be used to
6//! forward a serializer function-call to another serializer.
7
8use std::cell::Cell;
9use std::fmt::Display;
10
11use serde;
12
13
14/// A type that serializes the enclosed `u8` slice using `serialize_bytes`.
15pub struct Bytes<'a>(pub &'a [u8]);
16
17impl<'a> serde::Serialize for Bytes<'a> {
18    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19    where
20        S: serde::Serializer,
21    {
22        serializer.serialize_bytes(self.0)
23    }
24}
25
26
27/// A type that serializes using `serialize_none`.
28pub struct None;
29
30impl serde::Serialize for None {
31    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
32    where
33        S: serde::Serializer,
34    {
35        serializer.serialize_none()
36    }
37}
38
39
40/// A type that serializes the enclosed value using `serialize_some`.
41pub struct Some<'a, V: ?Sized + 'a>(pub &'a V);
42
43impl<'a, V: serde::Serialize + ?Sized + 'a> serde::Serialize for Some<'a, V> {
44    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
45    where
46        S: serde::Serializer,
47    {
48        serializer.serialize_some(self.0)
49    }
50}
51
52
53/// A type that serializes using `serialize_unit`.
54pub struct Unit;
55
56impl serde::Serialize for Unit {
57    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
58    where
59        S: serde::Serializer,
60    {
61        serializer.serialize_unit()
62    }
63}
64
65
66/// A type that serializes using `serialize_unit_struct`.
67pub struct UnitStruct(pub &'static str);
68
69impl serde::Serialize for UnitStruct {
70    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
71    where
72        S: serde::Serializer,
73    {
74        serializer.serialize_unit_struct(self.0)
75    }
76}
77
78
79/// A type that serializes using `serialize_unit_variant`.
80pub struct UnitVariant(pub &'static str, pub u32, pub &'static str);
81
82impl serde::Serialize for UnitVariant {
83    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
84    where
85        S: serde::Serializer,
86    {
87        serializer.serialize_unit_variant(self.0, self.1, self.2)
88    }
89}
90
91
92/// A type that serializes using `serialize_newtype_struct`.
93pub struct NewtypeStruct<'a, V: ?Sized + 'a>(pub &'static str, pub &'a V);
94
95impl<'a, V: serde::Serialize + ?Sized + 'a> serde::Serialize for NewtypeStruct<'a, V> {
96    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
97    where
98        S: serde::Serializer,
99    {
100        serializer.serialize_newtype_struct(self.0, self.1)
101    }
102}
103
104
105/// A type that serializes using `serialize_newtype_variant`.
106pub struct NewtypeVariant<'a, V: ?Sized + 'a>(
107    pub &'static str,
108    pub u32,
109    pub &'static str,
110    pub &'a V,
111);
112
113impl<'a, V: serde::Serialize + ?Sized + 'a> serde::Serialize for NewtypeVariant<'a, V> {
114    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
115    where
116        S: serde::Serializer,
117    {
118        serializer.serialize_newtype_variant(self.0, self.1, self.2, self.3)
119    }
120}
121
122
123/// A type that serializes using `collect_seq`.
124///
125/// Forwards an owned sequence collected during serialization.
126///
127/// This struct will take ownership of the sequence and pass it on to the
128/// serializer in the first call to serialize. Thus calling serialize on an
129/// object of this type more than once is illegal and will result in a panic.
130pub struct CollectSeq<I>(Cell<Option<I>>);
131
132impl<I> CollectSeq<I> {
133    pub fn new(iter: I) -> Self {
134        CollectSeq(Cell::new(Option::Some(iter)))
135    }
136}
137
138impl<I> serde::Serialize for CollectSeq<I>
139where
140    I: IntoIterator,
141    <I as IntoIterator>::Item: serde::Serialize,
142{
143    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
144    where
145        S: serde::Serializer,
146    {
147        serializer.collect_seq(self.0.take().unwrap())
148    }
149}
150
151
152/// A type that serializes using `collect_map`.
153///
154/// Forwards an owned key-value sequence collected during serialization.
155///
156/// This struct will take ownership of the sequence and pass it on to the
157/// serializer in the first call to serialize. Thus calling serialize on an
158/// object of this type more than once is illegal and will result in a panic.
159pub struct CollectMap<I>(Cell<Option<I>>);
160
161impl<I> CollectMap<I> {
162    pub fn new(iter: I) -> Self {
163        CollectMap(Cell::new(Option::Some(iter)))
164    }
165}
166
167impl<I, K, V> serde::Serialize for CollectMap<I>
168where
169    K: serde::Serialize,
170    V: serde::Serialize,
171    I: IntoIterator<Item = (K, V)>,
172{
173    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
174    where
175        S: serde::Serializer,
176    {
177        serializer.collect_map(self.0.take().unwrap())
178    }
179}
180
181
182/// A type that serializes using `collect_str`.
183pub struct CollectStr<'a, D: ?Sized + 'a>(pub &'a D);
184
185impl<'a, D: ?Sized> serde::Serialize for CollectStr<'a, D>
186where
187    D: Display,
188{
189    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
190    where
191        S: serde::Serializer,
192    {
193        serializer.collect_str(self.0)
194    }
195}