Skip to content

Commit b35d830

Browse files
committed
Rewrote Id trait to make it work with a wider range of representations
1 parent 0a1b584 commit b35d830

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

src/can.rs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
11
//! Controller Area Network
22
3-
use core::convert::TryInto;
4-
use core::convert::TryFrom;
5-
63
use nb;
74

85
/// A type that can either be `BaseId` or `ExtendedId`
96
#[cfg(feature = "unproven")]
10-
pub trait Id: Sized {
11-
/// The BaseId variant
12-
type BaseId: BaseId + Into<Self> + TryFrom<Self>;
7+
pub trait Id {
8+
/// The (11-bit) BaseId variant.
9+
type BaseId;
1310

14-
/// The ExtendedId variant
15-
type ExtendedId: ExtendedId + Into<Self> + TryFrom<Self>;
16-
}
11+
/// The (29-bit) ExtendedId variant.
12+
type ExtendedId;
1713

18-
/// A Can (11-bit) ID
19-
#[cfg(feature = "unproven")]
20-
pub trait BaseId: Sized {
21-
/// A generic ID type that encapsulate this type
22-
type Id: Id + From<Self> + TryInto<Self>;
23-
}
14+
/// Returns `Some(base_id)` if this Can-ID is 11-bit.
15+
/// Returns `None` if this Can-ID is 29-bit.
16+
fn base_id(&self) -> Option<Self::BaseId>;
2417

25-
/// A Can Extended (28-bit) ID
26-
#[cfg(feature = "unproven")]
27-
pub trait ExtendedId: Sized {
28-
/// A generic ID type that encapsulate this type
29-
type Id: Id + From<Self> + TryInto<Self>;
18+
/// Returns `Some(extended_id)` if this Can-ID is 29-bit.
19+
/// Returns `None` if this Can-ID is 11-bit.
20+
fn extended_id(&self) -> Option<Self::ExtendedId>;
3021
}
3122

23+
3224
/// A Can Frame
3325
#[cfg(feature = "unproven")]
3426
pub trait Frame {
3527
/// The Id type of this Frame
3628
type Id: Id;
3729

30+
/// Returns true if this `Frame` is a remote frame
31+
fn is_remote_frame(&self) -> bool;
32+
33+
/// Returns true if this `Frame` is a data frame
34+
fn is_data_frame(&self) -> bool;
35+
36+
/// Returns true if this `Frame` is a extended id frame
37+
fn is_base_id_frame(&self) -> bool {
38+
self.id().base_id().is_some()
39+
}
40+
41+
/// Returns true if this `Frame` is a extended id frame
42+
fn is_extended_id_frame(&self) -> bool {
43+
self.id().extended_id().is_some()
44+
}
45+
3846
/// Returns the Can-ID
3947
fn id(&self) -> Self::Id;
4048

@@ -55,6 +63,22 @@ pub trait FdFrame {
5563
/// Returns false if this frame would/has be(en) transmitted as a "ordinary" Can frame.
5664
fn is_fd_frame(&self) -> bool;
5765

66+
/// Returns true if this `Frame` is a remote frame
67+
fn is_remote_frame(&self) -> bool;
68+
69+
/// Returns true if this `Frame` is a data frame
70+
fn is_data_frame(&self) -> bool;
71+
72+
/// Returns true if this `Frame` is a extended id frame
73+
fn is_base_id_frame(&self) -> bool {
74+
self.id().base_id().is_some()
75+
}
76+
77+
/// Returns true if this `Frame` is a extended id frame
78+
fn is_extended_id_frame(&self) -> bool {
79+
self.id().extended_id().is_some()
80+
}
81+
5882
/// Returns the Can-ID
5983
fn id(&self) -> Self::Id;
6084

0 commit comments

Comments
 (0)