Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make Configuration functions const #456

Merged
merged 1 commit into from
Dec 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ impl Configuration {
/// - Little endian
/// - Variable int encoding
/// - Skip fixed array length
pub fn standard() -> Self {
pub const fn standard() -> Self {
Self::generate()
}

/// Creates the "legacy" default config. This is the default config that was present in bincode 1.0
/// - Little endian
/// - Fixed int length encoding
/// - Write array lengths
pub fn legacy() -> Configuration<LittleEndian, Fixint, WriteFixedArrayLength, NoLimit> {
pub const fn legacy() -> Configuration<LittleEndian, Fixint, WriteFixedArrayLength, NoLimit> {
Self::generate()
}
}

impl<E, I, A, L> Configuration<E, I, A, L> {
fn generate<_E, _I, _A, _L>() -> Configuration<_E, _I, _A, _L> {
const fn generate<_E, _I, _A, _L>() -> Configuration<_E, _I, _A, _L> {
Configuration {
_e: PhantomData,
_i: PhantomData,
Expand All @@ -76,12 +76,12 @@ impl<E, I, A, L> Configuration<E, I, A, L> {
}

/// Makes bincode encode all integer types in big endian.
pub fn with_big_endian(self) -> Configuration<BigEndian, I, A> {
pub const fn with_big_endian(self) -> Configuration<BigEndian, I, A> {
Self::generate()
}

/// Makes bincode encode all integer types in little endian.
pub fn with_little_endian(self) -> Configuration<LittleEndian, I, A> {
pub const fn with_little_endian(self) -> Configuration<LittleEndian, I, A> {
Self::generate()
}

Expand Down Expand Up @@ -142,7 +142,7 @@ impl<E, I, A, L> Configuration<E, I, A, L> {
///
/// Note that u256 and the like are unsupported by this format; if and when they are added to the
/// language, they may be supported via the extension point given by the 255 byte.
pub fn with_variable_int_encoding(self) -> Configuration<E, Varint, A> {
pub const fn with_variable_int_encoding(self) -> Configuration<E, Varint, A> {
Self::generate()
}

Expand All @@ -151,27 +151,27 @@ impl<E, I, A, L> Configuration<E, I, A, L> {
/// * Fixed size integers are encoded directly
/// * Enum discriminants are encoded as u32
/// * Lengths and usize are encoded as u64
pub fn with_fixed_int_encoding(self) -> Configuration<E, Fixint, A> {
pub const fn with_fixed_int_encoding(self) -> Configuration<E, Fixint, A> {
Self::generate()
}

/// Skip writing the length of fixed size arrays (`[u8; N]`) before writing the array
pub fn skip_fixed_array_length(self) -> Configuration<E, I, SkipFixedArrayLength> {
pub const fn skip_fixed_array_length(self) -> Configuration<E, I, SkipFixedArrayLength> {
Self::generate()
}

/// Write the length of fixed size arrays (`[u8; N]`) before writing the array
pub fn write_fixed_array_length(self) -> Configuration<E, I, WriteFixedArrayLength> {
pub const fn write_fixed_array_length(self) -> Configuration<E, I, WriteFixedArrayLength> {
Self::generate()
}

/// Sets the byte limit to `limit`.
pub fn with_limit<const N: usize>(self) -> Configuration<E, I, A, Limit<N>> {
pub const fn with_limit<const N: usize>(self) -> Configuration<E, I, A, Limit<N>> {
Self::generate()
}

/// Clear the byte limit.
pub fn with_no_limit(self) -> Configuration<E, I, A, NoLimit> {
pub const fn with_no_limit(self) -> Configuration<E, I, A, NoLimit> {
Self::generate()
}
}
Expand Down