Skip to content

Commit 016d524

Browse files
committed
Added impl to Id make it more directly usable. Added MAX_RAW to StandardId and ExtendedId.
1 parent 812471b commit 016d524

File tree

1 file changed

+80
-9
lines changed

1 file changed

+80
-9
lines changed

embedded-can/src/id.rs

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ impl StandardId {
99
pub const ZERO: Self = Self(0);
1010

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

1417
/// Tries to create a `StandardId` from a raw 16-bit integer.
1518
///
1619
/// This will return `None` if `raw` is out of range of an 11-bit integer (`> 0x7FF`).
1720
#[inline]
1821
pub const fn new(raw: u16) -> Option<Self> {
19-
if raw <= 0x7FF {
22+
if raw <= Self::MAX_RAW {
2023
Some(Self(raw))
2124
} else {
2225
None
@@ -48,14 +51,17 @@ impl ExtendedId {
4851
pub const ZERO: Self = Self(0);
4952

5053
/// CAN ID `0x1FFFFFFF`, the lowest priority.
51-
pub const MAX: Self = Self(0x1FFF_FFFF);
54+
pub const MAX: Self = Self(Self::MAX_RAW);
55+
56+
/// Raw CAN ID `0x1FFFFFFF`, the lowest priority.
57+
pub const MAX_RAW: u32 = 0x1FFF_FFFF;
5258

5359
/// Tries to create a `ExtendedId` from a raw 32-bit integer.
5460
///
5561
/// This will return `None` if `raw` is out of range of an 29-bit integer (`> 0x1FFF_FFFF`).
5662
#[inline]
5763
pub const fn new(raw: u32) -> Option<Self> {
58-
if raw <= 0x1FFF_FFFF {
64+
if raw <= Self::MAX_RAW {
5965
Some(Self(raw))
6066
} else {
6167
None
@@ -94,6 +100,37 @@ pub enum Id {
94100
Extended(ExtendedId),
95101
}
96102

103+
impl Id {
104+
/// Creates a CAN identifier as a standard ID.
105+
pub fn new_standard_id(raw: u16) -> Option<Self> {
106+
let id = StandardId::new(raw)?;
107+
Some(Id::Standard(id))
108+
}
109+
110+
/// Creates a CAN identifier as an extended ID.
111+
pub fn new_extended_id(raw: u32) -> Option<Self> {
112+
let id = ExtendedId::new(raw)?;
113+
Some(Id::Extended(id))
114+
}
115+
116+
/// Returns this CAN Identifier as a raw 32-bit integer, regardless of whether it's
117+
/// a standard or extended identifier.
118+
pub fn as_raw(&self) -> u32 {
119+
match self {
120+
Id::Standard(id) => id.as_raw() as u32,
121+
Id::Extended(id) => id.as_raw(),
122+
}
123+
}
124+
125+
/// Determines if the value is an extended identifier.
126+
pub fn is_extended(&self) -> bool {
127+
match self {
128+
Id::Extended(_) => true,
129+
_ => false
130+
}
131+
}
132+
}
133+
97134
/// Implement `Ord` according to the CAN arbitration rules
98135
///
99136
/// When performing arbitration, frames are looked at bit for bit starting
@@ -153,19 +190,19 @@ mod tests {
153190
#[test]
154191
fn standard_id_new() {
155192
assert_eq!(
156-
StandardId::new(StandardId::MAX.as_raw()),
193+
StandardId::new(StandardId::MAX_RAW),
157194
Some(StandardId::MAX)
158195
);
159196
}
160197

161198
#[test]
162199
fn standard_id_new_out_of_range() {
163-
assert_eq!(StandardId::new(StandardId::MAX.as_raw() + 1), None);
200+
assert_eq!(StandardId::new(StandardId::MAX_RAW + 1), None);
164201
}
165202

166203
#[test]
167204
fn standard_id_new_unchecked_out_of_range() {
168-
let id = StandardId::MAX.as_raw() + 1;
205+
let id = StandardId::MAX_RAW + 1;
169206
assert_eq!(unsafe { StandardId::new_unchecked(id) }, StandardId(id));
170207
}
171208

@@ -179,12 +216,12 @@ mod tests {
179216

180217
#[test]
181218
fn extended_id_new_out_of_range() {
182-
assert_eq!(ExtendedId::new(ExtendedId::MAX.as_raw() + 1), None);
219+
assert_eq!(ExtendedId::new(ExtendedId::MAX_RAW + 1), None);
183220
}
184221

185222
#[test]
186223
fn extended_id_new_unchecked_out_of_range() {
187-
let id = ExtendedId::MAX.as_raw() + 1;
224+
let id = ExtendedId::MAX_RAW + 1;
188225
assert_eq!(unsafe { ExtendedId::new_unchecked(id) }, ExtendedId(id));
189226
}
190227

@@ -206,4 +243,38 @@ mod tests {
206243
assert!(Id::Extended(ExtendedId((1 << 11) - 1)) < Id::Standard(StandardId(1)));
207244
assert!(Id::Standard(StandardId(1)) < Id::Extended(ExtendedId::MAX));
208245
}
246+
247+
#[test]
248+
fn id_new() {
249+
let id = Id::new_standard_id(StandardId::MAX_RAW);
250+
match id {
251+
Some(Id::Standard(id)) => assert_eq!(StandardId::MAX, id),
252+
_ => assert!(false),
253+
}
254+
255+
let id = Id::new_extended_id(ExtendedId::MAX_RAW);
256+
match id {
257+
Some(Id::Extended(id)) => assert_eq!(ExtendedId::MAX, id),
258+
_ => assert!(false),
259+
}
260+
}
261+
262+
#[test]
263+
fn id_raw() {
264+
const RAW_ID: u32 = StandardId::MAX_RAW as u32;
265+
266+
let id = StandardId::new(RAW_ID as u16).unwrap();
267+
assert_eq!(RAW_ID as u16, id.as_raw());
268+
269+
let id = Id::from(id);
270+
assert!(!id.is_extended());
271+
assert_eq!(RAW_ID, id.as_raw());
272+
273+
let id = ExtendedId::new(RAW_ID).unwrap();
274+
assert_eq!(RAW_ID, id.as_raw());
275+
276+
let id = Id::from(id);
277+
assert!(id.is_extended());
278+
assert_eq!(RAW_ID, id.as_raw());
279+
}
209280
}

0 commit comments

Comments
 (0)