musli/
compat.rs

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
//! Wrapper types for tweaking how something is encoded.
//!
//! Note that most types in this module have an attribute equivalent:
//! * [`Bytes`] corresponds to using `#[musli(bytes)]` on a field.
//! * [`Packed`] corresponds to using `#[musli(packed)]` on a field.

use crate::de::{Decode, DecodeBytes, DecodePacked, Decoder};
use crate::en::{Encode, EncodeBytes, EncodePacked, Encoder};
use crate::hint::SequenceHint;
use crate::mode::{Binary, Text};

/// Ensures that the given value `T` is encoded as a sequence.
///
/// This exists as a simple shim for certain types, to ensure they're encoded as
/// a sequence, such as `Sequence<()>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Sequence<T>(pub T);

impl<T> Sequence<T> {
    /// Construct a new sequence wrapper.
    pub const fn new(value: T) -> Self {
        Self(value)
    }
}

impl<M> Encode<M> for Sequence<()> {
    #[inline]
    fn encode<E>(&self, _: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
    where
        E: Encoder<Mode = M>,
    {
        static HINT: SequenceHint = SequenceHint::with_size(0);

        encoder.encode_sequence_fn(&HINT, |_| Ok(()))
    }
}

impl<'de, M> Decode<'de, M> for Sequence<()> {
    fn decode<D>(_: &D::Cx, decoder: D) -> Result<Self, D::Error>
    where
        D: Decoder<'de>,
    {
        decoder.decode_sequence(|_| Ok(Self(())))
    }
}

/// Treat `T` as if its bytes.
///
/// This corresponds to the "Bytes" type in the [data model of Müsli] and is the
/// equivalent of using [`#[musli(bytes)]`][bytes] on a field.
///
/// This is only implemented for type where the default behavior is not to pack
/// the value already, this applies to types which implements [`EncodeBytes`]
/// and [`DecodeBytes`].
///
/// [`Vec`]: alloc::vec::Vec
/// [`VecDeque`]: alloc::collections::VecDeque
/// [bytes]: crate::help::derives
/// [data model of Müsli]: crate::help::data_model
///
/// # Examples
///
/// ```
/// use musli::{Decode, Decoder};
/// use musli::compat::Bytes;
///
/// struct Struct {
///     field: Vec<u8>,
/// }
///
/// impl<'de, M> Decode<'de, M> for Struct
/// where
///     Bytes<Vec<u8>>: Decode<'de, M>
/// {
///     fn decode<D>(cx: &D::Cx, decoder: D) -> Result<Self, D::Error>
///     where
///         D: Decoder<'de, Mode = M>,
///     {
///         let Bytes(field) = Decode::decode(cx, decoder)?;
///
///         Ok(Struct {
///             field,
///         })
///     }
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode)]
#[musli(crate, transparent)]
#[musli(mode = Binary, bound = {T: EncodeBytes<Binary>}, decode_bound = {T: DecodeBytes<'de, Binary>})]
#[musli(mode = Text, bound = {T: EncodeBytes<Text>}, decode_bound = {T: DecodeBytes<'de, Text>})]
#[repr(transparent)]
pub struct Bytes<T>(#[musli(bytes)] pub T);

impl<T> AsRef<[u8]> for Bytes<T>
where
    T: AsRef<[u8]>,
{
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl<T> AsMut<[u8]> for Bytes<T>
where
    T: AsMut<[u8]>,
{
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        self.0.as_mut()
    }
}

/// Treat `T` as if its packed.
///
/// This corresponds to the "Bytes" type in the [data model of Müsli]. It
/// encodes any [`Encode`] / [`Decode`] type "on after another" and is the
/// equivalent of using [`#[musli(packed)]`][packed] on a field.
///
/// This is only implemented for type where the default behavior is not to pack
/// the value already, this applies to types which implements [`EncodePacked`]
/// and [`DecodePacked`].
///
/// [packed]: crate::help::derives
///
/// # Examples
///
/// ```
/// use musli::{Decode, Decoder};
/// use musli::compat::Packed;
///
/// struct Struct {
///     field: u8,
///     field2: u32,
/// }
///
/// impl<'de, M> Decode<'de, M> for Struct
/// where
///     Packed<(u8, u32)>: Decode<'de, M>
/// {
///     fn decode<D>(cx: &D::Cx, decoder: D) -> Result<Self, D::Error>
///     where
///         D: Decoder<'de, Mode = M>,
///     {
///         let Packed((field, field2)) = Decode::decode(cx, decoder)?;
///
///         Ok(Struct {
///             field,
///             field2,
///         })
///     }
/// }
/// ```
#[derive(Encode, Decode)]
#[musli(crate, transparent)]
#[musli(mode = Binary, bound = {T: EncodePacked<Binary>}, decode_bound = {T: DecodePacked<'de, Binary>})]
#[musli(mode = Text, bound = {T: EncodePacked<Text>}, decode_bound = {T: DecodePacked<'de, Text>})]
#[repr(transparent)]
pub struct Packed<T>(#[musli(packed)] pub T);