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