pub struct NodeId(/* private fields */);Expand description
A unique 48-bit OpenLCB node identifier, e.g. 05.01.01.01.22.00.
The value is stored in the low 48 bits of a u64; the constructor
guarantees the upper 16 bits are always zero. Display and FromStr
use the dotted-hex form, Debug prints NodeId(05.01.01.01.22.00).
With the serde feature it (de)serializes as the dotted-hex string.
The all-zero ID (NodeId::default()) is not a valid node and is used
by this crate as a “not known” placeholder in a few lenient decoders.
§Conversions
| From | To | How | Out-of-range input |
|---|---|---|---|
u64 | NodeId | NodeId::new | silently masked to 48 bits |
u64 | NodeId | NodeId::checked | None |
u64 | NodeId | NodeId::try_from(u64) | Err(ParseError::Length) |
[u8; 6] | NodeId | NodeId::from([u8; 6]) | (cannot fail) |
&[u8] | NodeId | NodeId::from_bytes | Err if shorter than 6 bytes |
NodeId | u64 | u64::from(id), NodeId::as_u64 | |
NodeId | [u8; 6] | NodeId::to_bytes |
use rolcb::NodeId;
let id = NodeId::new(0x0501_0101_2200);
assert_eq!(NodeId::from_bytes(&id.to_bytes())?, id);
assert_eq!(NodeId::from([0x05, 0x01, 0x01, 0x01, 0x22, 0x00]), id);
assert_eq!(u64::from(id), 0x0501_0101_2200);
// `new` DROPS bits above 48 without telling you…
assert_eq!(NodeId::new(0xFFFF_0501_0101_2200), id);
// …so prefer `checked` / `try_from` for values from outside the program.
assert_eq!(NodeId::checked(0xFFFF_0501_0101_2200), None);
assert!(NodeId::try_from(0x1_0000_0000_0000_u64).is_err());
assert_eq!(NodeId::try_from(0x0501_0101_2200_u64)?, id);Implementations§
Source§impl NodeId
impl NodeId
Sourcepub const MAX: u64 = 0xFFFF_FFFF_FFFF
pub const MAX: u64 = 0xFFFF_FFFF_FFFF
The largest representable Node ID value, 0xFFFF_FFFF_FFFF (48 bits).
Sourcepub const fn new(v: u64) -> Self
pub const fn new(v: u64) -> Self
Create from the low 48 bits of v.
Bits 48-63 are silently discarded: NodeId::new(0x1_0000_0000_0001)
is 00.00.00.00.00.01. That is convenient for literals, but for
values read from a file, a UI or another program use
NodeId::checked or NodeId::try_from(u64), which reject values
above NodeId::MAX instead of truncating them.
Sourcepub const fn checked(v: u64) -> Option<Self>
pub const fn checked(v: u64) -> Option<Self>
Create from v if it fits in 48 bits, otherwise None.
use rolcb::NodeId;
assert_eq!(NodeId::checked(NodeId::MAX).map(|n| n.as_u64()), Some(NodeId::MAX));
assert_eq!(NodeId::checked(NodeId::MAX + 1), None);Sourcepub const fn as_u64(self) -> u64
pub const fn as_u64(self) -> u64
The Node ID as an integer (always <= NodeId::MAX).
Sourcepub fn to_bytes(self) -> [u8; 6]
pub fn to_bytes(self) -> [u8; 6]
The six big-endian bytes, as transmitted on the wire (for example in the data field of an AMD frame or a Verified Node ID message).
Sourcepub fn from_bytes(b: &[u8]) -> Result<Self, ParseError>
pub fn from_bytes(b: &[u8]) -> Result<Self, ParseError>
Read a Node ID from the first six big-endian bytes of b. Extra
trailing bytes are ignored.
§Errors
ParseError::Length if b is shorter than six bytes.
Trait Implementations§
impl Copy for NodeId
Source§impl<'de> Deserialize<'de> for NodeId
Available on crate feature serde only.
impl<'de> Deserialize<'de> for NodeId
serde only.