Trait FixedInt

Source
pub trait FixedInt: Sized + Copy {
    type Bytes: AsRef<[u8]>;

    const ENCODED_SIZE: usize = _;

    // Required methods
    fn encode_fixed(self, dst: &mut [u8]) -> Option<()>;
    fn encode_fixed_light(self) -> Self::Bytes;
    fn decode_fixed(src: &[u8]) -> Option<Self>;
    fn switch_endianness(self) -> Self;

    // Provided method
    fn encode_fixed_vec(self) -> Vec<u8>  { ... }
}
Expand description

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.

Provided Associated Constants§

Required Associated Types§

Required Methods§

Source

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Encode a value into the given slice using little-endian. Returns None if dst doesn’t provide enough space to encode this integer.

Use switch_endianness() if machine endianness doesn’t match the desired target encoding.

Source

fn encode_fixed_light(self) -> Self::Bytes

Returns the representation of FixedInt as [Bytes], the little-endian representation of self in the stack.

Source

fn decode_fixed(src: &[u8]) -> Option<Self>

Decode a value from the given slice assuming little-endian. Use switch_endianness() on the returned value if the source was not encoded in little-endian.

Source

fn switch_endianness(self) -> Self

integer-encoding-rs always emits and receives little-endian integers (converting implicitly on big-endian machines). If you receive a big-endian integer, and would like it to be treated correctly, use this helper method to convert between endiannesses.

Provided Methods§

Source

fn encode_fixed_vec(self) -> Vec<u8>

Helper: Encode the value and return a Vec.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl FixedInt for i8

Source§

type Bytes = [u8; 1]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for i16

Source§

type Bytes = [u8; 2]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for i32

Source§

type Bytes = [u8; 4]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for i64

Source§

type Bytes = [u8; 8]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for isize

Source§

type Bytes = [u8; 8]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for u8

Source§

type Bytes = [u8; 1]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for u16

Source§

type Bytes = [u8; 2]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for u32

Source§

type Bytes = [u8; 4]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for u64

Source§

type Bytes = [u8; 8]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Source§

impl FixedInt for usize

Source§

type Bytes = [u8; 8]

Source§

fn encode_fixed(self, dst: &mut [u8]) -> Option<()>

Source§

fn encode_fixed_light(self) -> Self::Bytes

Source§

fn decode_fixed(src: &[u8]) -> Option<Self>

Source§

fn switch_endianness(self) -> Self

Implementors§