Skip to content

Commit 1646f05

Browse files
committed
Added impl to Id make it more directly usable. Added MAX_RAW to StandardId and ExtendedId.
1 parent 14b1765 commit 1646f05

File tree

1 file changed

+80
-9
lines changed

1 file changed

+80
-9
lines changed

embedded-can/src/id.rs

+80-9
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+
pub 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+
pub 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,37 @@ 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_id(raw: u16) -> Option<Self> {
116+
let id = StandardId::new(raw)?;
117+
Some(Id::Standard(id))
118+
}
119+
120+
/// Creates a CAN identifier as an extended ID.
121+
pub fn new_extended_id(raw: u32) -> Option<Self> {
122+
let id = ExtendedId::new(raw)?;
123+
Some(Id::Extended(id))
124+
}
125+
126+
/// Returns this CAN Identifier as a raw 32-bit integer, regardless of whether it's
127+
/// a standard or extended identifier.
128+
pub fn as_raw(&self) -> u32 {
129+
match self {
130+
Id::Standard(id) => id.as_raw() as u32,
131+
Id::Extended(id) => id.as_raw(),
132+
}
133+
}
134+
135+
/// Determines if the value is an extended identifier.
136+
pub fn is_extended(&self) -> bool {
137+
match self {
138+
Id::Extended(_) => true,
139+
_ => false
140+
}
141+
}
142+
}
143+
107144
/// Implement `Ord` according to the CAN arbitration rules
108145
///
109146
/// When performing arbitration, frames are looked at bit for bit starting
@@ -163,19 +200,19 @@ mod tests {
163200
#[test]
164201
fn standard_id_new() {
165202
assert_eq!(
166-
StandardId::new(StandardId::MAX.as_raw()),
203+
StandardId::new(StandardId::MAX_RAW),
167204
Some(StandardId::MAX)
168205
);
169206
}
170207

171208
#[test]
172209
fn standard_id_new_out_of_range() {
173-
assert_eq!(StandardId::new(StandardId::MAX.as_raw() + 1), None);
210+
assert_eq!(StandardId::new(StandardId::MAX_RAW + 1), None);
174211
}
175212

176213
#[test]
177214
fn standard_id_new_unchecked_out_of_range() {
178-
let id = StandardId::MAX.as_raw() + 1;
215+
let id = StandardId::MAX_RAW + 1;
179216
assert_eq!(unsafe { StandardId::new_unchecked(id) }, StandardId(id));
180217
}
181218

@@ -189,12 +226,12 @@ mod tests {
189226

190227
#[test]
191228
fn extended_id_new_out_of_range() {
192-
assert_eq!(ExtendedId::new(ExtendedId::MAX.as_raw() + 1), None);
229+
assert_eq!(ExtendedId::new(ExtendedId::MAX_RAW + 1), None);
193230
}
194231

195232
#[test]
196233
fn extended_id_new_unchecked_out_of_range() {
197-
let id = ExtendedId::MAX.as_raw() + 1;
234+
let id = ExtendedId::MAX_RAW + 1;
198235
assert_eq!(unsafe { ExtendedId::new_unchecked(id) }, ExtendedId(id));
199236
}
200237

@@ -216,4 +253,38 @@ mod tests {
216253
assert!(Id::Extended(ExtendedId((1 << 11) - 1)) < Id::Standard(StandardId(1)));
217254
assert!(Id::Standard(StandardId(1)) < Id::Extended(ExtendedId::MAX));
218255
}
256+
257+
#[test]
258+
fn id_new() {
259+
let id = Id::new_standard_id(StandardId::MAX_RAW);
260+
match id {
261+
Some(Id::Standard(id)) => assert_eq!(StandardId::MAX, id),
262+
_ => assert!(false),
263+
}
264+
265+
let id = Id::new_extended_id(ExtendedId::MAX_RAW);
266+
match id {
267+
Some(Id::Extended(id)) => assert_eq!(ExtendedId::MAX, id),
268+
_ => assert!(false),
269+
}
270+
}
271+
272+
#[test]
273+
fn id_raw() {
274+
const RAW_ID: u32 = StandardId::MAX_RAW as u32;
275+
276+
let id = StandardId::new(RAW_ID as u16).unwrap();
277+
assert_eq!(RAW_ID as u16, id.as_raw());
278+
279+
let id = Id::from(id);
280+
assert!(!id.is_extended());
281+
assert_eq!(RAW_ID, id.as_raw());
282+
283+
let id = ExtendedId::new(RAW_ID).unwrap();
284+
assert_eq!(RAW_ID, id.as_raw());
285+
286+
let id = Id::from(id);
287+
assert!(id.is_extended());
288+
assert_eq!(RAW_ID, id.as_raw());
289+
}
219290
}

0 commit comments

Comments
 (0)