embedded_interfaces/packable.rs
1/// A trait for any object that can be packed into and unpacked from an unsigned.
2pub trait UnsignedPackable: Sized {
3 /// The unsigned base type that is used to store this type. Must be [`u8`], [`u16`], [`u32`],
4 /// [`u64`] or [`u128`]. Must be able to hold at least [`Self::BITS`] bits.
5 type Base: Copy;
6 /// The amount of bits which this type occupies in the base unsigned type. The occupied bits
7 /// must always be the least significant N bits.
8 const BITS: usize;
9
10 /// Convert the base unsigned value into an instance of [`Self`]. Only the least significant
11 /// [`Self::BITS`] bits have meaning, all other bits are guaranteed to be zero. This function
12 /// must succeed for any given value.
13 fn from_unsigned(value: Self::Base) -> Self;
14 /// Convert this object into the base unsigned type. Only the least significant
15 /// [`Self::BITS`] bits need to be well defined.
16 fn to_unsigned(&self) -> Self::Base;
17}