Skip to content

Commit 2899169

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

File tree

2 files changed

+86
-35
lines changed

2 files changed

+86
-35
lines changed

crates/musli/src/int/encoding.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{Context, Options, Reader, Writer};
55

66
/// Governs how unsigned integers are encoded into a [`Writer`].
77
#[inline]
8-
pub fn encode_unsigned<C, W, T, const OPT: Options>(
8+
pub(crate) fn encode_unsigned<C, W, T, const OPT: Options>(
99
cx: &C,
1010
writer: W,
1111
value: T,
@@ -27,7 +27,10 @@ where
2727
/// Decode an unsigned value from the specified reader using the configuration
2828
/// passed in through `F`.
2929
#[inline]
30-
pub fn decode_unsigned<'de, C, R, T, const OPT: Options>(cx: &C, reader: R) -> Result<T, C::Error>
30+
pub(crate) fn decode_unsigned<'de, C, R, T, const OPT: Options>(
31+
cx: &C,
32+
reader: R,
33+
) -> Result<T, C::Error>
3134
where
3235
C: ?Sized + Context,
3336
R: Reader<'de>,
@@ -44,7 +47,7 @@ where
4447

4548
/// Governs how signed integers are encoded into a [`Writer`].
4649
#[inline]
47-
pub fn encode_signed<C, W, T, const OPT: Options>(
50+
pub(crate) fn encode_signed<C, W, T, const OPT: Options>(
4851
cx: &C,
4952
writer: W,
5053
value: T,
@@ -66,7 +69,10 @@ where
6669

6770
/// Governs how signed integers are decoded from a [`Reader`].
6871
#[inline]
69-
pub fn decode_signed<'de, C, R, T, const OPT: Options>(cx: &C, reader: R) -> Result<T, C::Error>
72+
pub(crate) fn decode_signed<'de, C, R, T, const OPT: Options>(
73+
cx: &C,
74+
reader: R,
75+
) -> Result<T, C::Error>
7076
where
7177
C: ?Sized + Context,
7278
R: Reader<'de>,
@@ -87,7 +93,7 @@ where
8793

8894
/// Governs how usize lengths are encoded into a [`Writer`].
8995
#[inline]
90-
pub fn encode_usize<C, W, const OPT: Options>(
96+
pub(crate) fn encode_usize<C, W, const OPT: Options>(
9197
cx: &C,
9298
writer: W,
9399
value: usize,
@@ -118,7 +124,10 @@ where
118124

119125
/// Governs how usize lengths are decoded from a [`Reader`].
120126
#[inline]
121-
pub fn decode_usize<'de, C, R, const OPT: Options>(cx: &C, reader: R) -> Result<usize, C::Error>
127+
pub(crate) fn decode_usize<'de, C, R, const OPT: Options>(
128+
cx: &C,
129+
reader: R,
130+
) -> Result<usize, C::Error>
122131
where
123132
C: ?Sized + Context,
124133
R: Reader<'de>,

crates/musli/src/options.rs

+71-29
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,55 @@ impl OptionsBuilder {
8383
}
8484
}
8585

86-
#[doc(hidden)]
87-
pub const fn integer<const OPT: Options>() -> Integer {
86+
#[cfg(any(
87+
feature = "storage",
88+
feature = "wire",
89+
feature = "descriptive",
90+
feature = "json",
91+
feature = "value"
92+
))]
93+
#[inline(always)]
94+
pub(crate) const fn integer<const OPT: Options>() -> Integer {
8895
match (OPT >> INTEGER_BIT) & 0b1 {
8996
0 => Integer::Variable,
9097
_ => Integer::Fixed,
9198
}
9299
}
93100

94-
#[doc(hidden)]
95-
pub const fn float<const OPT: Options>() -> Float {
101+
#[cfg(test)]
102+
#[inline(always)]
103+
pub(crate) const fn float<const OPT: Options>() -> Float {
96104
match (OPT >> FLOAT_BIT) & 0b11 {
97105
0 => Float::Integer,
98106
1 => Float::Variable,
99107
_ => Float::Fixed,
100108
}
101109
}
102110

103-
#[doc(hidden)]
104-
pub const fn length<const OPT: Options>() -> Integer {
111+
#[cfg(any(
112+
feature = "storage",
113+
feature = "wire",
114+
feature = "descriptive",
115+
feature = "json",
116+
feature = "value"
117+
))]
118+
#[inline(always)]
119+
pub(crate) const fn length<const OPT: Options>() -> Integer {
105120
match (OPT >> LENGTH_BIT) & 0b1 {
106121
0 => Integer::Variable,
107122
_ => Integer::Fixed,
108123
}
109124
}
110125

111-
#[doc(hidden)]
112-
pub const fn length_width<const OPT: Options>() -> Width {
126+
#[cfg(any(
127+
feature = "storage",
128+
feature = "wire",
129+
feature = "descriptive",
130+
feature = "json",
131+
feature = "value"
132+
))]
133+
#[inline(always)]
134+
pub(crate) const fn length_width<const OPT: Options>() -> Width {
113135
match (OPT >> LENGTH_WIDTH_BIT) & 0b11 {
114136
0 => Width::U8,
115137
1 => Width::U16,
@@ -118,16 +140,24 @@ pub const fn length_width<const OPT: Options>() -> Width {
118140
}
119141
}
120142

121-
#[doc(hidden)]
122-
pub const fn byteorder<const OPT: Options>() -> ByteOrder {
143+
#[cfg(any(
144+
feature = "storage",
145+
feature = "wire",
146+
feature = "descriptive",
147+
feature = "json",
148+
feature = "value"
149+
))]
150+
#[inline(always)]
151+
pub(crate) const fn byteorder<const OPT: Options>() -> ByteOrder {
123152
match (OPT >> BYTEORDER_BIT) & 0b1 {
124-
0 => ByteOrder::LittleEndian,
125-
_ => ByteOrder::BigEndian,
153+
0 => ByteOrder::Little,
154+
_ => ByteOrder::Big,
126155
}
127156
}
128157

129-
#[doc(hidden)]
130-
pub const fn is_map_keys_as_numbers<const OPT: Options>() -> bool {
158+
#[cfg(all(feature = "alloc", feature = "value"))]
159+
#[inline(always)]
160+
pub(crate) const fn is_map_keys_as_numbers<const OPT: Options>() -> bool {
131161
((OPT >> MAP_KEYS_AS_NUMBERS_BIT) & 0b1) == 1
132162
}
133163

@@ -156,28 +186,40 @@ pub enum Float {
156186
Fixed = 2,
157187
}
158188

159-
/// Byte order.
189+
/// Byte order to use when encoding numbers.
190+
///
191+
/// By default, this is the [`ByteOrder::NATIVE`] byte order of the target
192+
/// platform.
160193
#[derive(PartialEq, Eq)]
161194
#[cfg_attr(test, derive(Debug))]
162195
#[repr(u8)]
163196
#[non_exhaustive]
164197
pub enum ByteOrder {
165198
/// Little endian byte order.
166-
LittleEndian = 0,
199+
Little = 0,
167200
/// Big endian byte order.
168-
BigEndian = 1,
201+
Big = 1,
169202
}
170203

171204
impl ByteOrder {
172205
/// The native byte order.
206+
///
207+
/// [`Little`] for little and [`Big`] for big endian platforms.
208+
///
209+
/// [`Little`]: ByteOrder::Little
210+
/// [`Big`]: ByteOrder::Big
173211
pub const NATIVE: Self = if cfg!(target_endian = "little") {
174-
Self::LittleEndian
212+
Self::Little
175213
} else {
176-
Self::BigEndian
214+
Self::Big
177215
};
178216

179217
/// The network byte order.
180-
pub const NETWORK: Self = Self::BigEndian;
218+
///
219+
/// This is the same as [`Big`].
220+
///
221+
/// [`Big`]: ByteOrder::Big
222+
pub const NETWORK: Self = Self::Big;
181223
}
182224

183225
#[doc(hidden)]
@@ -307,14 +349,14 @@ fn test_builds() {
307349
}
308350

309351
test_case! {
310-
self::new().with_byte_order(ByteOrder::BigEndian) => {
311-
byteorder = ByteOrder::BigEndian,
352+
self::new().with_byte_order(ByteOrder::Big) => {
353+
byteorder = ByteOrder::Big,
312354
}
313355
}
314356

315357
test_case! {
316-
self::new().with_byte_order(ByteOrder::LittleEndian) => {
317-
byteorder = ByteOrder::LittleEndian,
358+
self::new().with_byte_order(ByteOrder::Little) => {
359+
byteorder = ByteOrder::Little,
318360
}
319361
}
320362

0 commit comments

Comments
 (0)