Skip to content

Commit 7800f23

Browse files
committed
Consolidate fixed integer API methods
1 parent f6e00a5 commit 7800f23

4 files changed

Lines changed: 27 additions & 55 deletions

File tree

src/fixed.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ pub trait FixedInt: Sized + Copy {
1010
type Bytes: AsRef<[u8]>;
1111
const ENCODED_SIZE: usize = size_of::<Self>();
1212

13-
/// Encode a value into the given slice using machine endianness. Returns `None` if `dst`
13+
/// Encode a value into the given slice using little-endian. Returns `None` if `dst`
1414
/// doesn't provide enough space to encode this integer.
15+
///
16+
/// Use `switch_endianness()` if machine endianness doesn't match the desired target encoding.
1517
fn encode_fixed(self, dst: &mut [u8]) -> Option<()>;
1618
/// Returns the representation of [`FixedInt`] as [`Bytes`], the little-endian representation
1719
/// of self in the stack.
1820
fn encode_fixed_light(self) -> Self::Bytes;
1921

20-
/// Decode a value from the given slice assuming little-endian.
21-
fn decode_le_fixed(src: &[u8]) -> Option<Self>;
22-
/// Decode a value from the given slice assuming big-endian.
23-
fn decode_be_fixed(src: &[u8]) -> Option<Self>;
22+
/// Decode a value from the given slice assuming little-endian. Use `switch_endianness()` on
23+
/// the returned value if the source was not encoded in little-endian.
24+
fn decode_fixed(src: &[u8]) -> Option<Self>;
2425

2526
/// Helper: Encode the value and return a Vec.
2627
fn encode_fixed_vec(self) -> Vec<u8> {
@@ -51,22 +52,14 @@ macro_rules! impl_fixedint {
5152
self.to_le_bytes()
5253
}
5354

54-
fn decode_le_fixed(src: &[u8]) -> Option<Self> {
55+
fn decode_fixed(src: &[u8]) -> Option<Self> {
5556
if src.len() == size_of::<Self>() {
5657
Some(Self::from_le_bytes(src.try_into().unwrap()))
5758
} else {
5859
None
5960
}
6061
}
6162

62-
fn decode_be_fixed(src: &[u8]) -> Option<Self> {
63-
if src.len() == size_of::<Self>() {
64-
Some(Self::from_be_bytes(src.try_into().unwrap()))
65-
} else {
66-
None
67-
}
68-
}
69-
7063
fn switch_endianness(self) -> Self {
7164
Self::from_le_bytes(self.to_be_bytes())
7265
}

src/fixed_tests.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,14 @@ mod tests {
9292
let g: i32 = -17;
9393
let h: i64 = -17;
9494

95-
assert_eq!(a, FixedInt::decode_fixed_vec(&a.encode_fixed_vec()));
96-
assert_eq!(b, FixedInt::decode_fixed_vec(&b.encode_fixed_vec()));
97-
assert_eq!(c, FixedInt::decode_fixed_vec(&c.encode_fixed_vec()));
98-
assert_eq!(d, FixedInt::decode_fixed_vec(&d.encode_fixed_vec()));
99-
assert_eq!(e, FixedInt::decode_fixed_vec(&e.encode_fixed_vec()));
100-
assert_eq!(f, FixedInt::decode_fixed_vec(&f.encode_fixed_vec()));
101-
assert_eq!(g, FixedInt::decode_fixed_vec(&g.encode_fixed_vec()));
102-
assert_eq!(h, FixedInt::decode_fixed_vec(&h.encode_fixed_vec()));
103-
104-
assert_eq!(a, FixedInt::decode_fixed(&a.encode_fixed_light()));
105-
assert_eq!(b, FixedInt::decode_fixed(&b.encode_fixed_light()));
106-
assert_eq!(c, FixedInt::decode_fixed(&c.encode_fixed_light()));
107-
assert_eq!(d, FixedInt::decode_fixed(&d.encode_fixed_light()));
108-
assert_eq!(e, FixedInt::decode_fixed(&e.encode_fixed_light()));
109-
assert_eq!(f, FixedInt::decode_fixed(&f.encode_fixed_light()));
110-
assert_eq!(g, FixedInt::decode_fixed(&g.encode_fixed_light()));
111-
assert_eq!(h, FixedInt::decode_fixed(&h.encode_fixed_light()));
95+
assert_eq!(a, FixedInt::decode_fixed(&a.encode_fixed_light()).unwrap());
96+
assert_eq!(b, FixedInt::decode_fixed(&b.encode_fixed_light()).unwrap());
97+
assert_eq!(c, FixedInt::decode_fixed(&c.encode_fixed_light()).unwrap());
98+
assert_eq!(d, FixedInt::decode_fixed(&d.encode_fixed_light()).unwrap());
99+
assert_eq!(e, FixedInt::decode_fixed(&e.encode_fixed_light()).unwrap());
100+
assert_eq!(f, FixedInt::decode_fixed(&f.encode_fixed_light()).unwrap());
101+
assert_eq!(g, FixedInt::decode_fixed(&g.encode_fixed_light()).unwrap());
102+
assert_eq!(h, FixedInt::decode_fixed(&h.encode_fixed_light()).unwrap());
112103
}
113104

114105
#[test]
@@ -141,13 +132,13 @@ mod tests {
141132
#[should_panic]
142133
#[test]
143134
fn test_invalid_decode_size() {
144-
assert_eq!(33, u64::decode_fixed(&[1, 0, 0, 0, 0, 1]));
135+
assert_eq!(33, u64::decode_fixed(&[1, 0, 0, 0, 0, 1]).unwrap());
145136
}
146137
#[should_panic]
147138
#[test]
148139
fn test_invalid_encode_size() {
149140
let mut buf = [0 as u8; 4];
150-
(11 as u64).encode_fixed(&mut buf);
141+
(11 as u64).encode_fixed(&mut buf).unwrap();
151142
}
152143

153144
#[cfg(any(feature = "tokio_async", feature = "futures_async"))]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! fn main() {
1515
//! let a: u32 = 344;
1616
//! let encoded_byte_slice = a.encode_fixed_light();
17-
//! assert_eq!(a, u32::decode_fixed(&encoded_byte_slice));
17+
//! assert_eq!(Some(a), u32::decode_fixed(&encoded_byte_slice));
1818
//! assert_eq!(4, encoded_byte_slice.len());
1919
//!
2020
//! let b: i32 = -111;

src/reader.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,47 +115,35 @@ impl<R: Read> VarIntReader for R {
115115
}
116116

117117
/// A trait for reading FixedInts from any other `Reader`.
118-
///
119-
/// On EOF, an io::Error with io::ErrorKind::UnexpectedEof is returned.
120118
pub trait FixedIntReader {
121-
fn read_le_fixedint<FI: FixedInt>(&mut self) -> Result<FI>;
122-
fn read_be_fixedint<FI: FixedInt>(&mut self) -> Result<FI>;
119+
/// Read a fixed integer from a reader. How many bytes are read depends on `FI`.
120+
///
121+
/// On EOF, an io::Error with io::ErrorKind::UnexpectedEof is returned.
122+
fn read_fixedint<FI: FixedInt>(&mut self) -> Result<FI>;
123123
}
124124

125125
/// Like FixedIntReader, but returns a future.
126126
#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
127127
#[async_trait::async_trait]
128128
pub trait FixedIntAsyncReader {
129-
async fn read_le_fixedint_async<FI: FixedInt>(&mut self) -> Result<FI>;
130-
async fn read_be_fixedint_async<FI: FixedInt>(&mut self) -> Result<FI>;
129+
async fn read_fixedint_async<FI: FixedInt>(&mut self) -> Result<FI>;
131130
}
132131

133132
#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
134133
#[async_trait::async_trait]
135134
impl<AR: AsyncRead + Unpin + Send> FixedIntAsyncReader for AR {
136-
async fn read_le_fixedint_async<FI: FixedInt>(&mut self) -> Result<FI> {
137-
let mut buf = [0 as u8; 8];
138-
self.read_exact(&mut buf[0..std::mem::size_of::<FI>()])
139-
.await?;
140-
Ok(FI::decode_le_fixed(&buf[0..std::mem::size_of::<FI>()]).unwrap())
141-
}
142-
async fn read_be_fixedint_async<FI: FixedInt>(&mut self) -> Result<FI> {
135+
async fn read_fixedint_async<FI: FixedInt>(&mut self) -> Result<FI> {
143136
let mut buf = [0 as u8; 8];
144137
self.read_exact(&mut buf[0..std::mem::size_of::<FI>()])
145138
.await?;
146-
Ok(FI::decode_be_fixed(&buf[0..std::mem::size_of::<FI>()]).unwrap())
139+
Ok(FI::decode_fixed(&buf[0..std::mem::size_of::<FI>()]).unwrap())
147140
}
148141
}
149142

150143
impl<R: Read> FixedIntReader for R {
151-
fn read_le_fixedint<FI: FixedInt>(&mut self) -> Result<FI> {
152-
let mut buf = [0 as u8; 8];
153-
self.read_exact(&mut buf[0..std::mem::size_of::<FI>()])?;
154-
Ok(FI::decode_le_fixed(&buf[0..std::mem::size_of::<FI>()]).unwrap())
155-
}
156-
fn read_be_fixedint<FI: FixedInt>(&mut self) -> Result<FI> {
144+
fn read_fixedint<FI: FixedInt>(&mut self) -> Result<FI> {
157145
let mut buf = [0 as u8; 8];
158146
self.read_exact(&mut buf[0..std::mem::size_of::<FI>()])?;
159-
Ok(FI::decode_be_fixed(&buf[0..std::mem::size_of::<FI>()]).unwrap())
147+
Ok(FI::decode_fixed(&buf[0..std::mem::size_of::<FI>()]).unwrap())
160148
}
161149
}

0 commit comments

Comments
 (0)