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 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}