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