`FixedInt` provides encoding/decoding to and from fixed int representations. Note that current Rust versions already provide this functionality via the `to_le_bytes()` and `to_be_bytes()` methods. The emitted bytestring contains the bytes of the integer in machine endianness.
| 7 | /// |
| 8 | /// The emitted bytestring contains the bytes of the integer in machine endianness. |
| 9 | pub 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 | |
| 37 | macro_rules! impl_fixedint { |
| 38 | ($t:ty) => { |
no outgoing calls
no test coverage detected