Skip to content

Commit

Permalink
Added impl to Id make it more directly usable.
Browse files Browse the repository at this point in the history
Added MAX_RAW to StandardId and ExtendedId (private).
  • Loading branch information
fpagliughi committed Nov 11, 2024
1 parent 14b1765 commit c0eb517
Showing 1 changed file with 64 additions and 12 deletions.
76 changes: 64 additions & 12 deletions embedded-can/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ impl StandardId {
pub const ZERO: Self = Self(0);

/// CAN ID `0x7FF`, the lowest priority.
pub const MAX: Self = Self(0x7FF);
pub const MAX: Self = Self(Self::MAX_RAW);

/// Raw CAN ID `0x7FF`, the lowest priority.
const MAX_RAW: u16 = 0x7FF;

/// Tries to create a `StandardId` from a raw 16-bit integer.
///
/// This will return `None` if `raw` is out of range of an 11-bit integer (`> 0x7FF`).
#[inline]
#[must_use]
pub const fn new(raw: u16) -> Option<Self> {
if raw <= 0x7FF {
if raw <= Self::MAX_RAW {
Some(Self(raw))
} else {
None
Expand Down Expand Up @@ -53,15 +56,18 @@ impl ExtendedId {
pub const ZERO: Self = Self(0);

/// CAN ID `0x1FFFFFFF`, the lowest priority.
pub const MAX: Self = Self(0x1FFF_FFFF);
pub const MAX: Self = Self(Self::MAX_RAW);

/// Raw CAN ID `0x1FFFFFFF`, the lowest priority.
const MAX_RAW: u32 = 0x1FFF_FFFF;

/// Tries to create a `ExtendedId` from a raw 32-bit integer.
///
/// This will return `None` if `raw` is out of range of an 29-bit integer (`> 0x1FFF_FFFF`).
#[inline]
#[must_use]
pub const fn new(raw: u32) -> Option<Self> {
if raw <= 0x1FFF_FFFF {
if raw <= Self::MAX_RAW {
Some(Self(raw))
} else {
None
Expand Down Expand Up @@ -104,6 +110,27 @@ pub enum Id {
Extended(ExtendedId),
}

impl Id {
/// Creates a CAN identifier as a standard ID.
pub fn new_standard(raw: u16) -> Option<Self> {
Some(Id::from(StandardId::new(raw)?))
}

/// Creates a CAN identifier as an extended ID.
pub fn new_extended(raw: u32) -> Option<Self> {
Some(Id::from(ExtendedId::new(raw)?))
}

/// Determines if the value is a standard, 11-bit, identifier.
pub fn is_standard(&self) -> bool {
matches!(self, Id::Standard(_))
}
/// Determines if the value is an extended, 29-bit, identifier.
pub fn is_extended(&self) -> bool {
matches!(self, Id::Extended(_))
}
}

/// Implement `Ord` according to the CAN arbitration rules
///
/// When performing arbitration, frames are looked at bit for bit starting
Expand Down Expand Up @@ -162,20 +189,17 @@ mod tests {

#[test]
fn standard_id_new() {
assert_eq!(
StandardId::new(StandardId::MAX.as_raw()),
Some(StandardId::MAX)
);
assert_eq!(StandardId::new(StandardId::MAX_RAW), Some(StandardId::MAX));
}

#[test]
fn standard_id_new_out_of_range() {
assert_eq!(StandardId::new(StandardId::MAX.as_raw() + 1), None);
assert_eq!(StandardId::new(StandardId::MAX_RAW + 1), None);
}

#[test]
fn standard_id_new_unchecked_out_of_range() {
let id = StandardId::MAX.as_raw() + 1;
let id = StandardId::MAX_RAW + 1;
assert_eq!(unsafe { StandardId::new_unchecked(id) }, StandardId(id));
}

Expand All @@ -189,12 +213,12 @@ mod tests {

#[test]
fn extended_id_new_out_of_range() {
assert_eq!(ExtendedId::new(ExtendedId::MAX.as_raw() + 1), None);
assert_eq!(ExtendedId::new(ExtendedId::MAX_RAW + 1), None);
}

#[test]
fn extended_id_new_unchecked_out_of_range() {
let id = ExtendedId::MAX.as_raw() + 1;
let id = ExtendedId::MAX_RAW + 1;
assert_eq!(unsafe { ExtendedId::new_unchecked(id) }, ExtendedId(id));
}

Expand All @@ -216,4 +240,32 @@ mod tests {
assert!(Id::Extended(ExtendedId((1 << 11) - 1)) < Id::Standard(StandardId(1)));
assert!(Id::Standard(StandardId(1)) < Id::Extended(ExtendedId::MAX));
}

#[test]
fn id_new() {
let id = Id::new_standard(StandardId::MAX_RAW).unwrap();
assert!(id.is_standard());
assert!(!id.is_extended());
match id {
Id::Standard(id) => assert_eq!(StandardId::MAX, id),
_ => assert!(false),
}

let id = Id::new_extended(ExtendedId::MAX_RAW).unwrap();
assert!(!id.is_standard());
assert!(id.is_extended());
match id {
Id::Extended(id) => assert_eq!(ExtendedId::MAX, id),
_ => assert!(false),
}
}

#[test]
fn id_raw() {
let id = StandardId::new(StandardId::MAX_RAW).unwrap();
assert_eq!(StandardId::MAX_RAW, id.as_raw());

let id = ExtendedId::new(ExtendedId::MAX_RAW).unwrap();
assert_eq!(ExtendedId::MAX_RAW, id.as_raw());
}
}

0 comments on commit c0eb517

Please sign in to comment.