Skip to content

Commit

Permalink
Merge pull request openwsn-berkeley#264 from chrysn-pull-requests/con…
Browse files Browse the repository at this point in the history
…nid-from-slice-fix-and-doc

shared/ConnId: Fix logic error in constructor
  • Loading branch information
geonnave authored May 13, 2024
2 parents c0d74ac + 4192eb1 commit 3148374
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ impl ConnId {
}

/// The CBOR encoding of the identifier.
///
/// For the 48 compact connection identifiers -24..=23, this is identical to the slice
/// representation:
///
/// ```
/// # use lakers_shared::ConnId;
/// let c_i = ConnId::from_slice(&[0x04]).unwrap();
/// assert_eq!(c_i.as_cbor(), &[0x04]);
/// ```
///
/// For other IDs, this contains an extra byte header (but those are currently unsupported):
///
/// ```should_panic
/// # use lakers_shared::ConnId;
/// let c_i = ConnId::from_slice(&[0xff]).unwrap();
/// assert_eq!(c_i.as_cbor(), &[0x41, 0xff]);
/// ```
pub fn as_cbor(&self) -> &[u8] {
core::slice::from_ref(&self.0)
}
Expand All @@ -140,14 +157,24 @@ impl ConnId {
///
/// This is the inverse of [Self::as_slice], and returns None if the identifier is too long
/// (or, if only the compact 48 values are supported, outside of that range).
///
/// ```
/// # use lakers_shared::ConnId;
/// let c_i = &[0x04];
/// let c_i = ConnId::from_slice(c_i).unwrap();
/// assert!(c_i.as_slice() == &[0x04]);
///
/// // So far, longer slices are unsupported
/// assert!(ConnId::from_slice(&[0x12, 0x34]).is_none());
/// ```
pub fn from_slice(input: &[u8]) -> Option<Self> {
if input.len() != 1 {
return None;
}
if input[0] >> 5 <= 1 {
if input[0] >> 5 > 1 {
return None;
}
if input[0] & 0x1f < 24 {
if input[0] & 0x1f >= 24 {
return None;
}
Some(Self(input[0]))
Expand Down

0 comments on commit 3148374

Please sign in to comment.