Skip to content

Commit f42b26c

Browse files
committed
Fixes from review
This applies _most_ of the final comments from the review on #101. I phrased one of the changes slightly differently.
1 parent cc0f23a commit f42b26c

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

src/common.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ use std::{
2121
/// cloned instance of the mock can be used to check the expectations of the
2222
/// original instance that has been moved into a driver.
2323
///
24-
/// Optional Data type allows for storage of arbitrary state data in mocks.
24+
/// The `mock_data` field allows for storage of arbitrary state data in mocks.
2525
/// Mock data can be accessed via appropriate getter/setter methods, i.e. `mock_data()` and `set_mock_data()`.
2626
///
2727
#[derive(Debug, Clone)]
28-
pub struct Generic<T: Clone + Debug + PartialEq, Data = ()> {
28+
pub struct Generic<T: Clone + Debug + PartialEq, TData = ()> {
2929
expected: Arc<Mutex<VecDeque<T>>>,
3030
done_called: Arc<Mutex<DoneCallDetector>>,
31-
mock_data: Option<Data>,
31+
mock_data: Option<TData>,
3232
}
3333

34-
impl<'a, T: 'a, Data> Generic<T, Data>
34+
impl<'a, T: 'a, TData> Generic<T, TData>
3535
where
3636
T: Clone + Debug + PartialEq,
3737
{
3838
/// Create a new mock interface
3939
///
4040
/// This creates a new generic mock interface with initial expectations
41-
pub fn new<E>(expected: E) -> Generic<T, Data>
41+
pub fn new<E>(expected: E) -> Generic<T, TData>
4242
where
4343
E: IntoIterator<Item = &'a T>,
4444
{
@@ -107,18 +107,18 @@ where
107107
}
108108

109109
/// Get the mock data
110-
pub fn mock_data(&self) -> &Option<Data> {
110+
pub fn mock_data(&self) -> &Option<TData> {
111111
&self.mock_data
112112
}
113113

114114
/// Set the mock data
115-
pub fn set_mock_data(&mut self, data: Option<Data>) {
115+
pub fn set_mock_data(&mut self, data: Option<TData>) {
116116
self.mock_data = data;
117117
}
118118
}
119119

120120
/// Iterator impl for use in mock impls
121-
impl<T, Data> Iterator for Generic<T, Data>
121+
impl<T, TData> Iterator for Generic<T, TData>
122122
where
123123
T: Clone + Debug + PartialEq,
124124
{

src/eh1/io.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! IO mock implementations.
22
//!
33
//! This mock supports the specification and checking of expectations to allow
4-
//! automated testing of IO based drivers. Mismatches between expected and
5-
//! real IO transactions will cause runtime assertions to assist with locating
4+
//! automated testing of `embedded-io` based drivers. Mismatches between expected
5+
//! and real IO transactions will cause runtime assertions to assist with locating
66
//! faults.
77
//!
88
//! ## Usage
@@ -21,28 +21,28 @@
2121
//! let mut io = IoMock::new(&expectations);
2222
//!
2323
//! // Writing
24-
//! let ret = io.write(&[10, 20]).unwrap();
25-
//! assert_eq!(ret, 2);
24+
//! let bytes_written = io.write(&[10, 20]).unwrap();
25+
//! assert_eq!(bytes_written, 2);
2626
//!
2727
//! // Reading
2828
//! let mut buffer = [0; 2];
29-
//! let ret = io.read(&mut buffer).unwrap();
29+
//! let bytes_read = io.read(&mut buffer).unwrap();
3030
//! assert_eq!(buffer, [20, 30]);
31-
//! assert_eq!(ret, 2);
31+
//! assert_eq!(bytes_read, 2);
3232
//!
3333
//! // Flushing
3434
//! io.flush().unwrap();
3535
//!
3636
//! // Finalizing expectations
3737
//! io.done();
3838
//!
39-
//! // optional async
39+
//! // Async is supported with the optional `embedded-hal-async` feature.
4040
//! #[cfg(feature = "embedded-hal-async")]
4141
//! async {
4242
//! use embedded_io_async;
4343
//! let mut io = IoMock::new(&[IoTransaction::write(vec![10, 20])]);
44-
//! let ret = embedded_io_async::Write::write(&mut io, &[10, 20]).await.unwrap();
45-
//! assert_eq!(ret, 2);
44+
//! let bytes_written = embedded_io_async::Write::write(&mut io, &[10, 20]).await.unwrap();
45+
//! assert_eq!(bytes_written, 2);
4646
//! io.done();
4747
//! };
4848
//!
@@ -92,10 +92,10 @@ pub struct Transaction {
9292

9393
impl Transaction {
9494
/// Create a write transaction
95-
pub fn write(expected: Vec<u8>) -> Transaction {
95+
pub fn write(expected_data: Vec<u8>) -> Transaction {
9696
Transaction {
9797
expected_mode: Mode::Write,
98-
expected_data: expected,
98+
expected_data,
9999
response: Vec::new(),
100100
expected_err: None,
101101
}
@@ -237,7 +237,7 @@ impl Read for Mock {
237237
panic!("response longer than read buffer for io::read");
238238
}
239239

240-
let len = std::cmp::min(buffer.len(), transaction.response.len());
240+
let len = transaction.response.len();
241241
buffer[..len].copy_from_slice(&transaction.response[..len]);
242242

243243
match transaction.expected_err {
@@ -252,7 +252,7 @@ impl Seek for Mock {
252252
let transaction = self.next().expect("no expectation for io::seek call");
253253

254254
if let Mode::Seek(expected_pos) = transaction.expected_mode {
255-
assert_eq!(expected_pos, pos, "io::seek unexpected mode");
255+
assert_eq!(expected_pos, pos, "io::seek unexpected pos");
256256

257257
let ret_offset: u64 = u64::from_be_bytes(transaction.response.try_into().unwrap());
258258
match transaction.expected_err {
@@ -313,8 +313,7 @@ impl BufRead for Mock {
313313
"io::fill_buf unexpected mode"
314314
);
315315

316-
let response_vec = transaction.response;
317-
self.set_mock_data(Some(response_vec));
316+
self.set_mock_data(Some(transaction.response));
318317

319318
match transaction.expected_err {
320319
Some(err) => Err(err),

0 commit comments

Comments
 (0)