Skip to content

Commit 86adcd7

Browse files
bors[bot]eldruin
andauthored
Merge #318
318: CAN improvements r=ryankurte a=eldruin I added a few unit tests and then run clippy, which made me notice some small issues. Most importantly, I changed the return type of the `Frame::new()` and `Frame::new_remote()` to be an `Option` since there is no error type defined for that. This matches the interface of `StandardId::new()` / `ExtendedId::new()`. `@timokroeger` do you expect any other failure reasons in these methods or is having an `Option` fine here? Co-authored-by: Diego Barrios Romero <[email protected]>
2 parents 45d3170 + 007f7cd commit 86adcd7

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

src/can/id.rs

+57
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ impl StandardId {
2424
}
2525

2626
/// 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.
2730
#[inline]
2831
pub const unsafe fn new_unchecked(raw: u16) -> Self {
2932
Self(raw)
@@ -60,6 +63,9 @@ impl ExtendedId {
6063
}
6164

6265
/// 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.
6369
#[inline]
6470
pub const unsafe fn new_unchecked(raw: u32) -> Self {
6571
Self(raw)
@@ -101,3 +107,54 @@ impl From<ExtendedId> for Id {
101107
Id::Extended(id)
102108
}
103109
}
110+
111+
#[cfg(test)]
112+
mod tests {
113+
use super::*;
114+
115+
#[test]
116+
fn standard_id_new() {
117+
assert_eq!(
118+
StandardId::new(StandardId::MAX.as_raw()),
119+
Some(StandardId::MAX)
120+
);
121+
}
122+
123+
#[test]
124+
fn standard_id_new_out_of_range() {
125+
assert_eq!(StandardId::new(StandardId::MAX.as_raw() + 1), None);
126+
}
127+
128+
#[test]
129+
fn standard_id_new_unchecked_out_of_range() {
130+
let id = StandardId::MAX.as_raw() + 1;
131+
assert_eq!(unsafe { StandardId::new_unchecked(id) }, StandardId(id));
132+
}
133+
134+
#[test]
135+
fn extended_id_new() {
136+
assert_eq!(
137+
ExtendedId::new(ExtendedId::MAX.as_raw()),
138+
Some(ExtendedId::MAX)
139+
);
140+
}
141+
142+
#[test]
143+
fn extended_id_new_out_of_range() {
144+
assert_eq!(ExtendedId::new(ExtendedId::MAX.as_raw() + 1), None);
145+
}
146+
147+
#[test]
148+
fn extended_id_new_unchecked_out_of_range() {
149+
let id = ExtendedId::MAX.as_raw() + 1;
150+
assert_eq!(unsafe { ExtendedId::new_unchecked(id) }, ExtendedId(id));
151+
}
152+
153+
#[test]
154+
fn get_standard_id_from_extended_id() {
155+
assert_eq!(
156+
Some(ExtendedId::MAX.standard_id()),
157+
StandardId::new((ExtendedId::MAX.0 >> 18) as u16)
158+
);
159+
}
160+
}

src/can/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ pub use id::*;
1010
/// A CAN2.0 Frame
1111
pub trait Frame: Sized {
1212
/// Creates a new frame.
13-
/// Returns an error when the data slice is too long.
14-
fn new(id: impl Into<Id>, data: &[u8]) -> Result<Self, ()>;
13+
///
14+
/// This will return `None` if the data slice is too long.
15+
fn new(id: impl Into<Id>, data: &[u8]) -> Option<Self>;
1516

1617
/// Creates a new remote frame (RTR bit set).
17-
/// Returns an error when the data length code (DLC) is not valid.
18-
fn new_remote(id: impl Into<Id>, dlc: usize) -> Result<Self, ()>;
18+
///
19+
/// This will return `None` if the data length code (DLC) is not valid.
20+
fn new_remote(id: impl Into<Id>, dlc: usize) -> Option<Self>;
1921

2022
/// Returns true if this frame is a extended frame.
2123
fn is_extended(&self) -> bool;

0 commit comments

Comments
 (0)