1//! CAN Identifiers.
23/// Standard 11-bit CAN Identifier (`0..=0x7FF`).
4#[derive(Debug, Copy, Clone, Eq, PartialEq)]
5pub struct StandardId(u16);
67impl StandardId {
8/// CAN ID `0`, the highest priority.
9pub const ZERO: Self = StandardId(0);
1011/// CAN ID `0x7FF`, the lowest priority.
12pub const MAX: Self = StandardId(0x7FF);
1314/// Tries to create a `StandardId` from a raw 16-bit integer.
15 ///
16 /// This will return `None` if `raw` is out of range of an 11-bit integer (`> 0x7FF`).
17#[inline]
18pub fn new(raw: u16) -> Option<Self> {
19if raw <= 0x7FF {
20Some(StandardId(raw))
21 } else {
22None
23}
24 }
2526/// Creates a new `StandardId` without checking if it is inside the valid range.
27 ///
28 /// # Safety
29 /// Using this method can create an invalid ID and is thus marked as unsafe.
30#[inline]
31pub const unsafe fn new_unchecked(raw: u16) -> Self {
32 StandardId(raw)
33 }
3435/// Returns this CAN Identifier as a raw 16-bit integer.
36#[inline]
37pub fn as_raw(&self) -> u16 {
38self.0
39}
40}
4142/// Extended 29-bit CAN Identifier (`0..=1FFF_FFFF`).
43#[derive(Debug, Copy, Clone, Eq, PartialEq)]
44pub struct ExtendedId(u32);
4546impl ExtendedId {
47/// CAN ID `0`, the highest priority.
48pub const ZERO: Self = ExtendedId(0);
4950/// CAN ID `0x1FFFFFFF`, the lowest priority.
51pub const MAX: Self = ExtendedId(0x1FFF_FFFF);
5253/// Tries to create a `ExtendedId` from a raw 32-bit integer.
54 ///
55 /// This will return `None` if `raw` is out of range of an 29-bit integer (`> 0x1FFF_FFFF`).
56#[inline]
57pub fn new(raw: u32) -> Option<Self> {
58if raw <= 0x1FFF_FFFF {
59Some(ExtendedId(raw))
60 } else {
61None
62}
63 }
6465/// Creates a new `ExtendedId` without checking if it is inside the valid range.
66 ///
67 /// # Safety
68 /// Using this method can create an invalid ID and is thus marked as unsafe.
69#[inline]
70pub const unsafe fn new_unchecked(raw: u32) -> Self {
71 ExtendedId(raw)
72 }
7374/// Returns this CAN Identifier as a raw 32-bit integer.
75#[inline]
76pub fn as_raw(&self) -> u32 {
77self.0
78}
7980/// Returns the Base ID part of this extended identifier.
81pub fn standard_id(&self) -> StandardId {
82// ID-28 to ID-18
83StandardId((self.0 >> 18) as u16)
84 }
85}
8687/// A CAN Identifier (standard or extended).
88#[derive(Debug, Copy, Clone, Eq, PartialEq)]
89pub enum Id {
90/// Standard 11-bit Identifier (`0..=0x7FF`).
91Standard(StandardId),
9293/// Extended 29-bit Identifier (`0..=0x1FFF_FFFF`).
94Extended(ExtendedId),
95}
9697impl From<StandardId> for Id {
98#[inline]
99fn from(id: StandardId) -> Self {
100 Id::Standard(id)
101 }
102}
103104impl From<ExtendedId> for Id {
105#[inline]
106fn from(id: ExtendedId) -> Self {
107 Id::Extended(id)
108 }
109}
110111#[cfg(test)]
112mod tests {
113use super::*;
114115#[test]
116fn standard_id_new() {
117assert_eq!(
118 StandardId::new(StandardId::MAX.as_raw()),
119Some(StandardId::MAX)
120 );
121 }
122123#[test]
124fn standard_id_new_out_of_range() {
125assert_eq!(StandardId::new(StandardId::MAX.as_raw() + 1), None);
126 }
127128#[test]
129fn standard_id_new_unchecked_out_of_range() {
130let id = StandardId::MAX.as_raw() + 1;
131assert_eq!(unsafe { StandardId::new_unchecked(id) }, StandardId(id));
132 }
133134#[test]
135fn extended_id_new() {
136assert_eq!(
137 ExtendedId::new(ExtendedId::MAX.as_raw()),
138Some(ExtendedId::MAX)
139 );
140 }
141142#[test]
143fn extended_id_new_out_of_range() {
144assert_eq!(ExtendedId::new(ExtendedId::MAX.as_raw() + 1), None);
145 }
146147#[test]
148fn extended_id_new_unchecked_out_of_range() {
149let id = ExtendedId::MAX.as_raw() + 1;
150assert_eq!(unsafe { ExtendedId::new_unchecked(id) }, ExtendedId(id));
151 }
152153#[test]
154fn get_standard_id_from_extended_id() {
155assert_eq!(
156Some(ExtendedId::MAX.standard_id()),
157 StandardId::new((ExtendedId::MAX.0 >> 18) as u16)
158 );
159 }
160}