musli_core/
never.rs

1//! Module that provides a never type which conveniently implements all the
2//! encoder and decoder traits so that it can be used as a placeholder.
3//!
4//! This is a private module of musli, and is not intended for use outside of
5//! the implementation attributes:
6//!
7//! * [`#[musli::trait_defaults]`][crate::trait_defaults].
8
9use core::fmt;
10use core::marker;
11
12use crate::alloc::ToOwned;
13
14use crate::Context;
15use crate::de::{
16    AsDecoder, Decode, DecodeUnsized, DecodeUnsizedBytes, Decoder, EntriesDecoder, EntryDecoder,
17    MapDecoder, SequenceDecoder, SizeHint, UnsizedVisitor, VariantDecoder,
18};
19use crate::en::{
20    Encode, Encoder, EntriesEncoder, EntryEncoder, MapEncoder, SequenceEncoder, VariantEncoder,
21};
22
23/// Marker type used for the [`Never`] type.
24#[doc(hidden)]
25pub enum NeverMarker {}
26
27/// An uninhabitable never type which implements all possible encoders and
28/// decoders. This can be used if your [Encoder] implementation doesn't
29/// implement a particular function.
30///
31/// ```
32/// use std::fmt;
33/// use std::marker::PhantomData;
34///
35/// use musli::Context;
36/// use musli::de::Decoder;
37///
38/// struct MyDecoder<C, M> {
39///     cx: C,
40///     number: u32,
41///     _marker: PhantomData<M>,
42/// }
43///
44/// #[musli::trait_defaults]
45/// impl<'de, C, M> Decoder<'de> for MyDecoder<C, M>
46/// where
47///     C: Context,
48///     M: 'static,
49/// {
50///     #[inline]
51///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52///         write!(f, "32-bit unsigned integers")
53///     }
54///
55///     #[inline]
56///     fn decode_u32(self) -> Result<u32, Self::Error> {
57///         if self.number == 42 {
58///             return Ok(self.number);
59///         }
60///
61///         Err(self.cx.message("I do not have the answer..."))
62///     }
63/// }
64/// ```
65pub struct Never<T: ?Sized = NeverMarker> {
66    // Field makes type uninhabitable.
67    _never: NeverMarker,
68    _marker: marker::PhantomData<T>,
69}
70
71impl<'de, C, M> Decoder<'de> for Never<(C, M)>
72where
73    C: Context,
74    M: 'static,
75{
76    type Cx = C;
77    type Error = C::Error;
78    type Allocator = C::Allocator;
79    type Mode = M;
80    type TryClone = Self;
81    type DecodeBuffer = Self;
82    type DecodePack = Self;
83    type DecodeSequence = Self;
84    type DecodeMapEntries = Self;
85    type DecodeSome = Self;
86    type DecodeMap = Self;
87    type DecodeVariant = Self;
88    type __UseMusliDecoderAttributeMacro = ();
89
90    #[inline]
91    fn cx(&self) -> Self::Cx {
92        match self._never {}
93    }
94
95    #[inline]
96    fn expecting(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
97        match self._never {}
98    }
99
100    #[inline]
101    fn try_clone(&self) -> Option<Self::TryClone> {
102        match self._never {}
103    }
104
105    #[inline]
106    fn decode<T>(self) -> Result<T, Self::Error>
107    where
108        T: Decode<'de, Self::Mode, Self::Allocator>,
109    {
110        match self._never {}
111    }
112
113    #[inline]
114    fn decode_unsized<T, F, O>(self, _: F) -> Result<O, Self::Error>
115    where
116        T: ?Sized + DecodeUnsized<'de, Self::Mode>,
117        F: FnOnce(&T) -> Result<O, Self::Error>,
118    {
119        match self._never {}
120    }
121
122    #[inline]
123    fn decode_unsized_bytes<T, F, O>(self, _: F) -> Result<O, Self::Error>
124    where
125        T: ?Sized + DecodeUnsizedBytes<'de, Self::Mode>,
126        F: FnOnce(&T) -> Result<O, Self::Error>,
127    {
128        match self._never {}
129    }
130}
131
132impl<C, M> AsDecoder for Never<(C, M)>
133where
134    C: Context,
135    M: 'static,
136{
137    type Cx = C;
138    type Error = C::Error;
139    type Allocator = C::Allocator;
140    type Mode = M;
141    type Decoder<'this>
142        = Self
143    where
144        Self: 'this;
145
146    #[inline]
147    fn as_decoder(&self) -> Result<Self::Decoder<'_>, Self::Error> {
148        match self._never {}
149    }
150}
151
152impl<C, M> EntriesDecoder<'_> for Never<(C, M)>
153where
154    C: Context,
155    M: 'static,
156{
157    type Cx = C;
158    type Error = C::Error;
159    type Allocator = C::Allocator;
160    type Mode = M;
161    type DecodeEntryKey<'this>
162        = Self
163    where
164        Self: 'this;
165    type DecodeEntryValue<'this>
166        = Self
167    where
168        Self: 'this;
169
170    #[inline]
171    fn cx(&self) -> Self::Cx {
172        match self._never {}
173    }
174
175    #[inline]
176    fn decode_entry_key(&mut self) -> Result<Option<Self::DecodeEntryKey<'_>>, Self::Error> {
177        match self._never {}
178    }
179
180    #[inline]
181    fn decode_entry_value(&mut self) -> Result<Self::DecodeEntryValue<'_>, Self::Error> {
182        match self._never {}
183    }
184
185    #[inline]
186    fn end_entries(self) -> Result<(), Self::Error> {
187        match self._never {}
188    }
189}
190
191impl<C, M> VariantDecoder<'_> for Never<(C, M)>
192where
193    C: Context,
194    M: 'static,
195{
196    type Cx = C;
197    type Error = C::Error;
198    type Allocator = C::Allocator;
199    type Mode = M;
200    type DecodeTag<'this>
201        = Self
202    where
203        Self: 'this;
204    type DecodeValue<'this>
205        = Self
206    where
207        Self: 'this;
208
209    #[inline]
210    fn cx(&self) -> Self::Cx {
211        match self._never {}
212    }
213
214    #[inline]
215    fn decode_tag(&mut self) -> Result<Self::DecodeTag<'_>, Self::Error> {
216        match self._never {}
217    }
218
219    #[inline]
220    fn decode_value(&mut self) -> Result<Self::DecodeValue<'_>, Self::Error> {
221        match self._never {}
222    }
223}
224
225impl<C, M> MapDecoder<'_> for Never<(C, M)>
226where
227    C: Context,
228    M: 'static,
229{
230    type Cx = C;
231    type Error = C::Error;
232    type Allocator = C::Allocator;
233    type Mode = M;
234    type DecodeEntry<'this>
235        = Self
236    where
237        Self: 'this;
238    type DecodeRemainingEntries<'this>
239        = Self
240    where
241        Self: 'this;
242
243    #[inline]
244    fn cx(&self) -> Self::Cx {
245        match self._never {}
246    }
247
248    #[inline]
249    fn size_hint(&self) -> SizeHint {
250        match self._never {}
251    }
252
253    #[inline]
254    fn decode_entry(&mut self) -> Result<Option<Self::DecodeEntry<'_>>, Self::Error> {
255        match self._never {}
256    }
257
258    #[inline]
259    fn decode_remaining_entries(
260        &mut self,
261    ) -> Result<Self::DecodeRemainingEntries<'_>, Self::Error> {
262        match self._never {}
263    }
264}
265
266impl<C, M> EntryDecoder<'_> for Never<(C, M)>
267where
268    C: Context,
269    M: 'static,
270{
271    type Cx = C;
272    type Error = C::Error;
273    type Allocator = C::Allocator;
274    type Mode = M;
275    type DecodeKey<'this>
276        = Self
277    where
278        Self: 'this;
279    type DecodeValue = Self;
280
281    #[inline]
282    fn cx(&self) -> Self::Cx {
283        match self._never {}
284    }
285
286    #[inline]
287    fn decode_key(&mut self) -> Result<Self::DecodeKey<'_>, Self::Error> {
288        match self._never {}
289    }
290
291    #[inline]
292    fn decode_value(self) -> Result<Self::DecodeValue, Self::Error> {
293        match self._never {}
294    }
295}
296
297impl<C, M> SequenceDecoder<'_> for Never<(C, M)>
298where
299    C: Context,
300    M: 'static,
301{
302    type Cx = C;
303    type Error = C::Error;
304    type Allocator = C::Allocator;
305    type Mode = M;
306    type DecodeNext<'this>
307        = Self
308    where
309        Self: 'this;
310
311    #[inline]
312    fn cx(&self) -> Self::Cx {
313        match self._never {}
314    }
315
316    #[inline]
317    fn decode_next(&mut self) -> Result<Self::DecodeNext<'_>, Self::Error> {
318        match self._never {}
319    }
320
321    #[inline]
322    fn try_decode_next(&mut self) -> Result<Option<Self::DecodeNext<'_>>, Self::Error> {
323        match self._never {}
324    }
325}
326
327impl<C, M> Encoder for Never<(C, M)>
328where
329    C: Context,
330    M: 'static,
331{
332    type Cx = C;
333    type Error = C::Error;
334    type Mode = M;
335    type EncodePack = Self;
336    type EncodeSome = Self;
337    type EncodeSequence = Self;
338    type EncodeMap = Self;
339    type EncodeMapEntries = Self;
340    type EncodeVariant = Self;
341    type EncodeSequenceVariant = Self;
342    type EncodeMapVariant = Self;
343    type __UseMusliEncoderAttributeMacro = ();
344
345    #[inline]
346    fn cx(&self) -> Self::Cx {
347        match self._never {}
348    }
349
350    #[inline]
351    fn expecting(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
352        match self._never {}
353    }
354
355    #[inline]
356    fn encode<T>(self, _: T) -> Result<(), Self::Error>
357    where
358        T: Encode<Self::Mode>,
359    {
360        match self._never {}
361    }
362}
363
364impl<C, O, T> UnsizedVisitor<'_, C, T> for Never<(O, T)>
365where
366    C: Context,
367    T: ?Sized + ToOwned,
368{
369    type Ok = O;
370    type Error = C::Error;
371    type Allocator = C::Allocator;
372    type __UseMusliUnsizedVisitorAttributeMacro = ();
373
374    #[inline]
375    fn expecting(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
376        match self._never {}
377    }
378}
379
380impl<C, M> SequenceEncoder for Never<(C, M)>
381where
382    C: Context,
383    M: 'static,
384{
385    type Cx = C;
386    type Error = C::Error;
387    type Mode = M;
388    type EncodeNext<'this>
389        = Self
390    where
391        Self: 'this;
392
393    #[inline]
394    fn cx(&self) -> Self::Cx {
395        match self._never {}
396    }
397
398    #[inline]
399    fn encode_next(&mut self) -> Result<Self::EncodeNext<'_>, Self::Error> {
400        match self._never {}
401    }
402
403    #[inline]
404    fn finish_sequence(self) -> Result<(), Self::Error> {
405        match self._never {}
406    }
407}
408
409impl<C, M> MapEncoder for Never<(C, M)>
410where
411    C: Context,
412    M: 'static,
413{
414    type Cx = C;
415    type Error = C::Error;
416    type Mode = M;
417    type EncodeEntry<'this>
418        = Self
419    where
420        Self: 'this;
421
422    #[inline]
423    fn cx(&self) -> Self::Cx {
424        match self._never {}
425    }
426
427    #[inline]
428    fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, Self::Error> {
429        match self._never {}
430    }
431
432    fn finish_map(self) -> Result<(), Self::Error> {
433        match self._never {}
434    }
435}
436
437impl<C, M> EntryEncoder for Never<(C, M)>
438where
439    C: Context,
440    M: 'static,
441{
442    type Cx = C;
443    type Error = C::Error;
444    type Mode = M;
445    type EncodeKey<'this>
446        = Self
447    where
448        Self: 'this;
449    type EncodeValue<'this>
450        = Self
451    where
452        Self: 'this;
453
454    #[inline]
455    fn cx(&self) -> Self::Cx {
456        match self._never {}
457    }
458
459    #[inline]
460    fn encode_key(&mut self) -> Result<Self::EncodeKey<'_>, Self::Error> {
461        match self._never {}
462    }
463
464    #[inline]
465    fn encode_value(&mut self) -> Result<Self::EncodeValue<'_>, Self::Error> {
466        match self._never {}
467    }
468
469    #[inline]
470    fn finish_entry(self) -> Result<(), Self::Error> {
471        match self._never {}
472    }
473}
474
475impl<C, M> EntriesEncoder for Never<(C, M)>
476where
477    C: Context,
478    M: 'static,
479{
480    type Cx = C;
481    type Error = C::Error;
482    type Mode = M;
483    type EncodeEntryKey<'this>
484        = Self
485    where
486        Self: 'this;
487    type EncodeEntryValue<'this>
488        = Self
489    where
490        Self: 'this;
491
492    #[inline]
493    fn cx(&self) -> Self::Cx {
494        match self._never {}
495    }
496
497    #[inline]
498    fn encode_entry_key(&mut self) -> Result<Self::EncodeEntryKey<'_>, Self::Error> {
499        match self._never {}
500    }
501
502    #[inline]
503    fn encode_entry_value(&mut self) -> Result<Self::EncodeEntryValue<'_>, Self::Error> {
504        match self._never {}
505    }
506
507    #[inline]
508    fn finish_entries(self) -> Result<(), Self::Error> {
509        match self._never {}
510    }
511}
512
513impl<C, M> VariantEncoder for Never<(C, M)>
514where
515    C: Context,
516    M: 'static,
517{
518    type Cx = C;
519    type Error = C::Error;
520    type Mode = M;
521    type EncodeTag<'this>
522        = Self
523    where
524        Self: 'this;
525    type EncodeData<'this>
526        = Self
527    where
528        Self: 'this;
529
530    #[inline]
531    fn cx(&self) -> Self::Cx {
532        match self._never {}
533    }
534
535    #[inline]
536    fn encode_tag(&mut self) -> Result<Self::EncodeTag<'_>, Self::Error> {
537        match self._never {}
538    }
539
540    #[inline]
541    fn encode_data(&mut self) -> Result<Self::EncodeData<'_>, Self::Error> {
542        match self._never {}
543    }
544
545    #[inline]
546    fn finish_variant(self) -> Result<(), Self::Error> {
547        match self._never {}
548    }
549}