musli/
de.rs

1//! Traits for generically dealing with a decoding framework.
2//!
3//! The central traits are [Decode] and [Decoder].
4//!
5//! A type implementing [Decode] can use an [Decoder] to decode an instance of
6//! itself. This also comes with a derive allowing you to derive high
7//! performance decoding associated with native Rust types.
8//!
9//! ```
10//! use musli::Decode;
11//!
12//! #[derive(Decode)]
13//! pub struct Person<'a> {
14//!     name: &'a str,
15//!     age: u32,
16//! }
17//! ```
18
19/// Derive which automatically implements the [`Decode` trait].
20///
21/// See the [`derives` module] for detailed documentation.
22///
23/// [`derives` module]: crate::_help::derives
24/// [`Decode` trait]: trait@Decode
25///
26/// # Examples
27///
28/// ```
29/// use musli::Decode;
30///
31/// #[derive(Decode)]
32/// struct MyType {
33///     data: [u8; 128],
34/// }
35/// ```
36#[doc(inline)]
37pub use musli_core::__macros::Decode;
38
39/// This is an attribute macro that must be used when implementing a
40/// [`Decoder`].
41///
42/// It is required to use because a [`Decoder`] implementation might introduce
43/// new associated types in the future, and this is [not yet supported] on a
44/// language level in Rust. So this attribute macro polyfills any missing types
45/// automatically.
46///
47/// [not yet supported]: https://rust-lang.github.io/rfcs/2532-associated-type-defaults.html
48///
49/// Note that if the `Cx` or `Mode` associated types are not specified, they
50/// will be defaulted to any type parameters which starts with the uppercase `C`
51/// or `M` respectively.
52///
53/// # Examples
54///
55/// ```
56/// use std::fmt;
57/// use std::marker::PhantomData;
58///
59/// use musli::Context;
60/// use musli::de::{Decoder, Decode};
61///
62/// struct MyDecoder<C, M> {
63///     cx: C,
64///     _marker: PhantomData<M>,
65/// }
66///
67/// #[musli::decoder]
68/// impl<'de, C, M> Decoder<'de> for MyDecoder<C, M>
69/// where
70///     C: Context,
71///     M: 'static,
72/// {
73///     #[inline]
74///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75///         write!(f, "32-bit unsigned integers")
76///     }
77///
78///     #[inline]
79///     fn decode_u32(self) -> Result<u32, Self::Error> {
80///         Ok(42)
81///     }
82/// }
83/// ```
84#[doc(inline)]
85pub use musli_core::__macros::decoder;
86
87/// This is an attribute macro that must be used when implementing a
88/// [`Visitor`].
89///
90/// It is required to use because a [`Visitor`] implementation might introduce
91/// new associated types in the future, and this is [not yet supported] on a
92/// language level in Rust. So this attribute macro polyfills any missing types
93/// automatically.
94///
95/// [not yet supported]: https://rust-lang.github.io/rfcs/2532-associated-type-defaults.html
96/// [`Visitor`]: crate::de::Visitor
97///
98/// # Examples
99///
100/// ```
101/// use std::fmt;
102///
103/// use musli::Context;
104/// use musli::de::Visitor;
105///
106/// struct AnyVisitor;
107///
108/// #[musli::de::visitor]
109/// impl<'de, C> Visitor<'de, C> for AnyVisitor
110/// where
111///     C: Context,
112/// {
113///     type Ok = ();
114///
115///     #[inline]
116///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117///         write!(
118///             f,
119///             "a value that can be decoded into dynamic container"
120///         )
121///     }
122/// }
123/// ```
124#[doc(inline)]
125pub use musli_core::__macros::visitor;
126
127/// This is an attribute macro that must be used when implementing a
128/// [`UnsizedVisitor`].
129///
130/// It is required to use because a [`UnsizedVisitor`] implementation might
131/// introduce new associated types in the future, and this is [not yet
132/// supported] on a language level in Rust. So this attribute macro polyfills
133/// any missing types automatically.
134///
135/// [not yet supported]: https://rust-lang.github.io/rfcs/2532-associated-type-defaults.html
136/// [`UnsizedVisitor`]: crate::de::UnsizedVisitor
137///
138/// # Examples
139///
140/// ```
141/// use std::fmt;
142///
143/// use musli::Context;
144/// use musli::de::UnsizedVisitor;
145///
146/// struct Visitor;
147///
148/// #[musli::de::unsized_visitor]
149/// impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor
150/// where
151///     C: Context,
152/// {
153///     type Ok = ();
154///
155///     #[inline]
156///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157///         write!(
158///             f,
159///             "a reference of bytes"
160///         )
161///     }
162/// }
163/// ```
164#[doc(inline)]
165pub use musli_core::__macros::unsized_visitor;
166
167#[doc(inline)]
168pub use musli_core::de::__traits::{
169    AsDecoder, Decode, DecodeBytes, DecodeOwned, DecodePacked, DecodeSliceBuilder, DecodeTrace,
170    DecodeUnsized, DecodeUnsizedBytes, Decoder, EntriesDecoder, EntryDecoder, MapDecoder,
171    SequenceDecoder, SizeHint, Skip, TryFastDecode, UnsizedVisitor, VariantDecoder, Visitor,
172};
173
174#[cfg(any(
175    feature = "storage",
176    feature = "wire",
177    feature = "descriptive",
178    feature = "value"
179))]
180pub(crate) use musli_core::de::utils;