pub struct ID(/* private fields */);
Expand description
An identifier for an HLC (MAX_SIZE bytes maximum).
This struct has a constant memory size (holding internally a NonZeroU8
),
allowing allocations on the stack for better performances.
§Examples
use std::convert::TryFrom;
use uhlc::ID;
let buf = [0x1a, 0x2b, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
// NOTE: ID::try_from(slice: &[u8]) assumes the slice is in little endian
let id1 = ID::try_from(&buf[..3]).unwrap();
assert_eq!(id1.size(), 3);
assert_eq!(id1.to_le_bytes(), buf);
assert_eq!(&id1.to_le_bytes()[..id1.size()], &[0x1a, 0x2b, 0x3c]);
let id2: ID = "3c2b1a".parse().unwrap();
assert_eq!(id2.size(), 3);
assert_eq!(id2.to_le_bytes(), buf);
assert_eq!(&id2.to_le_bytes()[..id2.size()], &[0x1a, 0x2b, 0x3c]);
assert_eq!(id2.to_string(), "3c2b1a");
assert_eq!(id1, id2);
use uhlc::ID;
let id = ID::rand();
assert!(id.size() <= 16);
Implementations§
source§impl ID
impl ID
sourcepub fn to_le_bytes(&self) -> [u8; 16]
pub fn to_le_bytes(&self) -> [u8; 16]
This ID as bytes
If you want to retrive a le-encoded slice of the ID
, you can do it as follows:
use uhlc::ID;
use std::convert::TryFrom;
let id = ID::try_from(&[0x01]).unwrap();
let slice = &id.to_le_bytes()[..id.size()];
assert_eq!(1, slice.len());
assert_eq!(&[0x01], slice);
Trait Implementations§
source§impl<'de> Deserialize<'de> for ID
impl<'de> Deserialize<'de> for ID
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl From<NonZero<u128>> for ID
impl From<NonZero<u128>> for ID
source§fn from(id: NonZeroU128) -> Self
fn from(id: NonZeroU128) -> Self
Converts to this type from the input type.
source§impl From<NonZero<u16>> for ID
impl From<NonZero<u16>> for ID
source§fn from(id: NonZeroU16) -> Self
fn from(id: NonZeroU16) -> Self
Converts to this type from the input type.
source§impl From<NonZero<u32>> for ID
impl From<NonZero<u32>> for ID
source§fn from(id: NonZeroU32) -> Self
fn from(id: NonZeroU32) -> Self
Converts to this type from the input type.
source§impl From<NonZero<u64>> for ID
impl From<NonZero<u64>> for ID
source§fn from(id: NonZeroU64) -> Self
fn from(id: NonZeroU64) -> Self
Converts to this type from the input type.
source§impl Ord for ID
impl Ord for ID
source§impl PartialOrd for ID
impl PartialOrd for ID
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl Copy for ID
impl Eq for ID
impl StructuralPartialEq for ID
Auto Trait Implementations§
impl Freeze for ID
impl RefUnwindSafe for ID
impl Send for ID
impl Sync for ID
impl Unpin for ID
impl UnwindSafe for ID
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)