Skip to content

Commit

Permalink
filter: allow method chaining (#10)
Browse files Browse the repository at this point in the history
* filter: allow method chaining

* fix return types

* update tests to use method chaining
  • Loading branch information
xoviat authored Jan 13, 2021
1 parent ad34859 commit 1d6f176
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
18 changes: 12 additions & 6 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,23 @@ impl<I: FilterOwner> MasterFilters<'_, I> {
/// Disables all enabled filter banks.
///
/// This causes all incoming frames to be disposed.
pub fn clear(&mut self) {
pub fn clear(&mut self) -> &mut Self {
self.banks_imm().clear();
self
}

/// Disables a filter bank.
///
/// If `index` is out of bounds, this will panic.
pub fn disable_bank(&mut self, index: u8) {
pub fn disable_bank(&mut self, index: u8) -> &mut Self {
self.banks_imm().disable(index);
self
}

/// Configures a filter bank according to `config` and enables it.
pub fn enable_bank(&mut self, index: u8, config: impl Into<BankConfig>) {
pub fn enable_bank(&mut self, index: u8, config: impl Into<BankConfig>) -> &mut Self {
self.banks_imm().enable(index, config.into());
self
}
}

Expand Down Expand Up @@ -327,20 +330,23 @@ impl<I: Instance> SlaveFilters<'_, I> {
/// Disables all enabled filter banks.
///
/// This causes all incoming frames to be disposed.
pub fn clear(&mut self) {
pub fn clear(&mut self) -> &mut Self {
self.banks_imm().clear();
self
}

/// Disables a filter bank.
///
/// If `index` is out of bounds, this will panic.
pub fn disable_bank(&mut self, index: u8) {
pub fn disable_bank(&mut self, index: u8) -> &mut Self {
self.banks_imm().disable(index);
self
}

/// Configures a filter bank according to `config` and enables it.
pub fn enable_bank(&mut self, index: u8, config: impl Into<BankConfig>) {
pub fn enable_bank(&mut self, index: u8, config: impl Into<BankConfig>) -> &mut Self {
self.banks_imm().enable(index, config.into());
self
}
}

Expand Down
55 changes: 24 additions & 31 deletions testsuite/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ mod tests {
fn init() -> State {
let mut state = State::init();

let mut filt = state.can1.modify_filters();
filt.clear();
drop(filt);
state.can1.modify_filters().clear();
nb::block!(state.can1.enable()).unwrap();

state
Expand Down Expand Up @@ -116,10 +114,11 @@ mod tests {

#[test]
fn basic_roundtrip(state: &mut State) {
let mut filt = state.can1.modify_filters();
filt.clear();
filt.enable_bank(0, Mask32::accept_all());
drop(filt);
state
.can1
.modify_filters()
.clear()
.enable_bank(0, Mask32::accept_all());

let frame = Frame::new_data(StandardId::new(0).unwrap(), []);
defmt::assert!(roundtrip_frame(&frame, state));
Expand All @@ -143,10 +142,11 @@ mod tests {
let target_id = StandardId::new(42).unwrap();
let mask = StandardId::MAX; // Exact match required

let mut filt = state.can1.modify_filters();
filt.clear();
filt.enable_bank(0, Mask32::frames_with_std_id(target_id, mask));
drop(filt);
state
.can1
.modify_filters()
.clear()
.enable_bank(0, Mask32::frames_with_std_id(target_id, mask));

// Data frames with matching IDs should be accepted.
let frame = Frame::new_data(target_id, []);
Expand Down Expand Up @@ -186,10 +186,11 @@ mod tests {
let target_id = ExtendedId::new(0).unwrap();
let mask = ExtendedId::MAX; // Exact match required

let mut filt = state.can1.modify_filters();
filt.clear();
filt.enable_bank(0, Mask32::frames_with_ext_id(target_id, mask));
drop(filt);
state
.can1
.modify_filters()
.clear()
.enable_bank(0, Mask32::frames_with_ext_id(target_id, mask));

// Data frames with matching IDs should be accepted.
let frame = Frame::new_data(target_id, []);
Expand Down Expand Up @@ -227,16 +228,13 @@ mod tests {
let target_id_2 = StandardId::new(17).unwrap();
let mask = StandardId::MAX; // Exact match required

let mut filt = state.can1.modify_filters();
filt.clear();
filt.enable_bank(
state.can1.modify_filters().clear().enable_bank(
0,
[
Mask16::frames_with_std_id(target_id_1, mask),
Mask16::frames_with_std_id(target_id_2, mask),
],
);
drop(filt);

// Data frames with matching IDs should be accepted.
let frame = Frame::new_data(target_id_1, []);
Expand Down Expand Up @@ -269,16 +267,13 @@ mod tests {
let target_id_1 = StandardId::MAX;
let target_id_2 = StandardId::new(42).unwrap();

let mut filt = state.can1.modify_filters();
filt.clear();
filt.enable_bank(
state.can1.modify_filters().clear().enable_bank(
0,
[
ListEntry32::data_frames_with_id(target_id_1),
ListEntry32::remote_frames_with_id(target_id_2),
],
);
drop(filt);

// Frames with matching IDs should be accepted.
let frame = Frame::new_data(target_id_1, []);
Expand All @@ -305,16 +300,13 @@ mod tests {
let target_id_1 = ExtendedId::MAX;
let target_id_2 = ExtendedId::new(42).unwrap();

let mut filt = state.can1.modify_filters();
filt.clear();
filt.enable_bank(
state.can1.modify_filters().clear().enable_bank(
0,
[
ListEntry32::data_frames_with_id(target_id_1),
ListEntry32::remote_frames_with_id(target_id_2),
],
);
drop(filt);

// Frames with matching IDs should be accepted.
let frame = Frame::new_data(target_id_1, []);
Expand Down Expand Up @@ -343,10 +335,11 @@ mod tests {
/// higher-priority frame while all mailboxes are full.
#[test]
fn dequeue_lower_priority_frame(state: &mut State) {
let mut filt = state.can1.modify_filters();
filt.clear();
filt.enable_bank(0, Mask32::accept_all());
drop(filt);
state
.can1
.modify_filters()
.clear()
.enable_bank(0, Mask32::accept_all());

defmt::assert!(state.can1.is_transmitter_idle());

Expand Down

0 comments on commit 1d6f176

Please sign in to comment.