integer_encoding/fixed.rs
1use std::convert::TryInto;
2use std::mem::size_of;
3
4/// `FixedInt` provides encoding/decoding to and from fixed int representations. Note that current
5/// Rust versions already provide this functionality via the `to_le_bytes()` and `to_be_bytes()`
6/// methods.
7///
8/// The emitted bytestring contains the bytes of the integer in machine endianness.
9pub trait FixedInt: Sized + Copy {
10 type Bytes: AsRef<[u8]>;
11 const ENCODED_SIZE: usize = size_of::<Self>();
12
13 /// Encode a value into the given slice using little-endian. Returns `None` if `dst`
14 /// doesn't provide enough space to encode this integer.
15 ///
16 /// Use `switch_endianness()` if machine endianness doesn't match the desired target encoding.
17 fn encode_fixed(self, dst: &mut [u8]) -> Option<()>;
18 /// Returns the representation of [`FixedInt`] as [`Bytes`], the little-endian representation
19 /// of self in the stack.
20 fn encode_fixed_light(self) -> Self::Bytes;
21
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>;
25
26 /// Helper: Encode the value and return a Vec.
27 fn encode_fixed_vec(self) -> Vec<u8> {
28 self.encode_fixed_light().as_ref().to_vec()
29 }
30
31 /// integer-encoding-rs always emits and receives little-endian integers (converting implicitly
32 /// on big-endian machines). If you receive a big-endian integer, and would like it to be
33 /// treated correctly, use this helper method to convert between endiannesses.
34 fn switch_endianness(self) -> Self;
35}
36
37macro_rules! impl_fixedint {
38 ($t:ty) => {
39 impl FixedInt for $t {
40 type Bytes = [u8; size_of::<$t>()];
41
42 fn encode_fixed(self, dst: &mut [u8]) -> Option<()> {
43 if dst.len() == size_of::<Self>() {
44 dst.clone_from_slice(&self.to_le_bytes());
45 Some(())
46 } else {
47 None
48 }
49 }
50
51 fn encode_fixed_light(self) -> Self::Bytes {
52 self.to_le_bytes()
53 }
54
55 fn decode_fixed(src: &[u8]) -> Option<Self> {
56 if src.len() == size_of::<Self>() {
57 Some(Self::from_le_bytes(src.try_into().unwrap()))
58 } else {
59 None
60 }
61 }
62
63 fn switch_endianness(self) -> Self {
64 Self::from_le_bytes(self.to_be_bytes())
65 }
66 }
67 };
68}
69
70impl_fixedint!(usize);
71impl_fixedint!(u64);
72impl_fixedint!(u32);
73impl_fixedint!(u16);
74impl_fixedint!(u8);
75impl_fixedint!(isize);
76impl_fixedint!(i64);
77impl_fixedint!(i32);
78impl_fixedint!(i16);
79impl_fixedint!(i8);