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::no_std::ToOwned;
14
15use crate::alloc::RawVec;
16use crate::de::{
17    AsDecoder, Decode, DecodeUnsized, DecodeUnsizedBytes, Decoder, EntriesDecoder, EntryDecoder,
18    MapDecoder, SequenceDecoder, SizeHint, UnsizedVisitor, VariantDecoder,
19};
20use crate::en::{
21    Encode, Encoder, EntriesEncoder, EntryEncoder, MapEncoder, SequenceEncoder, VariantEncoder,
22};
23use crate::Context;
24
25/// Marker type used for the [`Never`] type.
26#[doc(hidden)]
27pub enum NeverMarker {}
28
29/// An uninhabitable never type which implements all possible encoders and
30/// decoders. This can be used if your [Encoder] implementation doesn't
31/// implement a particular function.
32///
33/// ```
34/// use std::fmt;
35///
36/// use musli::Context;
37/// use musli::de::{Decoder, Decode};
38///
39/// struct MyDecoder<'a, C: ?Sized> {
40///     cx: &'a C,
41///     number: u32,
42/// }
43///
44/// #[musli::decoder]
45/// impl<'de, C: ?Sized + Context> Decoder<'de> for MyDecoder<'_, C> where {
46///     type Cx = C;
47///
48///     fn cx(&self) -> &C {
49///         self.cx
50///     }
51///
52///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53///         write!(f, "32-bit unsigned integers")
54///     }
55///
56///     fn decode_u32(self) -> Result<u32, C::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<A = NeverMarker, B: ?Sized = NeverMarker> {
66    // Field makes type uninhabitable.
67    _never: NeverMarker,
68    _marker: marker::PhantomData<(A, B)>,
69}
70
71impl<T> RawVec<T> for Never<T> {
72    #[inline]
73    fn resize(&mut self, _: usize, _: usize) -> bool {
74        match self._never {}
75    }
76
77    #[inline]
78    fn as_ptr(&self) -> *const T {
79        match self._never {}
80    }
81
82    #[inline]
83    fn as_mut_ptr(&mut self) -> *mut T {
84        match self._never {}
85    }
86
87    #[inline]
88    fn try_merge<B>(&mut self, _: usize, _: B, _: usize) -> Result<(), B>
89    where
90        B: RawVec<T>,
91    {
92        match self._never {}
93    }
94}
95
96impl<'de, C: ?Sized + Context> Decoder<'de> for Never<(), C> {
97    type Cx = C;
98    type Error = C::Error;
99    type Mode = C::Mode;
100    type WithContext<'this, U> = Never<(), U>
101    where
102        U: 'this + Context;
103    type DecodeBuffer = Self;
104    type DecodePack = Self;
105    type DecodeSequence = Self;
106    type DecodeMapEntries = Self;
107    type DecodeSome = Self;
108    type DecodeMap = Self;
109    type DecodeVariant = Self;
110    type __UseMusliDecoderAttributeMacro = ();
111
112    #[inline]
113    fn cx(&self) -> &Self::Cx {
114        match self._never {}
115    }
116
117    #[inline]
118    fn with_context<U>(self, _: &U) -> Result<Self::WithContext<'_, U>, C::Error>
119    where
120        U: Context,
121    {
122        match self._never {}
123    }
124
125    #[inline]
126    fn expecting(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
127        match self._never {}
128    }
129
130    #[inline]
131    fn decode<T>(self) -> Result<T, Self::Error>
132    where
133        T: Decode<'de, Self::Mode>,
134    {
135        match self._never {}
136    }
137
138    #[inline]
139    fn decode_unsized<T, F, O>(self, _: F) -> Result<O, Self::Error>
140    where
141        T: ?Sized + DecodeUnsized<'de, Self::Mode>,
142        F: FnOnce(&T) -> Result<O, Self::Error>,
143    {
144        match self._never {}
145    }
146
147    #[inline]
148    fn decode_unsized_bytes<T, F, O>(self, _: F) -> Result<O, Self::Error>
149    where
150        T: ?Sized + DecodeUnsizedBytes<'de, Self::Mode>,
151        F: FnOnce(&T) -> Result<O, Self::Error>,
152    {
153        match self._never {}
154    }
155}
156
157impl<C: ?Sized + Context> AsDecoder for Never<(), C> {
158    type Cx = C;
159    type Decoder<'this> = Self where Self: 'this;
160
161    #[inline]
162    fn as_decoder(&self) -> Result<Self::Decoder<'_>, C::Error> {
163        match self._never {}
164    }
165}
166
167impl<'de, C: ?Sized + Context> EntriesDecoder<'de> for Never<(), C> {
168    type Cx = C;
169    type DecodeEntryKey<'this> = Self where Self: 'this;
170    type DecodeEntryValue<'this> = Self where Self: 'this;
171
172    #[inline]
173    fn decode_entry_key(&mut self) -> Result<Option<Self::DecodeEntryKey<'_>>, C::Error> {
174        match self._never {}
175    }
176
177    #[inline]
178    fn decode_entry_value(&mut self) -> Result<Self::DecodeEntryValue<'_>, C::Error> {
179        match self._never {}
180    }
181
182    #[inline]
183    fn end_entries(self) -> Result<(), C::Error> {
184        match self._never {}
185    }
186}
187
188impl<'de, C: ?Sized + Context> VariantDecoder<'de> for Never<(), C> {
189    type Cx = C;
190    type DecodeTag<'this> = Self where Self: 'this;
191    type DecodeValue<'this> = Self where Self: 'this;
192
193    #[inline]
194    fn decode_tag(&mut self) -> Result<Self::DecodeTag<'_>, C::Error> {
195        match self._never {}
196    }
197
198    #[inline]
199    fn decode_value(&mut self) -> Result<Self::DecodeValue<'_>, C::Error> {
200        match self._never {}
201    }
202}
203
204impl<'de, C: ?Sized + Context> MapDecoder<'de> for Never<(), C> {
205    type Cx = C;
206    type DecodeEntry<'this> = Self where Self: 'this;
207    type DecodeRemainingEntries<'this> = Self where Self: 'this;
208
209    #[inline]
210    fn size_hint(&self) -> SizeHint {
211        match self._never {}
212    }
213
214    #[inline]
215    fn decode_entry(&mut self) -> Result<Option<Self::DecodeEntry<'_>>, C::Error> {
216        match self._never {}
217    }
218
219    #[inline]
220    fn decode_remaining_entries(
221        &mut self,
222    ) -> Result<Self::DecodeRemainingEntries<'_>, <Self::Cx as Context>::Error> {
223        match self._never {}
224    }
225}
226
227impl<'de, C: ?Sized + Context> EntryDecoder<'de> for Never<(), C> {
228    type Cx = C;
229    type DecodeKey<'this> = Self where Self: 'this;
230    type DecodeValue = Self;
231
232    #[inline]
233    fn decode_key(&mut self) -> Result<Self::DecodeKey<'_>, C::Error> {
234        match self._never {}
235    }
236
237    #[inline]
238    fn decode_value(self) -> Result<Self::DecodeValue, C::Error> {
239        match self._never {}
240    }
241}
242
243impl<'de, C: ?Sized + Context> SequenceDecoder<'de> for Never<(), C> {
244    type Cx = C;
245    type DecodeNext<'this> = Self where Self: 'this;
246
247    #[inline]
248    fn decode_next(&mut self) -> Result<Self::DecodeNext<'_>, C::Error> {
249        match self._never {}
250    }
251
252    #[inline]
253    fn try_decode_next(&mut self) -> Result<Option<Self::DecodeNext<'_>>, C::Error> {
254        match self._never {}
255    }
256}
257
258impl<C: ?Sized + Context, O: 'static> Encoder for Never<O, C> {
259    type Cx = C;
260    type Error = C::Error;
261    type Ok = O;
262    type Mode = C::Mode;
263    type WithContext<'this, U> = Never<O, U> where U: 'this + Context;
264    type EncodePack = Self;
265    type EncodeSome = Self;
266    type EncodeSequence = Self;
267    type EncodeMap = Self;
268    type EncodeMapEntries = Self;
269    type EncodeVariant = Self;
270    type EncodeSequenceVariant = Self;
271    type EncodeMapVariant = Self;
272    type __UseMusliEncoderAttributeMacro = ();
273
274    #[inline]
275    fn cx(&self) -> &Self::Cx {
276        match self._never {}
277    }
278
279    #[inline]
280    fn with_context<U>(self, _: &U) -> Result<Self::WithContext<'_, U>, C::Error>
281    where
282        U: Context,
283    {
284        match self._never {}
285    }
286
287    #[inline]
288    fn expecting(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
289        match self._never {}
290    }
291
292    #[inline]
293    fn encode<T>(self, _: T) -> Result<Self::Ok, C::Error>
294    where
295        T: Encode<Self::Mode>,
296    {
297        match self._never {}
298    }
299}
300
301impl<'de, C, O: 'static, T> UnsizedVisitor<'de, C, T> for Never<O, T>
302where
303    C: ?Sized + Context,
304    T: ?Sized + ToOwned,
305{
306    type Ok = O;
307
308    fn expecting(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
309        match self._never {}
310    }
311}
312
313impl<O: 'static, C: ?Sized + Context> SequenceEncoder for Never<O, C> {
314    type Cx = C;
315    type Ok = O;
316    type EncodeNext<'this> = Self where Self: 'this;
317
318    #[inline]
319    fn encode_next(&mut self) -> Result<Self::EncodeNext<'_>, C::Error> {
320        match self._never {}
321    }
322
323    #[inline]
324    fn finish_sequence(self) -> Result<Self::Ok, C::Error> {
325        match self._never {}
326    }
327}
328
329impl<O: 'static, C: ?Sized + Context> MapEncoder for Never<O, C> {
330    type Cx = C;
331    type Ok = O;
332    type EncodeEntry<'this> = Self where Self: 'this;
333
334    #[inline]
335    fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, C::Error> {
336        match self._never {}
337    }
338
339    fn finish_map(self) -> Result<Self::Ok, C::Error> {
340        match self._never {}
341    }
342}
343
344impl<O: 'static, C: ?Sized + Context> EntryEncoder for Never<O, C> {
345    type Cx = C;
346    type Ok = O;
347    type EncodeKey<'this> = Self where Self: 'this;
348    type EncodeValue<'this> = Self where Self: 'this;
349
350    #[inline]
351    fn encode_key(&mut self) -> Result<Self::EncodeKey<'_>, C::Error> {
352        match self._never {}
353    }
354
355    #[inline]
356    fn encode_value(&mut self) -> Result<Self::EncodeValue<'_>, C::Error> {
357        match self._never {}
358    }
359
360    #[inline]
361    fn finish_entry(self) -> Result<Self::Ok, C::Error> {
362        match self._never {}
363    }
364}
365
366impl<O: 'static, C: ?Sized + Context> EntriesEncoder for Never<O, C> {
367    type Cx = C;
368    type Ok = O;
369    type EncodeEntryKey<'this> = Self where Self: 'this;
370    type EncodeEntryValue<'this> = Self where Self: 'this;
371
372    #[inline]
373    fn encode_entry_key(&mut self) -> Result<Self::EncodeEntryKey<'_>, C::Error> {
374        match self._never {}
375    }
376
377    #[inline]
378    fn encode_entry_value(&mut self) -> Result<Self::EncodeEntryValue<'_>, C::Error> {
379        match self._never {}
380    }
381
382    #[inline]
383    fn finish_entries(self) -> Result<Self::Ok, C::Error> {
384        match self._never {}
385    }
386}
387
388impl<O: 'static, C: ?Sized + Context> VariantEncoder for Never<O, C> {
389    type Cx = C;
390    type Ok = O;
391    type EncodeTag<'this> = Self where Self: 'this;
392    type EncodeData<'this> = Self where Self: 'this;
393
394    #[inline]
395    fn encode_tag(&mut self) -> Result<Self::EncodeTag<'_>, C::Error> {
396        match self._never {}
397    }
398
399    #[inline]
400    fn encode_data(&mut self) -> Result<Self::EncodeData<'_>, C::Error> {
401        match self._never {}
402    }
403
404    #[inline]
405    fn finish_variant(self) -> Result<Self::Ok, C::Error> {
406        match self._never {}
407    }
408}