1use core::fmt;
2
3use crate::en::{
4 Encode, Encoder, EntriesEncoder, EntryEncoder, MapEncoder, SequenceEncoder, VariantEncoder,
5};
6use crate::hint::{MapHint, SequenceHint};
7use crate::{Context, Options, Writer};
8
9pub struct StorageEncoder<'a, W, const OPT: Options, C: ?Sized> {
11 cx: &'a C,
12 writer: W,
13}
14
15impl<'a, W, const OPT: Options, C: ?Sized> StorageEncoder<'a, W, OPT, C> {
16 #[inline]
18 pub fn new(cx: &'a C, writer: W) -> Self {
19 Self { cx, writer }
20 }
21}
22
23#[crate::encoder(crate)]
24impl<'a, W, const OPT: Options, C> Encoder for StorageEncoder<'a, W, OPT, C>
25where
26 C: ?Sized + Context,
27 W: Writer,
28{
29 type Cx = C;
30 type Error = C::Error;
31 type Ok = ();
32 type Mode = C::Mode;
33 type WithContext<'this, U> = StorageEncoder<'this, W, OPT, U> where U: 'this + Context;
34 type EncodePack = StorageEncoder<'a, W, OPT, C>;
35 type EncodeSome = Self;
36 type EncodeSequence = Self;
37 type EncodeMap = Self;
38 type EncodeMapEntries = Self;
39 type EncodeVariant = Self;
40 type EncodeSequenceVariant = Self;
41 type EncodeMapVariant = Self;
42
43 #[inline]
44 fn cx(&self) -> &Self::Cx {
45 self.cx
46 }
47
48 #[inline]
49 fn with_context<U>(self, cx: &U) -> Result<Self::WithContext<'_, U>, C::Error>
50 where
51 U: Context,
52 {
53 Ok(StorageEncoder::new(cx, self.writer))
54 }
55
56 #[inline]
57 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 write!(f, "type supported by the storage encoder")
59 }
60
61 #[inline]
62 fn encode<T>(self, value: T) -> Result<Self::Ok, Self::Error>
63 where
64 T: Encode<Self::Mode>,
65 {
66 value.encode(self.cx, self)
67 }
68
69 #[inline]
70 fn encode_empty(self) -> Result<Self::Ok, C::Error> {
71 static HINT: SequenceHint = SequenceHint::with_size(0);
72 self.encode_sequence_fn(&HINT, |_| Ok(()))
73 }
74
75 #[inline]
76 fn encode_pack(self) -> Result<Self::EncodePack, C::Error> {
77 Ok(self)
78 }
79
80 #[inline]
81 fn encode_array<const N: usize>(mut self, array: &[u8; N]) -> Result<Self::Ok, C::Error> {
82 self.writer.write_bytes(self.cx, array)
83 }
84
85 #[inline]
86 fn encode_bytes(mut self, bytes: &[u8]) -> Result<Self::Ok, C::Error> {
87 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), bytes.len())?;
88 self.writer.write_bytes(self.cx, bytes)?;
89 Ok(())
90 }
91
92 #[inline]
93 fn encode_bytes_vectored<I>(mut self, len: usize, vectors: I) -> Result<Self::Ok, C::Error>
94 where
95 I: IntoIterator<Item: AsRef<[u8]>>,
96 {
97 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), len)?;
98
99 for bytes in vectors {
100 self.writer.write_bytes(self.cx, bytes.as_ref())?;
101 }
102
103 Ok(())
104 }
105
106 #[inline]
107 fn encode_string(mut self, string: &str) -> Result<Self::Ok, C::Error> {
108 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), string.len())?;
109 self.writer.write_bytes(self.cx, string.as_bytes())?;
110 Ok(())
111 }
112
113 #[inline]
114 fn collect_string<T>(self, value: &T) -> Result<Self::Ok, <Self::Cx as Context>::Error>
115 where
116 T: ?Sized + fmt::Display,
117 {
118 let buf = self.cx.collect_string(value)?;
119 self.encode_string(buf.as_ref())
120 }
121
122 #[inline]
123 fn encode_usize(mut self, value: usize) -> Result<Self::Ok, C::Error> {
124 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), value)
125 }
126
127 #[inline]
128 fn encode_isize(self, value: isize) -> Result<Self::Ok, C::Error> {
129 self.encode_usize(value as usize)
130 }
131
132 #[inline]
133 fn encode_bool(mut self, value: bool) -> Result<Self::Ok, C::Error> {
134 self.writer.write_byte(self.cx, if value { 1 } else { 0 })
135 }
136
137 #[inline]
138 fn encode_char(self, value: char) -> Result<Self::Ok, C::Error> {
139 self.encode_u32(value as u32)
140 }
141
142 #[inline]
143 fn encode_u8(mut self, value: u8) -> Result<Self::Ok, C::Error> {
144 self.writer.write_byte(self.cx, value)
145 }
146
147 #[inline]
148 fn encode_u16(mut self, value: u16) -> Result<Self::Ok, C::Error> {
149 crate::int::encode_unsigned::<_, _, _, OPT>(self.cx, self.writer.borrow_mut(), value)
150 }
151
152 #[inline]
153 fn encode_u32(mut self, value: u32) -> Result<Self::Ok, C::Error> {
154 crate::int::encode_unsigned::<_, _, _, OPT>(self.cx, self.writer.borrow_mut(), value)
155 }
156
157 #[inline]
158 fn encode_u64(mut self, value: u64) -> Result<Self::Ok, C::Error> {
159 crate::int::encode_unsigned::<_, _, _, OPT>(self.cx, self.writer.borrow_mut(), value)
160 }
161
162 #[inline]
163 fn encode_u128(mut self, value: u128) -> Result<Self::Ok, C::Error> {
164 crate::int::encode_unsigned::<_, _, _, OPT>(self.cx, self.writer.borrow_mut(), value)
165 }
166
167 #[inline]
168 fn encode_i8(self, value: i8) -> Result<Self::Ok, C::Error> {
169 self.encode_u8(value as u8)
170 }
171
172 #[inline]
173 fn encode_i16(mut self, value: i16) -> Result<Self::Ok, C::Error> {
174 crate::int::encode_signed::<_, _, _, OPT>(self.cx, self.writer.borrow_mut(), value)
175 }
176
177 #[inline]
178 fn encode_i32(mut self, value: i32) -> Result<Self::Ok, C::Error> {
179 crate::int::encode_signed::<_, _, _, OPT>(self.cx, self.writer.borrow_mut(), value)
180 }
181
182 #[inline]
183 fn encode_i64(mut self, value: i64) -> Result<Self::Ok, C::Error> {
184 crate::int::encode_signed::<_, _, _, OPT>(self.cx, self.writer.borrow_mut(), value)
185 }
186
187 #[inline]
188 fn encode_i128(mut self, value: i128) -> Result<Self::Ok, C::Error> {
189 crate::int::encode_signed::<_, _, _, OPT>(self.cx, self.writer.borrow_mut(), value)
190 }
191
192 #[inline]
193 fn encode_f32(self, value: f32) -> Result<Self::Ok, C::Error> {
194 self.encode_u32(value.to_bits())
195 }
196
197 #[inline]
198 fn encode_f64(self, value: f64) -> Result<Self::Ok, C::Error> {
199 self.encode_u64(value.to_bits())
200 }
201
202 #[inline]
203 fn encode_some(mut self) -> Result<Self::EncodeSome, C::Error> {
204 self.writer.write_byte(self.cx, 1)?;
205 Ok(self)
206 }
207
208 #[inline]
209 fn encode_none(mut self) -> Result<Self::Ok, C::Error> {
210 self.writer.write_byte(self.cx, 0)?;
211 Ok(())
212 }
213
214 #[inline]
215 fn encode_sequence(mut self, hint: &SequenceHint) -> Result<Self::EncodeSequence, C::Error> {
216 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), hint.size)?;
217 Ok(self)
218 }
219
220 #[inline]
221 fn encode_map(mut self, hint: &MapHint) -> Result<Self::EncodeMap, C::Error> {
222 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), hint.size)?;
223 Ok(self)
224 }
225
226 #[inline]
227 fn encode_map_entries(mut self, hint: &MapHint) -> Result<Self::EncodeMapEntries, C::Error> {
228 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), hint.size)?;
229 Ok(self)
230 }
231
232 #[inline]
233 fn encode_variant(self) -> Result<Self::EncodeVariant, C::Error> {
234 Ok(self)
235 }
236
237 #[inline]
238 fn encode_sequence_variant<T>(
239 mut self,
240 tag: &T,
241 hint: &SequenceHint,
242 ) -> Result<Self::EncodeSequenceVariant, C::Error>
243 where
244 T: ?Sized + Encode<C::Mode>,
245 {
246 StorageEncoder::<_, OPT, _>::new(self.cx, self.writer.borrow_mut()).encode(tag)?;
247 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), hint.size)?;
248 Ok(self)
249 }
250
251 #[inline]
252 fn encode_map_variant<T>(
253 mut self,
254 tag: &T,
255 hint: &MapHint,
256 ) -> Result<Self::EncodeMapVariant, C::Error>
257 where
258 T: ?Sized + Encode<C::Mode>,
259 {
260 StorageEncoder::<_, OPT, _>::new(self.cx, self.writer.borrow_mut()).encode(tag)?;
261 crate::int::encode_usize::<_, _, OPT>(self.cx, self.writer.borrow_mut(), hint.size)?;
262 Ok(self)
263 }
264}
265
266impl<'a, W, const OPT: Options, C> SequenceEncoder for StorageEncoder<'a, W, OPT, C>
267where
268 C: ?Sized + Context,
269 W: Writer,
270{
271 type Cx = C;
272 type Ok = ();
273 type EncodeNext<'this> = StorageEncoder<'a, W::Mut<'this>, OPT, C> where Self: 'this;
274
275 #[inline]
276 fn encode_next(&mut self) -> Result<Self::EncodeNext<'_>, C::Error> {
277 Ok(StorageEncoder::new(self.cx, self.writer.borrow_mut()))
278 }
279
280 #[inline]
281 fn finish_sequence(self) -> Result<Self::Ok, C::Error> {
282 Ok(())
283 }
284}
285
286impl<'a, W, const OPT: Options, C> MapEncoder for StorageEncoder<'a, W, OPT, C>
287where
288 C: ?Sized + Context,
289 W: Writer,
290{
291 type Cx = C;
292 type Ok = ();
293 type EncodeEntry<'this> = StorageEncoder<'a, W::Mut<'this>, OPT, C> where Self: 'this;
294
295 #[inline]
296 fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, C::Error> {
297 Ok(StorageEncoder::new(self.cx, self.writer.borrow_mut()))
298 }
299
300 #[inline]
301 fn finish_map(self) -> Result<Self::Ok, C::Error> {
302 Ok(())
303 }
304}
305
306impl<'a, W, const OPT: Options, C> EntryEncoder for StorageEncoder<'a, W, OPT, C>
307where
308 C: ?Sized + Context,
309 W: Writer,
310{
311 type Cx = C;
312 type Ok = ();
313 type EncodeKey<'this> = StorageEncoder<'a, W::Mut<'this>, OPT, C> where Self: 'this;
314 type EncodeValue<'this> = StorageEncoder<'a, W::Mut<'this>, OPT, C> where Self: 'this;
315
316 #[inline]
317 fn encode_key(&mut self) -> Result<Self::EncodeKey<'_>, C::Error> {
318 Ok(StorageEncoder::new(self.cx, self.writer.borrow_mut()))
319 }
320
321 #[inline]
322 fn encode_value(&mut self) -> Result<Self::EncodeValue<'_>, C::Error> {
323 Ok(StorageEncoder::new(self.cx, self.writer.borrow_mut()))
324 }
325
326 #[inline]
327 fn finish_entry(self) -> Result<Self::Ok, C::Error> {
328 Ok(())
329 }
330}
331
332impl<'a, W, const OPT: Options, C> EntriesEncoder for StorageEncoder<'a, W, OPT, C>
333where
334 C: ?Sized + Context,
335 W: Writer,
336{
337 type Cx = C;
338 type Ok = ();
339 type EncodeEntryKey<'this> = StorageEncoder<'a, W::Mut<'this>, OPT, C> where Self: 'this;
340 type EncodeEntryValue<'this> = StorageEncoder<'a, W::Mut<'this>, OPT, C> where Self: 'this;
341
342 #[inline]
343 fn encode_entry_key(&mut self) -> Result<Self::EncodeEntryKey<'_>, C::Error> {
344 Ok(StorageEncoder::new(self.cx, self.writer.borrow_mut()))
345 }
346
347 #[inline]
348 fn encode_entry_value(&mut self) -> Result<Self::EncodeEntryValue<'_>, C::Error> {
349 Ok(StorageEncoder::new(self.cx, self.writer.borrow_mut()))
350 }
351
352 #[inline]
353 fn finish_entries(self) -> Result<Self::Ok, C::Error> {
354 Ok(())
355 }
356}
357
358impl<'a, W, const OPT: Options, C> VariantEncoder for StorageEncoder<'a, W, OPT, C>
359where
360 C: ?Sized + Context,
361 W: Writer,
362{
363 type Cx = C;
364 type Ok = ();
365 type EncodeTag<'this> = StorageEncoder<'a, W::Mut<'this>, OPT, C> where Self: 'this;
366 type EncodeData<'this> = StorageEncoder<'a, W::Mut<'this>, OPT, C> where Self: 'this;
367
368 #[inline]
369 fn encode_tag(&mut self) -> Result<Self::EncodeTag<'_>, C::Error> {
370 Ok(StorageEncoder::new(self.cx, self.writer.borrow_mut()))
371 }
372
373 #[inline]
374 fn encode_data(&mut self) -> Result<Self::EncodeData<'_>, C::Error> {
375 Ok(StorageEncoder::new(self.cx, self.writer.borrow_mut()))
376 }
377
378 #[inline]
379 fn finish_variant(self) -> Result<Self::Ok, C::Error> {
380 Ok(())
381 }
382}