Skip to content

Commit c0eb517

Browse files
committed
Added impl to Id make it more directly usable.
Added MAX_RAW to StandardId and ExtendedId (private).
1 parent 14b1765 commit c0eb517

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

embedded-can/src/id.rs

+64-12
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ impl StandardId {
1010
pub const ZERO: Self = Self(0);
1111

1212
/// CAN ID `0x7FF`, the lowest priority.
13-
pub const MAX: Self = Self(0x7FF);
13+
pub const MAX: Self = Self(Self::MAX_RAW);
14+
15+
/// Raw CAN ID `0x7FF`, the lowest priority.
16+
const MAX_RAW: u16 = 0x7FF;
1417

1518
/// Tries to create a `StandardId` from a raw 16-bit integer.
1619
///
1720
/// This will return `None` if `raw` is out of range of an 11-bit integer (`> 0x7FF`).
1821
#[inline]
1922
#[must_use]
2023
pub const fn new(raw: u16) -> Option<Self> {
21-
if raw <= 0x7FF {
24+
if raw <= Self::MAX_RAW {
2225
Some(Self(raw))
2326
} else {
2427
None
@@ -53,15 +56,18 @@ impl ExtendedId {
5356
pub const ZERO: Self = Self(0);
5457

5558
/// CAN ID `0x1FFFFFFF`, the lowest priority.
56-
pub const MAX: Self = Self(0x1FFF_FFFF);
59+
pub const MAX: Self = Self(Self::MAX_RAW);
60+
61+
/// Raw CAN ID `0x1FFFFFFF`, the lowest priority.
62+
const MAX_RAW: u32 = 0x1FFF_FFFF;
5763

5864
/// Tries to create a `ExtendedId` from a raw 32-bit integer.
5965
///
6066
/// This will return `None` if `raw` is out of range of an 29-bit integer (`> 0x1FFF_FFFF`).
6167
#[inline]
6268
#[must_use]
6369
pub const fn new(raw: u32) -> Option<Self> {
64-
if raw <= 0x1FFF_FFFF {
70+
if raw <= Self::MAX_RAW {
6571
Some(Self(raw))
6672
} else {
6773
None
@@ -104,6 +110,27 @@ pub enum Id {
104110
Extended(ExtendedId),
105111
}
106112

113+
impl Id {
114+
/// Creates a CAN identifier as a standard ID.
115+
pub fn new_standard(raw: u16) -> Option<Self> {
116+
Some(Id::from(StandardId::new(raw)?))
117+
}
118+
119+
/// Creates a CAN identifier as an extended ID.
120+
pub fn new_extended(raw: u32) -> Option<Self> {
121+
Some(Id::from(ExtendedId::new(raw)?))
122+
}
123+
124+
/// Determines if the value is a standard, 11-bit, identifier.
125+
pub fn is_standard(&self) -> bool {
126+
matches!(self, Id::Standard(_))
127+
}
128+
/// Determines if the value is an extended, 29-bit, identifier.
129+
pub fn is_extended(&self) -> bool {
130+
matches!(self, Id::Extended(_))
131+
}
132+
}
133+
107134
/// Implement `Ord` according to the CAN arbitration rules
108135
///
109136
/// When performing arbitration, frames are looked at bit for bit starting
@@ -162,20 +189,17 @@ mod tests {
162189

163190
#[test]
164191
fn standard_id_new() {
165-
assert_eq!(
166-
StandardId::new(StandardId::MAX.as_raw()),
167-
Some(StandardId::MAX)
168-
);
192+
assert_eq!(StandardId::new(StandardId::MAX_RAW), Some(StandardId::MAX));
169193
}
170194

171195
#[test]
172196
fn standard_id_new_out_of_range() {
173-
assert_eq!(StandardId::new(StandardId::MAX.as_raw() + 1), None);
197+
assert_eq!(StandardId::new(StandardId::MAX_RAW + 1), None);
174198
}
175199

176200
#[test]
177201
fn standard_id_new_unchecked_out_of_range() {
178-
let id = StandardId::MAX.as_raw() + 1;
202+
let id = StandardId::MAX_RAW + 1;
179203
assert_eq!(unsafe { StandardId::new_unchecked(id) }, StandardId(id));
180204
}
181205

@@ -189,12 +213,12 @@ mod tests {
189213

190214
#[test]
191215
fn extended_id_new_out_of_range() {
192-
assert_eq!(ExtendedId::new(ExtendedId::MAX.as_raw() + 1), None);
216+
assert_eq!(ExtendedId::new(ExtendedId::MAX_RAW + 1), None);
193217
}
194218

195219
#[test]
196220
fn extended_id_new_unchecked_out_of_range() {
197-
let id = ExtendedId::MAX.as_raw() + 1;
221+
let id = ExtendedId::MAX_RAW + 1;
198222
assert_eq!(unsafe { ExtendedId::new_unchecked(id) }, ExtendedId(id));
199223
}
200224

@@ -216,4 +240,32 @@ mod tests {
216240
assert!(Id::Extended(ExtendedId((1 << 11) - 1)) < Id::Standard(StandardId(1)));
217241
assert!(Id::Standard(StandardId(1)) < Id::Extended(ExtendedId::MAX));
218242
}
243+
244+
#[test]
245+
fn id_new() {
246+
let id = Id::new_standard(StandardId::MAX_RAW).unwrap();
247+
assert!(id.is_standard());
248+
assert!(!id.is_extended());
249+
match id {
250+
Id::Standard(id) => assert_eq!(StandardId::MAX, id),
251+
_ => assert!(false),
252+
}
253+
254+
let id = Id::new_extended(ExtendedId::MAX_RAW).unwrap();
255+
assert!(!id.is_standard());
256+
assert!(id.is_extended());
257+
match id {
258+
Id::Extended(id) => assert_eq!(ExtendedId::MAX, id),
259+
_ => assert!(false),
260+
}
261+
}
262+
263+
#[test]
264+
fn id_raw() {
265+
let id = StandardId::new(StandardId::MAX_RAW).unwrap();
266+
assert_eq!(StandardId::MAX_RAW, id.as_raw());
267+
268+
let id = ExtendedId::new(ExtendedId::MAX_RAW).unwrap();
269+
assert_eq!(ExtendedId::MAX_RAW, id.as_raw());
270+
}
219271
}

0 commit comments

Comments
 (0)