musli_core/de/decode.rs
1use crate::Allocator;
2
3use super::Decoder;
4
5/// Trait governing how types are decoded.
6///
7/// This is typically implemented automatically using the [`Decode` derive].
8///
9/// [`Decode` derive]: https://docs.rs/musli/latest/musli/_help/derives/
10///
11/// # Examples
12///
13/// ```
14/// use musli::Decode;
15///
16/// #[derive(Decode)]
17/// struct MyType {
18/// data: [u32; 8],
19/// }
20/// ```
21///
22/// Implementing manually:
23///
24/// ```
25/// use musli::{Allocator, Decode, Decoder};
26///
27/// struct MyType {
28/// data: [u32; 8],
29/// }
30///
31/// impl<'de, M, A> Decode<'de, M, A> for MyType
32/// where
33/// A: Allocator,
34/// {
35/// #[inline]
36/// fn decode<D>(decoder: D) -> Result<Self, D::Error>
37/// where
38/// D: Decoder<'de>,
39/// {
40/// Ok(Self {
41/// data: decoder.decode()?,
42/// })
43/// }
44/// }
45/// ```
46///
47/// # Owned bounds
48///
49/// To require a value which doesn't borrow from its input, use
50/// [`DecodeOwned`] instead of writing `for<'de> Decode<'de, M, A>`. See its
51/// documentation for how to use it.
52///
53/// [`DecodeOwned`]: crate::de::DecodeOwned
54pub trait Decode<'de, M, A>: Sized
55where
56 A: Allocator,
57{
58 /// Whether the type is packed. Packed types can be bitwise copied if the
59 /// representation of the serialization format is identical to the memory
60 /// layout of the type.
61 ///
62 /// Note that setting this to `true` has safety implications, since it
63 /// implies that assuming the type is correctly aligned it can be validly
64 /// bitwise copied when encoded. Setting it to `false` is always safe.
65 ///
66 /// This being set to `true` also implies that the type is `Copy`, and must
67 /// not have a `Drop` implementation.
68 const IS_BITWISE_DECODE: bool = false;
69
70 /// Decode the current value.
71 fn decode<D>(decoder: D) -> Result<Self, D::Error>
72 where
73 D: Decoder<'de, Mode = M, Allocator = A>;
74}