musli-core 0.1.4

Core traits and types for the Müsli serialization framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! [<img alt="github" src="https://img.shields.io/badge/github-udoprog/musli-8da0cb?style=for-the-badge&logo=github" height="20">](https://github.com/udoprog/musli)
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/musli-core.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/musli-core)
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-musli--core-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/musli-core)
//!
//! Core traits for [Müsli].
//!
//! [Müsli]: https://docs.rs/musli

#![deny(missing_docs)]
#![no_std]
#![cfg_attr(doc_cfg, feature(doc_cfg))]

#[cfg(feature = "alloc")]
extern crate alloc as rust_alloc;

#[cfg(feature = "std")]
extern crate std;

mod expecting;
mod impls;
mod internal;
mod never;

pub mod alloc;
#[doc(inline)]
pub use self::alloc::Allocator;

mod context;
#[doc(inline)]
pub use self::context::Context;

pub mod de;
#[doc(inline)]
pub use self::de::{Decode, Decoder};

pub mod en;
#[doc(inline)]
pub use self::en::{Encode, Encoder};

pub mod hint;
pub mod mode;

#[doc(hidden)]
pub use musli_macros as __macros;

/// This is an attribute macro that must be used when implementing the following traits:
///
/// * [`Decoder`]
/// * [`de::Visitor`][crate::de::Visitor]
/// * [`de::UnsizedVisitor`][crate::de::UnsizedVisitor]
/// * [`Encoder`]
///
/// It is required to use because these traits might introduce new associated
/// types in the future, and this is [not yet supported] on a language level in
/// Rust. So this attribute macro polyfills any missing types automatically.
///
/// [not yet supported]: https://rust-lang.github.io/rfcs/2532-associated-type-defaults.html
///
/// Note that if the `Cx` or `Mode` associated types are not specified, they
/// will be defaulted to any type parameters which starts with the uppercase `C`
/// or `M` respectively if the trait uses them.
///
/// # Examples
///
/// Implementing `Decoder`:
///
/// ```
/// use std::fmt;
/// use std::marker::PhantomData;
///
/// use musli_core::Context;
/// use musli_core::de::Decoder;
///
/// struct MyDecoder<C, M> {
///     cx: C,
///     _marker: PhantomData<M>,
/// }
///
/// #[musli_core::trait_defaults]
/// impl<'de, C, M> Decoder<'de> for MyDecoder<C, M>
/// where
///     C: Context,
///     M: 'static,
/// {
///     #[inline]
///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
///         write!(f, "32-bit unsigned integers")
///     }
///
///     #[inline]
///     fn decode_u32(self) -> Result<u32, Self::Error> {
///         Ok(42)
///     }
/// }
/// ```
///
/// Implementing `UnsizedVisitor`:
///
/// ```
/// use std::fmt;
///
/// use musli_core::Context;
/// use musli_core::de::UnsizedVisitor;
///
/// struct MyVisitor;
///
/// #[musli_core::trait_defaults]
/// impl<'de, C> UnsizedVisitor<'de, C, [u8]> for MyVisitor
/// where
///     C: Context,
/// {
///     type Ok = ();
///
///     #[inline]
///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
///         write!(f, "a reference of bytes")
///     }
/// }
/// ```
///
/// Implementing `Visitor`:
///
/// ```
/// use std::fmt;
///
/// use musli_core::Context;
/// use musli_core::de::Visitor;
///
/// struct MyVisitor;
///
/// #[musli_core::trait_defaults]
/// impl<'de, C> Visitor<'de, C> for MyVisitor
/// where
///     C: Context,
/// {
///     type Ok = ();
///
///     #[inline]
///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
///         write!(f, "a value that can be decoded into dynamic container")
///     }
/// }
/// ```
///
/// Implementing `Encoder`:
///
/// ```
/// use std::fmt;
/// use std::marker::PhantomData;
///
/// use musli_core::Context;
/// use musli_core::en::Encoder;
///
/// struct MyEncoder<'a, C, M> {
///     value: &'a mut Option<u32>,
///     cx: C,
///     _marker: PhantomData<M>,
/// }
///
/// #[musli_core::trait_defaults]
/// impl<C, M> Encoder for MyEncoder<'_, C, M>
/// where
///     C: Context,
///     M: 'static,
/// {
///     #[inline]
///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
///         write!(f, "32-bit unsigned integers")
///     }
///
///     #[inline]
///     fn encode_u32(self, value: u32) -> Result<(), Self::Error> {
///         *self.value = Some(value);
///         Ok(())
///     }
/// }
/// ```
#[doc(inline)]
pub use musli_macros::musli_core_trait_defaults as trait_defaults;

/// Internal implementation details of musli.
///
/// Using these directly is not supported.
#[doc(hidden)]
pub mod __priv {
    use core::marker::PhantomData;

    pub use crate::alloc::Allocator;
    use crate::alloc::String;
    pub use crate::context::Context;
    pub use crate::de::{
        AsDecoder, Decode, DecodeBytes, DecodePacked, DecodeTrace, Decoder, EntryDecoder,
        MapDecoder, SequenceDecoder, TryFastDecode, VariantDecoder,
    };
    pub use crate::en::{
        Encode, EncodeBytes, EncodePacked, EncodeTrace, Encoder, EntryEncoder, MapEncoder,
        SequenceEncoder, TryFastEncode, VariantEncoder,
    };
    pub use crate::hint::MapHint;
    pub use crate::never::Never;

    pub use ::core::fmt;
    pub use ::core::mem::{needs_drop, offset_of, size_of};
    pub use ::core::option::Option;
    pub use ::core::result::Result;

    #[inline]
    pub fn default<T>() -> T
    where
        T: ::core::default::Default,
    {
        ::core::default::Default::default()
    }

    /// Note that this returns `true` if skipping was unsupported.
    #[inline]
    pub fn skip<'de, D>(decoder: D) -> Result<bool, D::Error>
    where
        D: Decoder<'de>,
    {
        Ok(decoder.try_skip()?.is_unsupported())
    }

    /// Note that this returns `true` if skipping was unsupported.
    #[inline]
    pub fn skip_field<'de, D>(decoder: D) -> Result<bool, D::Error>
    where
        D: EntryDecoder<'de>,
    {
        skip(decoder.decode_value()?)
    }

    /// Collect and allocate a string from a [`Display`] implementation.
    ///
    /// [`Display`]: fmt::Display
    #[inline]
    pub fn collect_string<C>(
        cx: C,
        value: impl fmt::Display,
    ) -> Result<String<C::Allocator>, C::Error>
    where
        C: Context,
    {
        match crate::alloc::collect_string(cx.alloc(), value) {
            Ok(string) => Ok(string),
            Err(error) => Err(cx.message(error)),
        }
    }

    /// Construct a map hint from an `Encode` implementation.
    #[inline]
    pub fn map_hint<M>(encode: &(impl Encode<M> + ?Sized)) -> impl MapHint + '_
    where
        M: 'static,
    {
        EncodeMapHint {
            encode,
            _marker: PhantomData,
        }
    }

    pub(crate) struct EncodeMapHint<'a, T, M>
    where
        T: ?Sized,
    {
        encode: &'a T,
        _marker: PhantomData<M>,
    }

    impl<T, M> MapHint for EncodeMapHint<'_, T, M>
    where
        T: ?Sized + Encode<M>,
    {
        #[inline]
        fn get(self) -> Option<usize> {
            self.encode.size_hint()
        }
    }

    /// Helper methods to report errors.
    pub mod m {
        use core::fmt;

        use crate::Context;

        /// Report that an invalid variant tag was encountered.
        #[inline]
        pub fn invalid_variant_tag<C>(
            cx: C,
            type_name: &'static str,
            tag: impl fmt::Debug,
        ) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Type {type_name} received invalid variant tag {tag:?}"
            ))
        }

        /// The value for the given tag could not be collected.
        #[inline]
        pub fn expected_tag<C>(cx: C, type_name: &'static str, tag: impl fmt::Debug) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!("Type {type_name} expected tag {tag:?}"))
        }

        /// Trying to decode an uninhabitable type.
        #[inline]
        pub fn uninhabitable<C>(cx: C, type_name: &'static str) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Type {type_name} cannot be decoded since it's uninhabitable"
            ))
        }

        /// Encountered an unsupported field tag.
        #[inline]
        pub fn invalid_field_tag<C>(
            cx: C,
            type_name: &'static str,
            tag: impl fmt::Debug,
        ) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Type {type_name} is missing invalid field tag {tag:?}"
            ))
        }

        /// Expected another field to decode.
        #[inline]
        pub fn expected_field_adjacent<C>(
            cx: C,
            type_name: &'static str,
            tag: impl fmt::Debug,
            content: impl fmt::Debug,
        ) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Type {type_name} expected adjacent field {tag:?} or {content:?}"
            ))
        }

        /// Missing adjacent tag when decoding.
        #[inline]
        pub fn missing_adjacent_tag<C>(
            cx: C,
            type_name: &'static str,
            tag: impl fmt::Debug,
        ) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Type {type_name} is missing adjacent tag {tag:?}"
            ))
        }

        /// Encountered an unsupported field tag.
        #[inline]
        pub fn invalid_field_string_tag<C>(
            cx: C,
            type_name: &'static str,
            field: impl fmt::Debug,
        ) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Type {type_name} received invalid field tag {field:?}"
            ))
        }

        /// Missing variant field required to decode.
        #[inline]
        pub fn tagged_enum_unsupported<C>(cx: C, type_name: &'static str) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Encoding format does not supported decoding type {type_name} as a tagged enum"
            ))
        }

        /// Missing variant field required to decode.
        #[inline]
        pub fn missing_variant_field<C>(
            cx: C,
            type_name: &'static str,
            tag: impl fmt::Debug,
        ) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Type {type_name} is missing variant field {tag:?}"
            ))
        }

        /// Encountered an unsupported variant field.
        #[inline]
        pub fn invalid_variant_field_tag<C>(
            cx: C,
            type_name: &'static str,
            variant: impl fmt::Debug,
            tag: impl fmt::Debug,
        ) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!(
                "Type {type_name} received invalid variant field tag {tag:?} for variant {variant:?}",
            ))
        }

        /// Untagged enum could not be decoded.
        #[inline]
        pub fn untagged_mismatch<C>(cx: C, type_name: &'static str) -> C::Error
        where
            C: Context,
        {
            cx.message(format_args!("No variant of {type_name} could be decoded"))
        }
    }
}