Skip to content

Commit

Permalink
Add getters for current configuration values (#681)
Browse files Browse the repository at this point in the history
This was inspired by suggestions provided in #598, thanks a lot to
@VictorKoenders.

Fixes #598.

Co-authored-by: Trangar <[email protected]>
  • Loading branch information
shahn and VictorKoenders authored Dec 11, 2023
1 parent dd82a9c commit ff7a45a
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 218 deletions.
66 changes: 47 additions & 19 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,47 @@ impl<E, I, L> Configuration<E, I, L> {
pub trait Config:
InternalEndianConfig + InternalIntEncodingConfig + InternalLimitConfig + Copy + Clone
{
/// This configuration's Endianness
fn endianness(&self) -> Endianness;

/// This configuration's Integer Encoding
fn int_encoding(&self) -> IntEncoding;

/// This configuration's byte limit, or `None` if no limit is configured
fn limit(&self) -> Option<usize>;
}

impl<T> Config for T where
T: InternalEndianConfig + InternalIntEncodingConfig + InternalLimitConfig + Copy + Clone
impl<T> Config for T
where
T: InternalEndianConfig + InternalIntEncodingConfig + InternalLimitConfig + Copy + Clone,
{
fn endianness(&self) -> Endianness {
<T as InternalEndianConfig>::ENDIAN
}

fn int_encoding(&self) -> IntEncoding {
<T as InternalIntEncodingConfig>::INT_ENCODING
}

fn limit(&self) -> Option<usize> {
<T as InternalLimitConfig>::LIMIT
}
}

/// Encodes all integer types in big endian.
#[derive(Copy, Clone)]
pub struct BigEndian {}

impl InternalEndianConfig for BigEndian {
const ENDIAN: Endian = Endian::Big;
const ENDIAN: Endianness = Endianness::Big;
}

/// Encodes all integer types in little endian.
#[derive(Copy, Clone)]
pub struct LittleEndian {}

impl InternalEndianConfig for LittleEndian {
const ENDIAN: Endian = Endian::Little;
const ENDIAN: Endianness = Endianness::Little;
}

/// Use fixed-size integer encoding.
Expand Down Expand Up @@ -226,21 +246,35 @@ impl<const N: usize> InternalLimitConfig for Limit<N> {
const LIMIT: Option<usize> = Some(N);
}

/// Endianness of a `Configuration`.
#[derive(PartialEq, Eq)]
#[non_exhaustive]
pub enum Endianness {
/// Little Endian encoding, see `LittleEndian`.
Little,
/// Big Endian encoding, see `BigEndian`.
Big,
}

/// Integer Encoding of a `Configuration`.
#[derive(PartialEq, Eq)]
#[non_exhaustive]
pub enum IntEncoding {
/// Fixed Integer Encoding, see `Fixint`.
Fixed,
/// Variable Integer Encoding, see `Varint`.
Variable,
}

mod internal {
use super::Configuration;
use super::{Configuration, Endianness, IntEncoding};

pub trait InternalEndianConfig {
const ENDIAN: Endian;
const ENDIAN: Endianness;
}

impl<E: InternalEndianConfig, I, L> InternalEndianConfig for Configuration<E, I, L> {
const ENDIAN: Endian = E::ENDIAN;
}

#[derive(PartialEq, Eq)]
pub enum Endian {
Little,
Big,
const ENDIAN: Endianness = E::ENDIAN;
}

pub trait InternalIntEncodingConfig {
Expand All @@ -251,12 +285,6 @@ mod internal {
const INT_ENCODING: IntEncoding = I::INT_ENCODING;
}

#[derive(PartialEq, Eq)]
pub enum IntEncoding {
Fixed,
Variable,
}

pub trait InternalLimitConfig {
const LIMIT: Option<usize>;
}
Expand Down
50 changes: 25 additions & 25 deletions src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
BorrowDecode, BorrowDecoder, Decode, Decoder,
};
use crate::{
config::{Endian, IntEncoding, InternalEndianConfig, InternalIntEncodingConfig},
config::{Endianness, IntEncoding, InternalEndianConfig, InternalIntEncodingConfig},
error::{DecodeError, IntegerType},
impl_borrow_decode,
};
Expand Down Expand Up @@ -65,8 +65,8 @@ impl Decode for u16 {
let mut bytes = [0u8; 2];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => u16::from_le_bytes(bytes),
Endian::Big => u16::from_be_bytes(bytes),
Endianness::Little => u16::from_le_bytes(bytes),
Endianness::Big => u16::from_be_bytes(bytes),
})
}
}
Expand Down Expand Up @@ -94,8 +94,8 @@ impl Decode for u32 {
let mut bytes = [0u8; 4];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => u32::from_le_bytes(bytes),
Endian::Big => u32::from_be_bytes(bytes),
Endianness::Little => u32::from_le_bytes(bytes),
Endianness::Big => u32::from_be_bytes(bytes),
})
}
}
Expand Down Expand Up @@ -123,8 +123,8 @@ impl Decode for u64 {
let mut bytes = [0u8; 8];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => u64::from_le_bytes(bytes),
Endian::Big => u64::from_be_bytes(bytes),
Endianness::Little => u64::from_le_bytes(bytes),
Endianness::Big => u64::from_be_bytes(bytes),
})
}
}
Expand Down Expand Up @@ -152,8 +152,8 @@ impl Decode for u128 {
let mut bytes = [0u8; 16];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => u128::from_le_bytes(bytes),
Endian::Big => u128::from_be_bytes(bytes),
Endianness::Little => u128::from_le_bytes(bytes),
Endianness::Big => u128::from_be_bytes(bytes),
})
}
}
Expand Down Expand Up @@ -182,8 +182,8 @@ impl Decode for usize {
decoder.reader().read(&mut bytes)?;

let value = match D::C::ENDIAN {
Endian::Little => u64::from_le_bytes(bytes),
Endian::Big => u64::from_be_bytes(bytes),
Endianness::Little => u64::from_le_bytes(bytes),
Endianness::Big => u64::from_be_bytes(bytes),
};

value
Expand Down Expand Up @@ -234,8 +234,8 @@ impl Decode for i16 {
let mut bytes = [0u8; 2];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => i16::from_le_bytes(bytes),
Endian::Big => i16::from_be_bytes(bytes),
Endianness::Little => i16::from_le_bytes(bytes),
Endianness::Big => i16::from_be_bytes(bytes),
})
}
}
Expand Down Expand Up @@ -263,8 +263,8 @@ impl Decode for i32 {
let mut bytes = [0u8; 4];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => i32::from_le_bytes(bytes),
Endian::Big => i32::from_be_bytes(bytes),
Endianness::Little => i32::from_le_bytes(bytes),
Endianness::Big => i32::from_be_bytes(bytes),
})
}
}
Expand Down Expand Up @@ -292,8 +292,8 @@ impl Decode for i64 {
let mut bytes = [0u8; 8];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => i64::from_le_bytes(bytes),
Endian::Big => i64::from_be_bytes(bytes),
Endianness::Little => i64::from_le_bytes(bytes),
Endianness::Big => i64::from_be_bytes(bytes),
})
}
}
Expand Down Expand Up @@ -321,8 +321,8 @@ impl Decode for i128 {
let mut bytes = [0u8; 16];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => i128::from_le_bytes(bytes),
Endian::Big => i128::from_be_bytes(bytes),
Endianness::Little => i128::from_le_bytes(bytes),
Endianness::Big => i128::from_be_bytes(bytes),
})
}
}
Expand Down Expand Up @@ -350,8 +350,8 @@ impl Decode for isize {
let mut bytes = [0u8; 8];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => i64::from_le_bytes(bytes),
Endian::Big => i64::from_be_bytes(bytes),
Endianness::Little => i64::from_le_bytes(bytes),
Endianness::Big => i64::from_be_bytes(bytes),
} as isize)
}
}
Expand All @@ -374,8 +374,8 @@ impl Decode for f32 {
let mut bytes = [0u8; 4];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => f32::from_le_bytes(bytes),
Endian::Big => f32::from_be_bytes(bytes),
Endianness::Little => f32::from_le_bytes(bytes),
Endianness::Big => f32::from_be_bytes(bytes),
})
}
}
Expand All @@ -387,8 +387,8 @@ impl Decode for f64 {
let mut bytes = [0u8; 8];
decoder.reader().read(&mut bytes)?;
Ok(match D::C::ENDIAN {
Endian::Little => f64::from_le_bytes(bytes),
Endian::Big => f64::from_be_bytes(bytes),
Endianness::Little => f64::from_le_bytes(bytes),
Endianness::Big => f64::from_be_bytes(bytes),
})
}
}
Expand Down
50 changes: 25 additions & 25 deletions src/enc/impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{write::Writer, Encode, Encoder};
use crate::{
config::{Endian, IntEncoding, InternalEndianConfig, InternalIntEncodingConfig},
config::{Endianness, IntEncoding, InternalEndianConfig, InternalIntEncodingConfig},
error::EncodeError,
};
use core::{
Expand Down Expand Up @@ -51,8 +51,8 @@ impl Encode for u16 {
crate::varint::varint_encode_u16(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
},
}
}
Expand All @@ -71,8 +71,8 @@ impl Encode for u32 {
crate::varint::varint_encode_u32(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
},
}
}
Expand All @@ -91,8 +91,8 @@ impl Encode for u64 {
crate::varint::varint_encode_u64(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
},
}
}
Expand All @@ -111,8 +111,8 @@ impl Encode for u128 {
crate::varint::varint_encode_u128(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
},
}
}
Expand All @@ -131,8 +131,8 @@ impl Encode for usize {
crate::varint::varint_encode_usize(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&(*self as u64).to_be_bytes()),
Endian::Little => encoder.writer().write(&(*self as u64).to_le_bytes()),
Endianness::Big => encoder.writer().write(&(*self as u64).to_be_bytes()),
Endianness::Little => encoder.writer().write(&(*self as u64).to_le_bytes()),
},
}
}
Expand Down Expand Up @@ -163,8 +163,8 @@ impl Encode for i16 {
crate::varint::varint_encode_i16(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
},
}
}
Expand All @@ -183,8 +183,8 @@ impl Encode for i32 {
crate::varint::varint_encode_i32(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
},
}
}
Expand All @@ -203,8 +203,8 @@ impl Encode for i64 {
crate::varint::varint_encode_i64(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
},
}
}
Expand All @@ -223,8 +223,8 @@ impl Encode for i128 {
crate::varint::varint_encode_i128(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
},
}
}
Expand All @@ -243,8 +243,8 @@ impl Encode for isize {
crate::varint::varint_encode_isize(encoder.writer(), E::C::ENDIAN, *self)
}
IntEncoding::Fixed => match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&(*self as i64).to_be_bytes()),
Endian::Little => encoder.writer().write(&(*self as i64).to_le_bytes()),
Endianness::Big => encoder.writer().write(&(*self as i64).to_be_bytes()),
Endianness::Little => encoder.writer().write(&(*self as i64).to_le_bytes()),
},
}
}
Expand All @@ -259,17 +259,17 @@ impl Encode for NonZeroIsize {
impl Encode for f32 {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
}
}
}

impl Encode for f64 {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
match E::C::ENDIAN {
Endian::Big => encoder.writer().write(&self.to_be_bytes()),
Endian::Little => encoder.writer().write(&self.to_le_bytes()),
Endianness::Big => encoder.writer().write(&self.to_be_bytes()),
Endianness::Little => encoder.writer().write(&self.to_le_bytes()),
}
}
}
Expand Down
Loading

0 comments on commit ff7a45a

Please sign in to comment.