musli_common/
options.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
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
/// Type encapsulating a static flavor of an encoding.
pub struct OptionsBuilder(Options);

const DEFAULT: Options = (ByteOrder::NATIVE as Options) << BYTEORDER_BIT;

/// Start building new options.
///
/// Call [`OptionsBuilder::build`] to construct them.
pub const fn new() -> OptionsBuilder {
    OptionsBuilder(DEFAULT)
}

/// Type encapsulating a static flavor of an encoding.
///
/// Note: despite being made up of a primitive type, this cannot be serialized
/// and correctly re-used.
///
/// Making assumptions about its layout might lead to unspecified behavior
/// during encoding. Only use this type through the provided API.
pub type Options = u128;

const BYTEORDER_BIT: Options = 0;
const INTEGER_BIT: Options = 1;
const LENGTH_BIT: Options = 2;
const FLOAT_BIT: Options = 8;
const LENGTH_WIDTH_BIT: Options = 16;

impl OptionsBuilder {
    /// Indicates if an integer serialization should be variable.
    pub const fn with_integer(self, integer: Integer) -> Self {
        Self(self.0 | ((integer as Options) << INTEGER_BIT))
    }

    /// Indicates the configuration of float serialization.
    pub const fn with_float(self, float: Float) -> Self {
        Self(self.0 | ((float as Options) << FLOAT_BIT))
    }

    /// Specify which byte order to use, if that's relevant.
    pub const fn with_byte_order(self, byte_order: ByteOrder) -> Self {
        Self(self.0 | ((byte_order as Options) << BYTEORDER_BIT))
    }

    /// Specify how lengths should be serialized.
    pub const fn with_length(self, length: Integer) -> Self {
        Self(self.0 | ((length as Options) << LENGTH_BIT))
    }

    /// If length is set to [`Integer::Fixed`], specify the width of the length.
    pub const fn with_length_width(self, width: Width) -> Self {
        let this = self.with_length(Integer::Fixed);
        Self(this.0 | ((width as Options) << LENGTH_WIDTH_BIT))
    }

    /// Build a flavor.
    pub const fn build(self) -> Options {
        self.0
    }
}

#[doc(hidden)]
pub const fn integer<const OPT: Options>() -> Integer {
    match (OPT >> INTEGER_BIT) & 0b1 {
        0 => Integer::Variable,
        _ => Integer::Fixed,
    }
}

#[doc(hidden)]
pub const fn float<const OPT: Options>() -> Float {
    match (OPT >> FLOAT_BIT) & 0b11 {
        0 => Float::Integer,
        1 => Float::Variable,
        _ => Float::Fixed,
    }
}

#[doc(hidden)]
pub const fn length<const OPT: Options>() -> Integer {
    match (OPT >> LENGTH_BIT) & 0b1 {
        0 => Integer::Variable,
        _ => Integer::Fixed,
    }
}

#[doc(hidden)]
pub const fn length_width<const OPT: Options>() -> Width {
    match (OPT >> LENGTH_WIDTH_BIT) & 0b11 {
        0 => Width::U8,
        1 => Width::U16,
        2 => Width::U32,
        _ => Width::U64,
    }
}

#[doc(hidden)]
pub const fn byteorder<const OPT: Options>() -> ByteOrder {
    match (OPT >> BYTEORDER_BIT) & 0b1 {
        0 => ByteOrder::LittleEndian,
        _ => ByteOrder::BigEndian,
    }
}

/// Integer serialization mode.
#[cfg_attr(test, derive(Debug, PartialEq))]
#[repr(u8)]
#[non_exhaustive]
pub enum Integer {
    /// Variable number encoding.
    Variable = 0,
    /// Fixed number encoding.
    Fixed = 1,
}

/// Float serialization mode.
#[cfg_attr(test, derive(Debug, PartialEq))]
#[repr(u8)]
#[non_exhaustive]
pub enum Float {
    /// Use the same serialization as integers, after coercing the bits of a
    /// float into an unsigned integer.
    Integer = 0,
    /// Use variable float encoding.
    Variable = 1,
    /// Use fixed float encoding.
    Fixed = 2,
}

/// Byte order.
#[derive(PartialEq, Eq)]
#[cfg_attr(test, derive(Debug))]
#[repr(u8)]
#[non_exhaustive]
pub enum ByteOrder {
    /// Little endian byte order.
    LittleEndian = 0,
    /// Big endian byte order.
    BigEndian = 1,
}

impl ByteOrder {
    /// The native byte order.
    pub const NATIVE: Self = if cfg!(target_endian = "little") {
        Self::LittleEndian
    } else {
        Self::BigEndian
    };

    /// The network byte order.
    pub const NETWORK: Self = Self::BigEndian;
}

#[doc(hidden)]
#[macro_export]
macro_rules! width_arm {
    ($width:expr, $macro:path) => {
        match $width {
            $crate::exports::options::Width::U8 => {
                $macro!(u8)
            }
            $crate::exports::options::Width::U16 => {
                $macro!(u16)
            }
            $crate::exports::options::Width::U32 => {
                $macro!(u32)
            }
            _ => {
                $macro!(u64)
            }
        }
    };
}

/// The width of a numerical type.
#[derive(Clone, Copy)]
#[cfg_attr(test, derive(Debug, PartialEq))]
#[repr(u8)]
#[non_exhaustive]
pub enum Width {
    /// 8 bit width.
    U8 = 0,
    /// 16 bit width.
    U16 = 1,
    /// 32 bit width.
    U32 = 2,
    /// 64 bit width.
    U64 = 3,
}

#[test]
fn test_builds() {
    macro_rules! assert_or_default {
        ($expr:expr, $test:expr, $default:expr, ()) => {
            assert_eq!(
                $test,
                $default,
                "{}: Expected default value for {}",
                stringify!($expr),
                stringify!($test)
            );
        };

        ($expr:expr, $test:expr, $_default:expr, ($expected:expr)) => {
            assert_eq!(
                $test,
                $expected,
                "{}: Expected custom value for {}",
                stringify!($expr),
                stringify!($test)
            );
        };
    }

    macro_rules! test_case {
        ($expr:expr => {
            $(byteorder = $byteorder:expr,)?
            $(integer = $integer:expr,)?
            $(float = $float:expr,)?
            $(length = $length:expr,)?
            $(length_width = $length_width:expr,)?
        }) => {{
            const O: Options = $expr.build();
            assert_or_default!($expr, byteorder::<O>(), ByteOrder::NATIVE, ($($byteorder)?));
            assert_or_default!($expr, integer::<O>(), Integer::Variable, ($($integer)?));
            assert_or_default!($expr, float::<O>(), Float::Integer, ($($float)?));
            assert_or_default!($expr, length::<O>(), Integer::Variable, ($($length)?));
            assert_or_default!($expr, length_width::<O>(), Width::U8, ($($length_width)?));
        }}
    }

    test_case! {
        self::new() => {}
    }

    test_case! {
        self::new().with_integer(Integer::Fixed) => {
            integer = Integer::Fixed,
        }
    }

    test_case! {
        self::new().with_float(Float::Fixed) => {
            float = Float::Fixed,
        }
    }

    test_case! {
        self::new().with_float(Float::Variable) => {
            float = Float::Variable,
        }
    }

    test_case! {
        self::new().with_float(Float::Variable) => {
            float = Float::Variable,
        }
    }

    test_case! {
        self::new().with_byte_order(ByteOrder::BigEndian) => {
            byteorder = ByteOrder::BigEndian,
        }
    }

    test_case! {
        self::new().with_byte_order(ByteOrder::LittleEndian) => {
            byteorder = ByteOrder::LittleEndian,
        }
    }

    test_case! {
        self::new().with_length_width(Width::U16) => {
            length = Integer::Fixed,
            length_width = Width::U16,
        }
    }

    test_case! {
        self::new().with_length_width(Width::U32) => {
            length = Integer::Fixed,
            length_width = Width::U32,
        }
    }

    test_case! {
        self::new().with_length_width(Width::U64) => {
            length = Integer::Fixed,
            length_width = Width::U64,
        }
    }
}