musli_core/en/encoder.rs
1#![allow(unused_variables)]
2
3use core::fmt;
4
5use crate::expecting::{self, Expecting};
6use crate::hint::{MapHint, SequenceHint};
7use crate::Context;
8
9use super::{Encode, EntriesEncoder, MapEncoder, SequenceEncoder, VariantEncoder};
10
11/// Trait governing how the encoder works.
12#[must_use = "Encoders must be consumed through one of its encode_* methods"]
13pub trait Encoder: Sized {
14 /// Context associated with the encoder.
15 type Cx: ?Sized + Context<Error = Self::Error, Mode = Self::Mode>;
16 /// The type returned by the encoder. For [Encode] implementations ensures
17 /// that they are used correctly, since only functions returned by the
18 /// [Encoder] is capable of returning this value.
19 type Ok;
20 /// Error associated with encoding.
21 type Error;
22 /// Mode associated with encoding.
23 type Mode: 'static;
24 /// Constructed [`Encoder`] with a different context.
25 type WithContext<'this, U>: Encoder<Cx = U, Ok = Self::Ok, Error = U::Error, Mode = U::Mode>
26 where
27 U: 'this + Context;
28 /// A simple pack that packs a sequence of elements.
29 type EncodePack: SequenceEncoder<Cx = Self::Cx, Ok = Self::Ok>;
30 /// Encoder returned when encoding an optional value which is present.
31 type EncodeSome: Encoder<Cx = Self::Cx, Ok = Self::Ok, Error = Self::Error, Mode = Self::Mode>;
32 /// The type of a sequence encoder.
33 type EncodeSequence: SequenceEncoder<Cx = Self::Cx, Ok = Self::Ok>;
34 /// The type of a map encoder.
35 type EncodeMap: MapEncoder<Cx = Self::Cx, Ok = Self::Ok>;
36 /// Streaming encoder for map pairs.
37 type EncodeMapEntries: EntriesEncoder<Cx = Self::Cx, Ok = Self::Ok>;
38 /// Encoder for a struct variant.
39 type EncodeVariant: VariantEncoder<Cx = Self::Cx, Ok = Self::Ok>;
40 /// Specialized encoder for a tuple variant.
41 type EncodeSequenceVariant: SequenceEncoder<Cx = Self::Cx, Ok = Self::Ok>;
42 /// Specialized encoder for a struct variant.
43 type EncodeMapVariant: MapEncoder<Cx = Self::Cx, Ok = Self::Ok>;
44
45 /// This is a type argument used to hint to any future implementor that they
46 /// should be using the [`#[musli::encoder]`][musli::encoder] attribute
47 /// macro when implementing [`Encoder`].
48 #[doc(hidden)]
49 type __UseMusliEncoderAttributeMacro;
50
51 /// Access the context associated with the encoder.
52 fn cx(&self) -> &Self::Cx;
53
54 /// Construct an encoder with a different context.
55 fn with_context<U>(
56 self,
57 _: &U,
58 ) -> Result<Self::WithContext<'_, U>, <Self::Cx as Context>::Error>
59 where
60 U: Context,
61 {
62 Err(self.cx().message(format_args!(
63 "Context switch not supported, expected {}",
64 ExpectingWrapper::new(&self).format()
65 )))
66 }
67
68 /// An expectation error. Every other implementation defers to this to
69 /// report that something unexpected happened.
70 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
71
72 /// Encode the value `T` into the current encoder.
73 ///
74 /// This calls the appropriate [`Encode`] implementation for the given type.
75 fn encode<T>(self, value: T) -> Result<Self::Ok, Self::Error>
76 where
77 T: Encode<Self::Mode>;
78
79 /// Encode a unit or something that is completely empty.
80 ///
81 /// # Examples
82 ///
83 /// Deriving an implementation:
84 ///
85 /// ```
86 /// use musli::Encode;
87 ///
88 /// #[derive(Encode)]
89 /// struct UnitStruct;
90 /// ```
91 ///
92 /// Implementing manually:
93 ///
94 /// ```
95 /// use musli::{Encode, Encoder};
96 /// # struct UnitStruct;
97 ///
98 /// impl<M> Encode<M> for UnitStruct {
99 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
100 /// where
101 /// E: Encoder,
102 /// {
103 /// encoder.encode_empty()
104 /// }
105 /// }
106 /// ```
107 #[inline]
108 fn encode_empty(self) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
109 Err(self.cx().message(expecting::unsupported_type(
110 &expecting::Empty,
111 ExpectingWrapper::new(&self),
112 )))
113 }
114
115 /// Encode a boolean value.
116 ///
117 /// # Examples
118 ///
119 /// Deriving an implementation:
120 ///
121 /// ```
122 /// use musli::Encode;
123 ///
124 /// #[derive(Encode)]
125 /// #[musli(packed)]
126 /// struct MyType {
127 /// data: bool
128 /// }
129 /// ```
130 ///
131 /// Implementing manually:
132 ///
133 /// ```
134 /// use musli::{Encode, Encoder};
135 /// # struct MyType { data: bool }
136 ///
137 /// impl<M> Encode<M> for MyType {
138 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
139 /// where
140 /// E: Encoder,
141 /// {
142 /// encoder.encode_bool(self.data)
143 /// }
144 /// }
145 /// ```
146 #[inline]
147 fn encode_bool(self, v: bool) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
148 Err(self.cx().message(expecting::unsupported_type(
149 &expecting::Bool,
150 ExpectingWrapper::new(&self),
151 )))
152 }
153
154 /// Encode a character.
155 ///
156 /// # Examples
157 ///
158 /// Deriving an implementation:
159 ///
160 /// ```
161 /// use musli::Encode;
162 ///
163 /// #[derive(Encode)]
164 /// #[musli(packed)]
165 /// struct MyType {
166 /// data: char
167 /// }
168 /// ```
169 ///
170 /// Implementing manually:
171 ///
172 /// ```
173 /// use musli::{Encode, Encoder};
174 /// # struct MyType { data: char }
175 ///
176 /// impl<M> Encode<M> for MyType {
177 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
178 /// where
179 /// E: Encoder,
180 /// {
181 /// encoder.encode_char(self.data)
182 /// }
183 /// }
184 /// ```
185 #[inline]
186 fn encode_char(self, v: char) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
187 Err(self.cx().message(expecting::unsupported_type(
188 &expecting::Char,
189 ExpectingWrapper::new(&self),
190 )))
191 }
192
193 /// Encode a 8-bit unsigned integer.
194 ///
195 /// # Examples
196 ///
197 /// Deriving an implementation:
198 ///
199 /// ```
200 /// use musli::Encode;
201 ///
202 /// #[derive(Encode)]
203 /// #[musli(packed)]
204 /// struct MyType {
205 /// data: u8
206 /// }
207 /// ```
208 ///
209 /// Implementing manually:
210 ///
211 /// ```
212 /// use musli::{Encode, Encoder};
213 /// # struct MyType { data: u8 }
214 ///
215 /// impl<M> Encode<M> for MyType {
216 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
217 /// where
218 /// E: Encoder,
219 /// {
220 /// encoder.encode_u8(self.data)
221 /// }
222 /// }
223 /// ```
224 #[inline]
225 fn encode_u8(self, v: u8) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
226 Err(self.cx().message(expecting::unsupported_type(
227 &expecting::Unsigned8,
228 ExpectingWrapper::new(&self),
229 )))
230 }
231
232 /// Encode a 16-bit unsigned integer.
233 ///
234 /// # Examples
235 ///
236 /// Deriving an implementation:
237 ///
238 /// ```
239 /// use musli::Encode;
240 ///
241 /// #[derive(Encode)]
242 /// #[musli(packed)]
243 /// struct MyType {
244 /// data: u16
245 /// }
246 /// ```
247 ///
248 /// Implementing manually:
249 ///
250 /// ```
251 /// use musli::{Encode, Encoder};
252 /// # struct MyType { data: u16 }
253 ///
254 /// impl<M> Encode<M> for MyType {
255 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
256 /// where
257 /// E: Encoder,
258 /// {
259 /// encoder.encode_u16(self.data)
260 /// }
261 /// }
262 /// ```
263 #[inline]
264 fn encode_u16(self, v: u16) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
265 Err(self.cx().message(expecting::unsupported_type(
266 &expecting::Unsigned16,
267 ExpectingWrapper::new(&self),
268 )))
269 }
270
271 /// Encode a 32-bit unsigned integer.
272 ///
273 /// # Examples
274 ///
275 /// Deriving an implementation:
276 ///
277 /// ```
278 /// use musli::Encode;
279 ///
280 /// #[derive(Encode)]
281 /// #[musli(packed)]
282 /// struct MyType {
283 /// data: u32
284 /// }
285 /// ```
286 ///
287 /// Implementing manually:
288 ///
289 /// ```
290 /// use musli::{Encode, Encoder};
291 /// # struct MyType { data: u32 }
292 ///
293 /// impl<M> Encode<M> for MyType {
294 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
295 /// where
296 /// E: Encoder,
297 /// {
298 /// encoder.encode_u32(self.data)
299 /// }
300 /// }
301 /// ```
302 #[inline]
303 fn encode_u32(self, v: u32) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
304 Err(self.cx().message(expecting::unsupported_type(
305 &expecting::Unsigned32,
306 ExpectingWrapper::new(&self),
307 )))
308 }
309
310 /// Encode a 64-bit unsigned integer.
311 ///
312 /// # Examples
313 ///
314 /// Deriving an implementation:
315 ///
316 /// ```
317 /// use musli::Encode;
318 ///
319 /// #[derive(Encode)]
320 /// #[musli(packed)]
321 /// struct MyType {
322 /// data: u64
323 /// }
324 /// ```
325 ///
326 /// Implementing manually:
327 ///
328 /// ```
329 /// use musli::{Encode, Encoder};
330 /// # struct MyType { data: u64 }
331 ///
332 /// impl<M> Encode<M> for MyType {
333 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
334 /// where
335 /// E: Encoder,
336 /// {
337 /// encoder.encode_u64(self.data)
338 /// }
339 /// }
340 /// ```
341 #[inline]
342 fn encode_u64(self, v: u64) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
343 Err(self.cx().message(expecting::unsupported_type(
344 &expecting::Unsigned64,
345 ExpectingWrapper::new(&self),
346 )))
347 }
348
349 /// Encode a 128-bit unsigned integer.
350 ///
351 /// # Examples
352 ///
353 /// Deriving an implementation:
354 ///
355 /// ```
356 /// use musli::Encode;
357 ///
358 /// #[derive(Encode)]
359 /// #[musli(packed)]
360 /// struct MyType {
361 /// data: u128
362 /// }
363 /// ```
364 ///
365 /// Implementing manually:
366 ///
367 /// ```
368 /// use musli::{Encode, Encoder};
369 /// # struct MyType { data: u128 }
370 ///
371 /// impl<M> Encode<M> for MyType {
372 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
373 /// where
374 /// E: Encoder,
375 /// {
376 /// encoder.encode_u128(self.data)
377 /// }
378 /// }
379 /// ```
380 #[inline]
381 fn encode_u128(self, v: u128) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
382 Err(self.cx().message(expecting::unsupported_type(
383 &expecting::Unsigned128,
384 ExpectingWrapper::new(&self),
385 )))
386 }
387
388 /// Encode a 8-bit signed integer.
389 ///
390 /// # Examples
391 ///
392 /// Deriving an implementation:
393 ///
394 /// ```
395 /// use musli::Encode;
396 ///
397 /// #[derive(Encode)]
398 /// #[musli(packed)]
399 /// struct MyType {
400 /// data: i8
401 /// }
402 /// ```
403 ///
404 /// Implementing manually:
405 ///
406 /// ```
407 /// use musli::{Encode, Encoder};
408 /// # struct MyType { data: i8 }
409 ///
410 /// impl<M> Encode<M> for MyType {
411 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
412 /// where
413 /// E: Encoder,
414 /// {
415 /// encoder.encode_i8(self.data)
416 /// }
417 /// }
418 /// ```
419 #[inline]
420 fn encode_i8(self, v: i8) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
421 Err(self.cx().message(expecting::unsupported_type(
422 &expecting::Signed8,
423 ExpectingWrapper::new(&self),
424 )))
425 }
426
427 /// Encode a 16-bit signed integer.
428 ///
429 /// # Examples
430 ///
431 /// Deriving an implementation:
432 ///
433 /// ```
434 /// use musli::Encode;
435 ///
436 /// #[derive(Encode)]
437 /// #[musli(packed)]
438 /// struct MyType {
439 /// data: i16
440 /// }
441 /// ```
442 ///
443 /// Implementing manually:
444 ///
445 /// ```
446 /// use musli::{Encode, Encoder};
447 /// # struct MyType { data: i16 }
448 ///
449 /// impl<M> Encode<M> for MyType {
450 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
451 /// where
452 /// E: Encoder,
453 /// {
454 /// encoder.encode_i16(self.data)
455 /// }
456 /// }
457 /// ```
458 #[inline]
459 fn encode_i16(self, v: i16) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
460 Err(self.cx().message(expecting::unsupported_type(
461 &expecting::Signed16,
462 ExpectingWrapper::new(&self),
463 )))
464 }
465
466 /// Encode a 32-bit signed integer.
467 ///
468 /// # Examples
469 ///
470 /// Deriving an implementation:
471 ///
472 /// ```
473 /// use musli::Encode;
474 ///
475 /// #[derive(Encode)]
476 /// #[musli(packed)]
477 /// struct MyType {
478 /// data: i32
479 /// }
480 /// ```
481 ///
482 /// Implementing manually:
483 ///
484 /// ```
485 /// use musli::{Encode, Encoder};
486 /// # struct MyType { data: i32 }
487 ///
488 /// impl<M> Encode<M> for MyType {
489 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
490 /// where
491 /// E: Encoder,
492 /// {
493 /// encoder.encode_i32(self.data)
494 /// }
495 /// }
496 /// ```
497 #[inline]
498 fn encode_i32(self, v: i32) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
499 Err(self.cx().message(expecting::unsupported_type(
500 &expecting::Signed32,
501 ExpectingWrapper::new(&self),
502 )))
503 }
504
505 /// Encode a 64-bit signed integer.
506 ///
507 /// # Examples
508 ///
509 /// Deriving an implementation:
510 ///
511 /// ```
512 /// use musli::Encode;
513 ///
514 /// #[derive(Encode)]
515 /// #[musli(packed)]
516 /// struct MyType {
517 /// data: i64
518 /// }
519 /// ```
520 ///
521 /// Implementing manually:
522 ///
523 /// ```
524 /// use musli::{Encode, Encoder};
525 /// # struct MyType { data: i64 }
526 ///
527 /// impl<M> Encode<M> for MyType {
528 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
529 /// where
530 /// E: Encoder,
531 /// {
532 /// encoder.encode_i64(self.data)
533 /// }
534 /// }
535 /// ```
536 #[inline]
537 fn encode_i64(self, v: i64) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
538 Err(self.cx().message(expecting::unsupported_type(
539 &expecting::Signed64,
540 ExpectingWrapper::new(&self),
541 )))
542 }
543
544 /// Encode a 128-bit signed integer.
545 ///
546 /// # Examples
547 ///
548 /// Deriving an implementation:
549 ///
550 /// ```
551 /// use musli::Encode;
552 ///
553 /// #[derive(Encode)]
554 /// #[musli(packed)]
555 /// struct MyType {
556 /// data: i128
557 /// }
558 /// ```
559 ///
560 /// Implementing manually:
561 ///
562 /// ```
563 /// use musli::{Encode, Encoder};
564 /// # struct MyType { data: i128 }
565 ///
566 /// impl<M> Encode<M> for MyType {
567 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
568 /// where
569 /// E: Encoder,
570 /// {
571 /// encoder.encode_i128(self.data)
572 /// }
573 /// }
574 /// ```
575 #[inline]
576 fn encode_i128(self, v: i128) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
577 Err(self.cx().message(expecting::unsupported_type(
578 &expecting::Signed128,
579 ExpectingWrapper::new(&self),
580 )))
581 }
582
583 /// Encode a [`usize`].
584 ///
585 /// # Examples
586 ///
587 /// Deriving an implementation:
588 ///
589 /// ```
590 /// use musli::Encode;
591 ///
592 /// #[derive(Encode)]
593 /// #[musli(packed)]
594 /// struct MyType {
595 /// data: usize
596 /// }
597 /// ```
598 ///
599 /// Implementing manually:
600 ///
601 /// ```
602 /// use musli::{Encode, Encoder};
603 /// # struct MyType { data: usize }
604 ///
605 /// impl<M> Encode<M> for MyType {
606 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
607 /// where
608 /// E: Encoder,
609 /// {
610 /// encoder.encode_usize(self.data)
611 /// }
612 /// }
613 /// ```
614 #[inline]
615 fn encode_usize(self, v: usize) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
616 Err(self.cx().message(expecting::unsupported_type(
617 &expecting::Usize,
618 ExpectingWrapper::new(&self),
619 )))
620 }
621
622 /// Encode a [`isize`].
623 ///
624 /// # Examples
625 ///
626 /// Deriving an implementation:
627 ///
628 /// ```
629 /// use musli::Encode;
630 ///
631 /// #[derive(Encode)]
632 /// #[musli(packed)]
633 /// struct MyType {
634 /// data: isize
635 /// }
636 /// ```
637 ///
638 /// Implementing manually:
639 ///
640 /// ```
641 /// use musli::{Encode, Encoder};
642 /// # struct MyType { data: isize }
643 ///
644 /// impl<M> Encode<M> for MyType {
645 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
646 /// where
647 /// E: Encoder,
648 /// {
649 /// encoder.encode_isize(self.data)
650 /// }
651 /// }
652 /// ```
653 #[inline]
654 fn encode_isize(self, v: isize) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
655 Err(self.cx().message(expecting::unsupported_type(
656 &expecting::Isize,
657 ExpectingWrapper::new(&self),
658 )))
659 }
660
661 /// Encode a 32-bit floating point value.
662 ///
663 /// # Examples
664 ///
665 /// Deriving an implementation:
666 ///
667 /// ```
668 /// use musli::Encode;
669 ///
670 /// #[derive(Encode)]
671 /// #[musli(packed)]
672 /// struct MyType {
673 /// data: f32
674 /// }
675 /// ```
676 ///
677 /// Implementing manually:
678 ///
679 /// ```
680 /// use musli::{Encode, Encoder};
681 /// # struct MyType { data: f32 }
682 ///
683 /// impl<M> Encode<M> for MyType {
684 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
685 /// where
686 /// E: Encoder,
687 /// {
688 /// encoder.encode_f32(self.data)
689 /// }
690 /// }
691 /// ```
692 #[inline]
693 fn encode_f32(self, v: f32) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
694 Err(self.cx().message(expecting::unsupported_type(
695 &expecting::Float32,
696 ExpectingWrapper::new(&self),
697 )))
698 }
699
700 /// Encode a 64-bit floating point value.
701 ///
702 /// # Examples
703 ///
704 /// Deriving an implementation:
705 ///
706 /// ```
707 /// use musli::Encode;
708 ///
709 /// #[derive(Encode)]
710 /// #[musli(packed)]
711 /// struct MyType {
712 /// data: f64
713 /// }
714 /// ```
715 ///
716 /// Implementing manually:
717 ///
718 /// ```
719 /// use musli::{Encode, Encoder};
720 /// # struct MyType { data: f64 }
721 ///
722 /// impl<M> Encode<M> for MyType {
723 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
724 /// where
725 /// E: Encoder,
726 /// {
727 /// encoder.encode_f64(self.data)
728 /// }
729 /// }
730 /// ```
731 #[inline]
732 fn encode_f64(self, v: f64) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
733 Err(self.cx().message(expecting::unsupported_type(
734 &expecting::Float64,
735 ExpectingWrapper::new(&self),
736 )))
737 }
738
739 /// Encode fixed-length array.
740 ///
741 /// # Examples
742 ///
743 /// Deriving an implementation:
744 ///
745 /// ```
746 /// use musli::Encode;
747 ///
748 /// #[derive(Encode)]
749 /// #[musli(packed)]
750 /// struct MyType {
751 /// data: [u8; 128]
752 /// }
753 /// ```
754 ///
755 /// Implementing manually:
756 ///
757 /// ```
758 /// use musli::{Encode, Encoder};
759 /// # struct MyType { data: [u8; 128] }
760 ///
761 /// impl<M> Encode<M> for MyType {
762 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
763 /// where
764 /// E: Encoder,
765 /// {
766 /// encoder.encode_array(&self.data)
767 /// }
768 /// }
769 /// ```
770 #[inline]
771 fn encode_array<const N: usize>(
772 self,
773 array: &[u8; N],
774 ) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
775 Err(self.cx().message(expecting::unsupported_type(
776 &expecting::Array,
777 ExpectingWrapper::new(&self),
778 )))
779 }
780
781 /// Encode a sequence of bytes.
782 ///
783 /// # Examples
784 ///
785 /// Deriving an implementation:
786 ///
787 /// ```
788 /// use musli::Encode;
789 ///
790 /// #[derive(Encode)]
791 /// #[musli(packed)]
792 /// struct MyType {
793 /// #[musli(bytes)]
794 /// data: Vec<u8>
795 /// }
796 /// ```
797 ///
798 /// Implementing manually:
799 ///
800 /// ```
801 /// use musli::{Encode, Encoder};
802 /// # struct MyType { data: Vec<u8> }
803 ///
804 /// impl<M> Encode<M> for MyType {
805 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
806 /// where
807 /// E: Encoder,
808 /// {
809 /// encoder.encode_bytes(self.data.as_slice())
810 /// }
811 /// }
812 /// ```
813 #[inline]
814 fn encode_bytes(self, bytes: &[u8]) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
815 Err(self.cx().message(expecting::unsupported_type(
816 &expecting::Bytes,
817 ExpectingWrapper::new(&self),
818 )))
819 }
820
821 /// Encode the given slices of bytes in sequence, with one following another
822 /// as a single contiguous byte array.
823 ///
824 /// The provided `len` is trusted, but providing the wrong length must never
825 /// result in any memory unsafety. It might just cause the payload to be
826 /// corrupted.
827 ///
828 /// This can be useful to avoid allocations when a caller doesn't have
829 /// access to a single byte sequence like in
830 /// [`VecDeque`][std::collections::VecDeque].
831 ///
832 /// # Examples
833 ///
834 /// Deriving an implementation:
835 ///
836 /// ```
837 /// use std::collections::VecDeque;
838 /// use musli::Encode;
839 ///
840 /// #[derive(Encode)]
841 /// #[musli(packed)]
842 /// struct MyType {
843 /// data: VecDeque<u8>
844 /// }
845 /// ```
846 ///
847 /// Implementing manually:
848 ///
849 /// ```
850 /// use musli::{Encode, Encoder};
851 /// # use std::collections::VecDeque;
852 /// # struct MyType { data: VecDeque<u8> }
853 ///
854 /// impl<M> Encode<M> for MyType {
855 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
856 /// where
857 /// E: Encoder,
858 /// {
859 /// let (first, second) = self.data.as_slices();
860 /// encoder.encode_bytes_vectored(self.data.len(), [first, second])
861 /// }
862 /// }
863 /// ```
864 #[inline]
865 fn encode_bytes_vectored<I>(
866 self,
867 len: usize,
868 vectors: I,
869 ) -> Result<Self::Ok, <Self::Cx as Context>::Error>
870 where
871 I: IntoIterator<Item: AsRef<[u8]>>,
872 {
873 Err(self.cx().message(expecting::unsupported_type(
874 &expecting::Bytes,
875 ExpectingWrapper::new(&self),
876 )))
877 }
878
879 /// Encode a string.
880 ///
881 /// # Examples
882 ///
883 /// Deriving an implementation:
884 ///
885 /// ```
886 /// use musli::Encode;
887 ///
888 /// #[derive(Encode)]
889 /// #[musli(packed)]
890 /// struct MyType {
891 /// data: String
892 /// }
893 /// ```
894 ///
895 /// Implementing manually:
896 ///
897 /// ```
898 /// use musli::{Encode, Encoder};
899 /// # struct MyType { data: String }
900 ///
901 /// impl<M> Encode<M> for MyType {
902 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
903 /// where
904 /// E: Encoder,
905 /// {
906 /// encoder.encode_string(self.data.as_str())
907 /// }
908 /// }
909 /// ```
910 #[inline]
911 fn encode_string(self, string: &str) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
912 Err(self.cx().message(expecting::unsupported_type(
913 &expecting::String,
914 ExpectingWrapper::new(&self),
915 )))
916 }
917
918 /// Encode a value that implements [`Display`] as a string.
919 ///
920 /// [`Display`]: fmt::Display
921 ///
922 /// # Examples
923 ///
924 /// ```
925 /// use musli::{Encode, Encoder};
926 ///
927 /// struct MyType {
928 /// data: String,
929 /// }
930 ///
931 /// impl<M> Encode<M> for MyType {
932 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
933 /// where
934 /// E: Encoder,
935 /// {
936 /// encoder.collect_string(self.data.as_str())
937 /// }
938 /// }
939 /// ```
940 #[inline]
941 fn collect_string<T>(self, value: &T) -> Result<Self::Ok, <Self::Cx as Context>::Error>
942 where
943 T: ?Sized + fmt::Display,
944 {
945 Err(self.cx().message(expecting::unsupported_type(
946 &expecting::CollectString,
947 ExpectingWrapper::new(&self),
948 )))
949 }
950
951 /// Encode an optional value that is present.
952 ///
953 /// # Examples
954 ///
955 /// Deriving an implementation:
956 ///
957 /// ```
958 /// use musli::Encode;
959 ///
960 /// #[derive(Encode)]
961 /// #[musli(packed)]
962 /// struct MyType {
963 /// data: Option<String>
964 /// }
965 /// ```
966 ///
967 /// Implementing manually:
968 ///
969 /// ```
970 /// use musli::{Encode, Encoder};
971 /// # struct MyType { data: Option<String> }
972 ///
973 /// impl<M> Encode<M> for MyType {
974 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
975 /// where
976 /// E: Encoder,
977 /// {
978 /// match &self.data {
979 /// Some(data) => {
980 /// encoder.encode_some()?.encode(data)
981 /// }
982 /// None => {
983 /// encoder.encode_none()
984 /// }
985 /// }
986 /// }
987 /// }
988 /// ```
989 #[inline]
990 fn encode_some(self) -> Result<Self::EncodeSome, <Self::Cx as Context>::Error> {
991 Err(self.cx().message(expecting::unsupported_type(
992 &expecting::Option,
993 ExpectingWrapper::new(&self),
994 )))
995 }
996
997 /// Encode an optional value that is absent.
998 ///
999 /// # Examples
1000 ///
1001 /// Deriving an implementation:
1002 ///
1003 /// ```
1004 /// use musli::Encode;
1005 ///
1006 /// #[derive(Encode)]
1007 /// #[musli(packed)]
1008 /// struct MyType {
1009 /// data: Option<String>
1010 /// }
1011 /// ```
1012 ///
1013 /// Implementing manually:
1014 ///
1015 /// ```
1016 /// use musli::{Encode, Encoder};
1017 /// # struct MyType { data: Option<String> }
1018 ///
1019 /// impl<M> Encode<M> for MyType {
1020 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1021 /// where
1022 /// E: Encoder,
1023 /// {
1024 /// match &self.data {
1025 /// Some(data) => {
1026 /// encoder.encode_some()?.encode(data)
1027 /// }
1028 /// None => {
1029 /// encoder.encode_none()
1030 /// }
1031 /// }
1032 /// }
1033 /// }
1034 /// ```
1035 #[inline]
1036 fn encode_none(self) -> Result<Self::Ok, <Self::Cx as Context>::Error> {
1037 Err(self.cx().message(expecting::unsupported_type(
1038 &expecting::Option,
1039 ExpectingWrapper::new(&self),
1040 )))
1041 }
1042
1043 /// Construct a pack that can encode more than one element at a time.
1044 ///
1045 /// This hints to the format that it should attempt to encode all of the
1046 /// elements in the packed sequence as compact as possible and that
1047 /// subsequent unpackers will know the exact length of the element being
1048 /// unpacked.
1049 ///
1050 /// # Examples
1051 ///
1052 /// ```
1053 /// use musli::{Encode, Encoder};
1054 /// use musli::en::SequenceEncoder;
1055 ///
1056 /// struct PackedStruct {
1057 /// field: u32,
1058 /// data: [u8; 128],
1059 /// }
1060 ///
1061 /// impl<M> Encode<M> for PackedStruct {
1062 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1063 /// where
1064 /// E: Encoder,
1065 /// {
1066 /// let mut pack = encoder.encode_pack()?;
1067 /// pack.encode_next()?.encode(self.field)?;
1068 /// pack.encode_next()?.encode(self.data)?;
1069 /// pack.finish_sequence()
1070 /// }
1071 /// }
1072 /// ```
1073 #[inline]
1074 fn encode_pack(self) -> Result<Self::EncodePack, <Self::Cx as Context>::Error> {
1075 Err(self.cx().message(expecting::unsupported_type(
1076 &expecting::Pack,
1077 ExpectingWrapper::new(&self),
1078 )))
1079 }
1080
1081 /// Encodes a pack using a closure.
1082 ///
1083 /// # Examples
1084 ///
1085 /// ```
1086 /// use musli::{Encode, Encoder};
1087 /// use musli::en::SequenceEncoder;
1088 ///
1089 /// struct PackedStruct {
1090 /// field: u32,
1091 /// data: [u8; 128],
1092 /// }
1093 ///
1094 /// impl<M> Encode<M> for PackedStruct {
1095 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1096 /// where
1097 /// E: Encoder,
1098 /// {
1099 /// encoder.encode_pack_fn(|pack| {
1100 /// pack.encode_next()?.encode(self.field)?;
1101 /// pack.encode_next()?.encode(&self.data)?;
1102 /// Ok(())
1103 /// })
1104 /// }
1105 /// }
1106 /// ```
1107 #[inline]
1108 fn encode_pack_fn<F>(self, f: F) -> Result<Self::Ok, <Self::Cx as Context>::Error>
1109 where
1110 F: FnOnce(&mut Self::EncodePack) -> Result<(), <Self::Cx as Context>::Error>,
1111 {
1112 let mut pack = self.encode_pack()?;
1113 f(&mut pack)?;
1114 pack.finish_sequence()
1115 }
1116
1117 /// Encode a sequence with a known length `len`.
1118 ///
1119 /// A sequence encodes one element following another and must in some way
1120 /// encode the length of the sequence in the underlying format. It is
1121 /// decoded with [Decoder::decode_sequence].
1122 ///
1123 /// [Decoder::decode_sequence]: crate::de::Decoder::decode_sequence
1124 ///
1125 /// # Examples
1126 ///
1127 /// Deriving an implementation:
1128 ///
1129 /// ```
1130 /// use musli::Encode;
1131 ///
1132 /// #[derive(Encode)]
1133 /// #[musli(packed)]
1134 /// struct MyType {
1135 /// data: Vec<String>
1136 /// }
1137 /// ```
1138 ///
1139 /// Implementing manually:
1140 ///
1141 /// ```
1142 /// use musli::{Encode, Encoder};
1143 /// use musli::en::SequenceEncoder;
1144 /// use musli::hint::SequenceHint;
1145 /// # struct MyType { data: Vec<String> }
1146 ///
1147 /// impl<M> Encode<M> for MyType {
1148 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1149 /// where
1150 /// E: Encoder,
1151 /// {
1152 /// let hint = SequenceHint::with_size(self.data.len());
1153 ///
1154 /// let mut seq = encoder.encode_sequence(&hint)?;
1155 ///
1156 /// for element in &self.data {
1157 /// seq.push(element)?;
1158 /// }
1159 ///
1160 /// seq.finish_sequence()
1161 /// }
1162 /// }
1163 /// ```
1164 ///
1165 /// Encoding a tuple:
1166 ///
1167 /// ```
1168 /// use musli::{Encode, Encoder};
1169 /// use musli::en::SequenceEncoder;
1170 /// use musli::hint::SequenceHint;
1171 ///
1172 /// struct PackedTuple(u32, [u8; 128]);
1173 ///
1174 /// impl<M> Encode<M> for PackedTuple {
1175 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1176 /// where
1177 /// E: Encoder,
1178 /// {
1179 /// static HINT: SequenceHint = SequenceHint::with_size(2);
1180 ///
1181 /// let mut tuple = encoder.encode_sequence(&HINT)?;
1182 /// tuple.encode_next()?.encode(self.0)?;
1183 /// tuple.encode_next()?.encode(&self.1)?;
1184 /// tuple.finish_sequence()
1185 /// }
1186 /// }
1187 /// ```
1188 #[inline]
1189 fn encode_sequence(
1190 self,
1191 hint: &SequenceHint,
1192 ) -> Result<Self::EncodeSequence, <Self::Cx as Context>::Error> {
1193 Err(self.cx().message(expecting::unsupported_type(
1194 &expecting::SequenceWith(hint.size_hint()),
1195 ExpectingWrapper::new(&self),
1196 )))
1197 }
1198
1199 /// Encode a sequence using a closure.
1200 ///
1201 /// # Examples
1202 ///
1203 /// ```
1204 /// use musli::{Encode, Encoder};
1205 /// use musli::en::SequenceEncoder;
1206 /// use musli::hint::SequenceHint;
1207 /// # struct MyType { data: Vec<String> }
1208 ///
1209 /// impl<M> Encode<M> for MyType {
1210 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1211 /// where
1212 /// E: Encoder,
1213 /// {
1214 /// let hint = SequenceHint::with_size(self.data.len());
1215 ///
1216 /// encoder.encode_sequence_fn(&hint, |seq| {
1217 /// for element in &self.data {
1218 /// seq.push(element)?;
1219 /// }
1220 ///
1221 /// Ok(())
1222 /// })
1223 /// }
1224 /// }
1225 /// ```
1226 ///
1227 /// Encoding a tuple:
1228 ///
1229 /// ```
1230 /// use musli::{Encode, Encoder};
1231 /// use musli::en::SequenceEncoder;
1232 /// use musli::hint::SequenceHint;
1233 ///
1234 /// struct PackedTuple(u32, [u8; 128]);
1235 ///
1236 /// impl<M> Encode<M> for PackedTuple {
1237 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1238 /// where
1239 /// E: Encoder,
1240 /// {
1241 /// static HINT: SequenceHint = SequenceHint::with_size(2);
1242 ///
1243 /// encoder.encode_sequence_fn(&HINT, |tuple| {
1244 /// tuple.encode_next()?.encode(self.0)?;
1245 /// tuple.encode_next()?.encode(&self.1)?;
1246 /// Ok(())
1247 /// })
1248 /// }
1249 /// }
1250 /// ```
1251 #[inline]
1252 fn encode_sequence_fn<F>(
1253 self,
1254 hint: &SequenceHint,
1255 f: F,
1256 ) -> Result<Self::Ok, <Self::Cx as Context>::Error>
1257 where
1258 F: FnOnce(&mut Self::EncodeSequence) -> Result<(), <Self::Cx as Context>::Error>,
1259 {
1260 let mut seq = self.encode_sequence(hint)?;
1261 f(&mut seq)?;
1262 seq.finish_sequence()
1263 }
1264
1265 /// Encode a map with a known length `len`.
1266 ///
1267 /// # Examples
1268 ///
1269 /// Deriving an implementation:
1270 ///
1271 /// ```
1272 /// use musli::Encode;
1273 ///
1274 /// #[derive(Encode)]
1275 /// struct Struct {
1276 /// name: String,
1277 /// age: u32,
1278 /// }
1279 /// ```
1280 ///
1281 /// Implementing manually:
1282 ///
1283 /// ```
1284 /// use musli::{Encode, Encoder};
1285 /// use musli::en::MapEncoder;
1286 /// use musli::hint::MapHint;
1287 /// # struct Struct { name: String, age: u32 }
1288 ///
1289 /// impl<M> Encode<M> for Struct {
1290 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1291 /// where
1292 /// E: Encoder,
1293 /// {
1294 /// let hint = MapHint::with_size(2);
1295 ///
1296 /// let mut map = encoder.encode_map(&hint)?;
1297 /// map.insert_entry("name", &self.name)?;
1298 /// map.insert_entry("age", self.age)?;
1299 /// map.finish_map()
1300 /// }
1301 /// }
1302 /// ```
1303 #[inline]
1304 fn encode_map(self, hint: &MapHint) -> Result<Self::EncodeMap, <Self::Cx as Context>::Error> {
1305 Err(self.cx().message(expecting::unsupported_type(
1306 &expecting::Map,
1307 ExpectingWrapper::new(&self),
1308 )))
1309 }
1310
1311 /// Encode a map using a closure.
1312 ///
1313 /// # Examples
1314 ///
1315 /// ```
1316 /// use musli::{Encode, Encoder};
1317 /// use musli::en::MapEncoder;
1318 /// use musli::hint::MapHint;
1319 ///
1320 /// struct Struct {
1321 /// name: String,
1322 /// age: u32
1323 /// }
1324 ///
1325 /// impl<M> Encode<M> for Struct {
1326 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1327 /// where
1328 /// E: Encoder,
1329 /// {
1330 /// let hint = MapHint::with_size(2);
1331 ///
1332 /// encoder.encode_map_fn(&hint, |map| {
1333 /// map.insert_entry("name", &self.name)?;
1334 /// map.insert_entry("age", self.age)?;
1335 /// Ok(())
1336 /// })
1337 /// }
1338 /// }
1339 /// ```
1340 #[inline]
1341 fn encode_map_fn<F>(
1342 self,
1343 hint: &MapHint,
1344 f: F,
1345 ) -> Result<Self::Ok, <Self::Cx as Context>::Error>
1346 where
1347 F: FnOnce(&mut Self::EncodeMap) -> Result<(), <Self::Cx as Context>::Error>,
1348 {
1349 let mut map = self.encode_map(hint)?;
1350 f(&mut map)?;
1351 map.finish_map()
1352 }
1353
1354 /// Encode a map through pairs with a known length `len`.
1355 ///
1356 /// # Examples
1357 ///
1358 /// ```
1359 /// use musli::{Encode, Encoder};
1360 /// use musli::en::EntriesEncoder;
1361 /// use musli::hint::MapHint;
1362 ///
1363 /// struct Struct {
1364 /// name: String,
1365 /// age: u32,
1366 /// }
1367 ///
1368 /// impl<M> Encode<M> for Struct {
1369 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1370 /// where
1371 /// E: Encoder,
1372 /// {
1373 /// let hint = MapHint::with_size(2);
1374 ///
1375 /// let mut m = encoder.encode_map_entries(&hint)?;
1376 ///
1377 /// // Simplified encoding.
1378 /// m.insert_entry("name", &self.name)?;
1379 ///
1380 /// // Key and value encoding as a stream.
1381 /// m.encode_entry_key()?.encode("age")?;
1382 /// m.encode_entry_value()?.encode(self.age)?;
1383 /// m.finish_entries()
1384 /// }
1385 /// }
1386 /// ```
1387 #[inline]
1388 fn encode_map_entries(
1389 self,
1390 hint: &MapHint,
1391 ) -> Result<Self::EncodeMapEntries, <Self::Cx as Context>::Error> {
1392 Err(self.cx().message(expecting::unsupported_type(
1393 &expecting::MapWith(hint.size_hint()),
1394 ExpectingWrapper::new(&self),
1395 )))
1396 }
1397
1398 /// Encode a variant.
1399 ///
1400 /// # Examples
1401 ///
1402 /// ```
1403 /// use musli::Encode;
1404 ///
1405 /// #[derive(Encode)]
1406 /// enum Enum {
1407 /// UnitVariant,
1408 /// TupleVariant(String),
1409 /// StructVariant {
1410 /// data: String,
1411 /// age: u32,
1412 /// }
1413 /// }
1414 /// ```
1415 ///
1416 /// Implementing manually:
1417 ///
1418 /// ```
1419 /// use musli::{Encode, Encoder};
1420 /// use musli::en::{VariantEncoder, SequenceEncoder, MapEncoder};
1421 /// use musli::hint::{MapHint, SequenceHint};
1422 /// # enum Enum { UnitVariant, TupleVariant(String), StructVariant { data: String, age: u32 } }
1423 ///
1424 /// impl<M> Encode<M> for Enum {
1425 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1426 /// where
1427 /// E: Encoder,
1428 /// {
1429 /// let mut variant = encoder.encode_variant()?;
1430 ///
1431 /// match self {
1432 /// Enum::UnitVariant => {
1433 /// variant.insert_variant("UnitVariant", ())
1434 /// }
1435 /// Enum::TupleVariant(data) => {
1436 /// static HINT: SequenceHint = SequenceHint::with_size(1);
1437 ///
1438 /// variant.encode_tag()?.encode_string("TupleVariant")?;
1439 ///
1440 /// let mut tuple = variant.encode_data()?.encode_sequence(&HINT)?;
1441 /// tuple.push(data)?;
1442 /// tuple.finish_sequence()?;
1443 ///
1444 /// variant.finish_variant()
1445 /// }
1446 /// Enum::StructVariant { data, age } => {
1447 /// static HINT: MapHint = MapHint::with_size(2);
1448 ///
1449 /// variant.encode_tag()?.encode_string("StructVariant")?;
1450 ///
1451 /// let mut st = variant.encode_data()?.encode_map(&HINT)?;
1452 /// st.insert_entry("data", data)?;
1453 /// st.insert_entry("age", age)?;
1454 /// st.finish_map()?;
1455 ///
1456 /// variant.finish_variant()
1457 /// }
1458 /// }
1459 /// }
1460 /// }
1461 /// ```
1462 #[inline]
1463 fn encode_variant(self) -> Result<Self::EncodeVariant, <Self::Cx as Context>::Error> {
1464 Err(self.cx().message(expecting::unsupported_type(
1465 &expecting::Variant,
1466 ExpectingWrapper::new(&self),
1467 )))
1468 }
1469
1470 /// Encode a variant using a closure.
1471 ///
1472 /// # Examples
1473 ///
1474 /// ```
1475 /// use musli::{Encode, Encoder};
1476 /// use musli::en::{VariantEncoder, SequenceEncoder, MapEncoder};
1477 /// use musli::hint::{MapHint, SequenceHint};
1478 ///
1479 /// enum Enum {
1480 /// UnitVariant,
1481 /// TupleVariant(String),
1482 /// StructVariant {
1483 /// data: String,
1484 /// age: u32,
1485 /// }
1486 /// }
1487 ///
1488 /// impl<M> Encode<M> for Enum {
1489 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1490 /// where
1491 /// E: Encoder,
1492 /// {
1493 /// match self {
1494 /// Enum::UnitVariant => {
1495 /// encoder.encode_variant()?.insert_variant("variant1", ())
1496 /// }
1497 /// Enum::TupleVariant(data) => {
1498 /// static HINT: SequenceHint = SequenceHint::with_size(2);
1499 ///
1500 /// encoder.encode_variant_fn(|variant| {
1501 /// variant.encode_tag()?.encode("TupleVariant")?;
1502 ///
1503 /// variant.encode_data()?.encode_sequence_fn(&HINT, |tuple| {
1504 /// tuple.push(data)?;
1505 /// Ok(())
1506 /// })?;
1507 ///
1508 /// Ok(())
1509 /// })
1510 /// }
1511 /// Enum::StructVariant { data, age } => {
1512 /// encoder.encode_variant_fn(|variant| {
1513 /// static HINT: MapHint = MapHint::with_size(2);
1514 ///
1515 /// variant.encode_tag()?.encode("variant3")?;
1516 ///
1517 /// variant.encode_data()?.encode_map_fn(&HINT, |st| {
1518 /// st.insert_entry("data", data)?;
1519 /// st.insert_entry("age", age)?;
1520 /// Ok(())
1521 /// })?;
1522 ///
1523 /// Ok(())
1524 /// })
1525 /// }
1526 /// }
1527 /// }
1528 /// }
1529 /// ```
1530 #[inline]
1531 fn encode_variant_fn<F>(self, f: F) -> Result<Self::Ok, <Self::Cx as Context>::Error>
1532 where
1533 F: FnOnce(&mut Self::EncodeVariant) -> Result<(), <Self::Cx as Context>::Error>,
1534 {
1535 let mut variant = self.encode_variant()?;
1536 f(&mut variant)?;
1537 variant.finish_variant()
1538 }
1539
1540 /// Simplified encoding for a unit variant.
1541 ///
1542 /// # Examples
1543 ///
1544 /// Deriving an implementation:
1545 ///
1546 /// ```
1547 /// use musli::Encode;
1548 ///
1549 /// #[derive(Encode)]
1550 /// enum Enum {
1551 /// UnitVariant,
1552 /// }
1553 /// ```
1554 ///
1555 /// Implementing manually:
1556 ///
1557 /// ```
1558 /// use musli::{Encode, Encoder};
1559 /// use musli::en::{VariantEncoder, MapEncoder, SequenceEncoder};
1560 /// # enum Enum { UnitVariant }
1561 ///
1562 /// impl<M> Encode<M> for Enum {
1563 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1564 /// where
1565 /// E: Encoder,
1566 /// {
1567 /// match self {
1568 /// Enum::UnitVariant => {
1569 /// encoder.encode_unit_variant("variant1")
1570 /// }
1571 /// }
1572 /// }
1573 /// }
1574 /// ```
1575 #[inline]
1576 fn encode_unit_variant<T>(self, tag: &T) -> Result<Self::Ok, <Self::Cx as Context>::Error>
1577 where
1578 T: ?Sized + Encode<<Self::Cx as Context>::Mode>,
1579 {
1580 self.encode_variant_fn(|variant| {
1581 variant.encode_tag()?.encode(tag)?;
1582 variant.encode_data()?.encode_empty()?;
1583 Ok(())
1584 })
1585 }
1586
1587 /// Simplified encoding for a tuple variant.
1588 ///
1589 /// # Examples
1590 ///
1591 /// Deriving an implementation:
1592 ///
1593 /// ```
1594 /// use musli::Encode;
1595 ///
1596 /// #[derive(Encode)]
1597 /// enum Enum {
1598 /// TupleVariant(String),
1599 /// }
1600 /// ```
1601 ///
1602 /// Implementing manually:
1603 ///
1604 /// ```
1605 /// use musli::{Encode, Encoder};
1606 /// use musli::en::SequenceEncoder;
1607 /// use musli::hint::SequenceHint;
1608 /// # enum Enum { TupleVariant(String) }
1609 ///
1610 /// impl<M> Encode<M> for Enum {
1611 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1612 /// where
1613 /// E: Encoder,
1614 /// {
1615 /// match self {
1616 /// Enum::TupleVariant(data) => {
1617 /// static HINT: SequenceHint = SequenceHint::with_size(1);
1618 ///
1619 /// let mut variant = encoder.encode_sequence_variant("variant2", &HINT)?;
1620 /// variant.push(data)?;
1621 /// variant.finish_sequence()
1622 /// }
1623 /// }
1624 /// }
1625 /// }
1626 /// ```
1627 #[inline]
1628 fn encode_sequence_variant<T>(
1629 self,
1630 tag: &T,
1631 hint: &SequenceHint,
1632 ) -> Result<Self::EncodeSequenceVariant, <Self::Cx as Context>::Error>
1633 where
1634 T: ?Sized + Encode<<Self::Cx as Context>::Mode>,
1635 {
1636 Err(self.cx().message(expecting::unsupported_type(
1637 &expecting::SequenceVariant,
1638 ExpectingWrapper::new(&self),
1639 )))
1640 }
1641
1642 /// Simplified encoding for a struct variant.
1643 ///
1644 /// # Examples
1645 ///
1646 /// Deriving an implementation:
1647 ///
1648 /// ```
1649 /// use musli::Encode;
1650 ///
1651 /// #[derive(Encode)]
1652 /// enum Enum {
1653 /// StructVariant {
1654 /// data: String,
1655 /// age: u32,
1656 /// }
1657 /// }
1658 /// ```
1659 ///
1660 /// Implementing manually:
1661 ///
1662 /// ```
1663 /// use musli::{Encode, Encoder};
1664 /// use musli::en::MapEncoder;
1665 /// use musli::hint::MapHint;
1666 /// # enum Enum { StructVariant { data: String, age: u32 } }
1667 ///
1668 /// impl<M> Encode<M> for Enum {
1669 /// fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
1670 /// where
1671 /// E: Encoder,
1672 /// {
1673 /// match self {
1674 /// Enum::StructVariant { data, age } => {
1675 /// static HINT: MapHint = MapHint::with_size(2);
1676 ///
1677 /// let mut variant = encoder.encode_map_variant("variant3", &HINT)?;
1678 /// variant.insert_entry("data", data)?;
1679 /// variant.insert_entry("age", age)?;
1680 /// variant.finish_map()
1681 /// }
1682 /// }
1683 /// }
1684 /// }
1685 /// ```
1686 #[inline]
1687 fn encode_map_variant<T>(
1688 self,
1689 tag: &T,
1690 hint: &MapHint,
1691 ) -> Result<Self::EncodeMapVariant, <Self::Cx as Context>::Error>
1692 where
1693 T: ?Sized + Encode<<Self::Cx as Context>::Mode>,
1694 {
1695 Err(self.cx().message(expecting::unsupported_type(
1696 &expecting::MapVariant,
1697 ExpectingWrapper::new(&self),
1698 )))
1699 }
1700}
1701
1702#[repr(transparent)]
1703struct ExpectingWrapper<T> {
1704 inner: T,
1705}
1706
1707impl<T> ExpectingWrapper<T> {
1708 #[inline]
1709 const fn new(value: &T) -> &Self {
1710 // SAFETY: `ExpectingWrapper` is repr(transparent) over `T`.
1711 unsafe { &*(value as *const T as *const Self) }
1712 }
1713}
1714
1715impl<T> Expecting for ExpectingWrapper<T>
1716where
1717 T: Encoder,
1718{
1719 #[inline]
1720 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1721 self.inner.expecting(f)
1722 }
1723}