Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added impl to Id make it more directly usable. #428

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 56 additions & 9 deletions embedded-can/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ 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.
pub 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]
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 @@ -48,14 +51,17 @@ 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.
pub const MAX_RAW: u32 = 0x1FFF_FFFF;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep MAX_RAW private and make as_raw const instead.


/// 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]
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 @@ -94,6 +100,23 @@ pub enum Id {
Extended(ExtendedId),
}

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

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

/// Determines if the value is an extended 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 @@ -153,19 +176,19 @@ mod tests {
#[test]
fn standard_id_new() {
assert_eq!(
StandardId::new(StandardId::MAX.as_raw()),
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 @@ -179,12 +202,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 @@ -206,4 +229,28 @@ 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_id(StandardId::MAX_RAW);
match id {
Some(Id::Standard(id)) => assert_eq!(StandardId::MAX, id),
_ => assert!(false),
}

let id = Id::new_extended_id(ExtendedId::MAX_RAW);
match id {
Some(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());
}
}