MCPcopy Create free account
hub / github.com/dermesser/integer-encoding-rs / FixedInt

Interface FixedInt

src/fixed.rs:9–35  ·  view source on GitHub ↗

`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.

Source from the content-addressed store, hash-verified

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) => {

Callers 7

decode_fFunction · 0.80
test_u32_encFunction · 0.80
test_u16_encFunction · 0.80
test_u8_encFunction · 0.80
test_i8_encFunction · 0.80
test_i16_encFunction · 0.80
test_i32_encFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected