Skip to content

Commit 531dc7e

Browse files
committed
Clean up options a bit
1 parent d5ff0f9 commit 531dc7e

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

crates/musli/src/options.rs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Serialization options.
22
33
/// [`Options`] builder.
4-
pub struct OptionsBuilder(Options);
4+
pub struct Builder(Options);
55

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

88
/// Start building new options.
99
///
10-
/// Call [`OptionsBuilder::build`] to construct them.
11-
pub const fn new() -> OptionsBuilder {
12-
OptionsBuilder(DEFAULT)
10+
/// Call [`Builder::build`] to construct them.
11+
pub const fn new() -> Builder {
12+
Builder(DEFAULT)
1313
}
1414

1515
/// Type encapsulating a static options for an encoding.
@@ -31,7 +31,7 @@ const MAP_KEYS_AS_NUMBERS_BIT: Options = 3;
3131
const FLOAT_BIT: Options = 8;
3232
const LENGTH_WIDTH_BIT: Options = 16;
3333

34-
impl OptionsBuilder {
34+
impl Builder {
3535
/// Indicates if an integer serialization should be variable.
3636
#[inline(always)]
3737
pub const fn with_integer(self, integer: Integer) -> Self {
@@ -83,33 +83,34 @@ impl OptionsBuilder {
8383
}
8484
}
8585

86-
#[doc(hidden)]
87-
pub const fn integer<const OPT: Options>() -> Integer {
86+
#[inline(always)]
87+
pub(crate) const fn integer<const OPT: Options>() -> Integer {
8888
match (OPT >> INTEGER_BIT) & 0b1 {
8989
0 => Integer::Variable,
9090
_ => Integer::Fixed,
9191
}
9292
}
9393

94-
#[doc(hidden)]
95-
pub const fn float<const OPT: Options>() -> Float {
94+
#[cfg(test)]
95+
#[inline(always)]
96+
pub(crate) const fn float<const OPT: Options>() -> Float {
9697
match (OPT >> FLOAT_BIT) & 0b11 {
9798
0 => Float::Integer,
9899
1 => Float::Variable,
99100
_ => Float::Fixed,
100101
}
101102
}
102103

103-
#[doc(hidden)]
104-
pub const fn length<const OPT: Options>() -> Integer {
104+
#[inline(always)]
105+
pub(crate) const fn length<const OPT: Options>() -> Integer {
105106
match (OPT >> LENGTH_BIT) & 0b1 {
106107
0 => Integer::Variable,
107108
_ => Integer::Fixed,
108109
}
109110
}
110111

111-
#[doc(hidden)]
112-
pub const fn length_width<const OPT: Options>() -> Width {
112+
#[inline(always)]
113+
pub(crate) const fn length_width<const OPT: Options>() -> Width {
113114
match (OPT >> LENGTH_WIDTH_BIT) & 0b11 {
114115
0 => Width::U8,
115116
1 => Width::U16,
@@ -118,16 +119,16 @@ pub const fn length_width<const OPT: Options>() -> Width {
118119
}
119120
}
120121

121-
#[doc(hidden)]
122-
pub const fn byteorder<const OPT: Options>() -> ByteOrder {
122+
#[inline(always)]
123+
pub(crate) const fn byteorder<const OPT: Options>() -> ByteOrder {
123124
match (OPT >> BYTEORDER_BIT) & 0b1 {
124-
0 => ByteOrder::LittleEndian,
125-
_ => ByteOrder::BigEndian,
125+
0 => ByteOrder::Little,
126+
_ => ByteOrder::Big,
126127
}
127128
}
128129

129-
#[doc(hidden)]
130-
pub const fn is_map_keys_as_numbers<const OPT: Options>() -> bool {
130+
#[inline(always)]
131+
pub(crate) const fn is_map_keys_as_numbers<const OPT: Options>() -> bool {
131132
((OPT >> MAP_KEYS_AS_NUMBERS_BIT) & 0b1) == 1
132133
}
133134

@@ -156,28 +157,40 @@ pub enum Float {
156157
Fixed = 2,
157158
}
158159

159-
/// Byte order.
160+
/// Byte order to use when encoding numbers.
161+
///
162+
/// By default, this is the [`ByteOrder::NATIVE`] byte order of the target
163+
/// platform.
160164
#[derive(PartialEq, Eq)]
161165
#[cfg_attr(test, derive(Debug))]
162166
#[repr(u8)]
163167
#[non_exhaustive]
164168
pub enum ByteOrder {
165169
/// Little endian byte order.
166-
LittleEndian = 0,
170+
Little = 0,
167171
/// Big endian byte order.
168-
BigEndian = 1,
172+
Big = 1,
169173
}
170174

171175
impl ByteOrder {
172176
/// The native byte order.
177+
///
178+
/// [`Little`] for little and [`Big`] for big endian platforms.
179+
///
180+
/// [`Little`]: ByteOrder::Little
181+
/// [`Big`]: ByteOrder::Big
173182
pub const NATIVE: Self = if cfg!(target_endian = "little") {
174-
Self::LittleEndian
183+
Self::Little
175184
} else {
176-
Self::BigEndian
185+
Self::Big
177186
};
178187

179188
/// The network byte order.
180-
pub const NETWORK: Self = Self::BigEndian;
189+
///
190+
/// This is the same as [`Big`].
191+
///
192+
/// [`Big`]: ByteOrder::Big
193+
pub const NETWORK: Self = Self::Big;
181194
}
182195

183196
#[doc(hidden)]
@@ -307,14 +320,14 @@ fn test_builds() {
307320
}
308321

309322
test_case! {
310-
self::new().with_byte_order(ByteOrder::BigEndian) => {
311-
byteorder = ByteOrder::BigEndian,
323+
self::new().with_byte_order(ByteOrder::Big) => {
324+
byteorder = ByteOrder::Big,
312325
}
313326
}
314327

315328
test_case! {
316-
self::new().with_byte_order(ByteOrder::LittleEndian) => {
317-
byteorder = ByteOrder::LittleEndian,
329+
self::new().with_byte_order(ByteOrder::Little) => {
330+
byteorder = ByteOrder::Little,
318331
}
319332
}
320333

0 commit comments

Comments
 (0)