Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO implementation for embedded-io #101

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ rust-version = "1.63"

[features]
eh0 = ["dep:eh0", "dep:nb"]
eh1 = ["dep:eh1", "dep:embedded-hal-nb"]
eh1 = ["dep:eh1", "dep:embedded-hal-nb", "dep:embedded-io"]
dbrgn marked this conversation as resolved.
Show resolved Hide resolved

embedded-time = ["dep:embedded-time", "dep:void"]
embedded-hal-async = ["dep:embedded-hal-async"]
embedded-hal-async = ["dep:embedded-hal-async", "dep:embedded-io-async"]

default = ["eh0", "eh1", "embedded-time"]

Expand All @@ -35,6 +35,8 @@ eh1 = { package = "embedded-hal", version = "1.0", optional = true }
embedded-hal-nb = { version = "1.0", optional = true }
embedded-hal-async = { version = "1.0", optional = true }
embedded-time = { version = "0.12", optional = true }
embedded-io = { version = "0.6.1", optional = true }
embedded-io-async = { version = "0.6.1", optional = true }
nb = { version = "1.1", optional = true }
void = { version = "^1.0", optional = true }

Expand Down
24 changes: 20 additions & 4 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,32 @@ use std::{
/// Note that the implementation uses an `Arc<Mutex<...>>` internally, so a
/// cloned instance of the mock can be used to check the expectations of the
/// original instance that has been moved into a driver.
///
/// Optional Data type allows for storage of arbitrary state data in mocks.
/// Mock data can be accessed via appropriate getter/setter methods, i.e. `mock_data()` and `set_mock_data()`.
Comment on lines +24 to +25
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Optional Data type allows for storage of arbitrary state data in mocks.
/// Mock data can be accessed via appropriate getter/setter methods, i.e. `mock_data()` and `set_mock_data()`.
/// The `mock_data` field allows for storage of arbitrary state data in mocks.
/// Mock data can be accessed via appropriate getter/setter methods, i.e. `mock_data()` and `set_mock_data()`.

///
#[derive(Debug, Clone)]
pub struct Generic<T: Clone + Debug + PartialEq> {
pub struct Generic<T: Clone + Debug + PartialEq, Data = ()> {
Copy link
Owner

@dbrgn dbrgn May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the T-prefix convention for type parameters. Can you rename Data to TData?

expected: Arc<Mutex<VecDeque<T>>>,
done_called: Arc<Mutex<DoneCallDetector>>,
mock_data: Option<Data>,
}

impl<'a, T: 'a> Generic<T>
impl<'a, T: 'a, Data> Generic<T, Data>
where
T: Clone + Debug + PartialEq,
{
/// Create a new mock interface
///
/// This creates a new generic mock interface with initial expectations
pub fn new<E>(expected: E) -> Generic<T>
pub fn new<E>(expected: E) -> Generic<T, Data>
where
E: IntoIterator<Item = &'a T>,
{
let mut g = Generic {
expected: Arc::new(Mutex::new(VecDeque::new())),
done_called: Arc::new(Mutex::new(DoneCallDetector::new())),
mock_data: None,
};

g.update_expectations(expected);
Expand Down Expand Up @@ -99,10 +105,20 @@ where
let e = self.expected.lock().unwrap();
assert!(e.is_empty(), "Not all expectations consumed");
}

/// Get the mock data
pub fn mock_data(&self) -> &Option<Data> {
&self.mock_data
}

/// Set the mock data
pub fn set_mock_data(&mut self, data: Option<Data>) {
dbrgn marked this conversation as resolved.
Show resolved Hide resolved
self.mock_data = data;
}
}

/// Iterator impl for use in mock impls
impl<T> Iterator for Generic<T>
impl<T, Data> Iterator for Generic<T, Data>
where
T: Clone + Debug + PartialEq,
{
Expand Down
1 change: 1 addition & 0 deletions src/eh1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use crate::eh1::error::MockError;

pub mod delay;
pub mod i2c;
pub mod io;
pub mod pin;
pub mod pwm;
pub mod serial;
Expand Down
Loading