diff --git a/.circleci/config.yml b/.circleci/config.yml index 5ec088f..5336550 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,7 +2,7 @@ version: 2 jobs: build: docker: - - image: rust:1.31 + - image: rust:1.35 steps: - checkout diff --git a/Cargo.toml b/Cargo.toml index da335c6..1a20fce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "embedded-hal-mock" -version = "0.7.2" +version = "0.8.0" authors = ["Danilo Bargen "] categories = ["embedded", "hardware-support", "development-tools::testing"] description = "A collection of mocked devices that implement the embedded-hal traits" @@ -20,5 +20,5 @@ include = [ edition = "2018" [dependencies] -embedded-hal = { version = "0.2.3", features = ["unproven"] } +embedded-hal = "=1.0.0-alpha.1" nb = "0.1.1" diff --git a/README.md b/README.md index b9f6222..4c6ee56 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,6 @@ or no-op implementations are used. The goal of the crate is to be able to test drivers in CI without having access to hardware. -This crate requires Rust 1.31+! - [Docs](https://docs.rs/embedded-hal-mock/) @@ -45,8 +43,8 @@ See [docs](https://docs.rs/embedded-hal-mock/). ## Development Version of `embedded-hal` -If you would like to use the current development version of `embedded-hal` (or any other version), -so long as they are API compatible you can use a patch field in your `Cargo.toml` file to override +If you would like to use the current development version of `embedded-hal` (or any other version), +so long as they are API compatible you can use a patch field in your `Cargo.toml` file to override the dependency version. ```yaml @@ -55,6 +53,12 @@ embedded-hal = { git = "https://github.com/rust-embedded/embedded-hal" } ``` +# Minimum Supported Rust Version (MSRV) + +This crate is guaranteed to compile on stable Rust 1.35 and up. It *might* +compile with older versions but that may change in any new patch release. + + ## License Licensed under either of diff --git a/src/delay.rs b/src/delay.rs index ac332e0..11284df 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -12,6 +12,7 @@ //! [`std::thread::sleep`](https://doc.rust-lang.org/std/thread/fn.sleep.html) //! to implement the delay. +use core::convert::Infallible; use std::thread; use std::time::Duration; @@ -30,8 +31,12 @@ impl MockNoop { macro_rules! impl_noop_delay_us { ($type:ty) => { impl delay::DelayUs<$type> for MockNoop { + type Error = Infallible; + /// A no-op delay implementation. - fn delay_us(&mut self, _n: $type) {} + fn try_delay_us(&mut self, _n: $type) -> Result<(), Self::Error> { + Ok(()) + } } }; } @@ -44,8 +49,12 @@ impl_noop_delay_us!(u64); macro_rules! impl_noop_delay_ms { ($type:ty) => { impl delay::DelayMs<$type> for MockNoop { + type Error = Infallible; + /// A no-op delay implementation. - fn delay_ms(&mut self, _n: $type) {} + fn try_delay_ms(&mut self, _n: $type) -> Result<(), Self::Error> { + Ok(()) + } } }; } @@ -68,9 +77,12 @@ impl StdSleep { macro_rules! impl_stdsleep_delay_us { ($type:ty) => { impl delay::DelayUs<$type> for StdSleep { + type Error = Infallible; + /// A `Delay` implementation that uses `std::thread::sleep`. - fn delay_us(&mut self, n: $type) { + fn try_delay_us(&mut self, n: $type) -> Result<(), Self::Error> { thread::sleep(Duration::from_micros(n as u64)); + Ok(()) } } }; @@ -84,9 +96,12 @@ impl_stdsleep_delay_us!(u64); macro_rules! impl_stdsleep_delay_ms { ($type:ty) => { impl delay::DelayMs<$type> for StdSleep { + type Error = Infallible; + /// A `Delay` implementation that uses `std::thread::sleep`. - fn delay_ms(&mut self, n: $type) { + fn try_delay_ms(&mut self, n: $type) -> Result<(), Self::Error> { thread::sleep(Duration::from_millis(n as u64)); + Ok(()) } } }; diff --git a/src/i2c.rs b/src/i2c.rs index 622d9de..eb34e35 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -18,11 +18,11 @@ //! let mut i2c = I2cMock::new(&expectations); //! //! // Writing -//! i2c.write(0xaa, &vec![1, 2]).unwrap(); +//! i2c.try_write(0xaa, &vec![1, 2]).unwrap(); //! //! // Reading //! let mut buf = vec![0u8; 2]; -//! i2c.read(0xbb, &mut buf).unwrap(); +//! i2c.try_read(0xbb, &mut buf).unwrap(); //! assert_eq!(buf, vec![3, 4]); //! //! // Finalise expectations @@ -60,11 +60,11 @@ //! let mut i2c = I2cMock::new(&expectations); //! //! // Writing returns without an error -//! i2c.write(0xaa, &vec![1, 2]).unwrap(); +//! i2c.try_write(0xaa, &vec![1, 2]).unwrap(); //! //! // Reading returns an error //! let mut buf = vec![0u8; 2]; -//! let err = i2c.read(0xbb, &mut buf).unwrap_err(); +//! let err = i2c.try_read(0xbb, &mut buf).unwrap_err(); //! assert_eq!(err, MockError::Io(ErrorKind::Other)); //! //! // Finalise expectations @@ -158,7 +158,7 @@ pub type Mock = Generic; impl i2c::Read for Mock { type Error = MockError; - fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + fn try_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { let w = self .next() .expect("no pending expectation for i2c::read call"); @@ -185,7 +185,7 @@ impl i2c::Read for Mock { impl i2c::Write for Mock { type Error = MockError; - fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { + fn try_write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { let w = self .next() .expect("no pending expectation for i2c::write call"); @@ -207,7 +207,7 @@ impl i2c::Write for Mock { impl i2c::WriteRead for Mock { type Error = MockError; - fn write_read( + fn try_write_read( &mut self, address: u8, bytes: &[u8], @@ -257,7 +257,7 @@ mod test { let expectations = [Transaction::write(0xaa, vec![10, 12])]; let mut i2c = Mock::new(&expectations); - i2c.write(0xaa, &vec![10, 12]).unwrap(); + i2c.try_write(0xaa, &vec![10, 12]).unwrap(); i2c.done(); } @@ -268,7 +268,7 @@ mod test { let mut i2c = Mock::new(&expectations); let mut buff = vec![0u8; 2]; - i2c.read(0xaa, &mut buff).unwrap(); + i2c.try_read(0xaa, &mut buff).unwrap(); assert_eq!(vec![1, 2], buff); i2c.done(); @@ -281,7 +281,7 @@ mod test { let v = vec![1, 2]; let mut buff = vec![0u8; 2]; - i2c.write_read(0xaa, &v, &mut buff).unwrap(); + i2c.try_write_read(0xaa, &v, &mut buff).unwrap(); assert_eq!(vec![3, 4], buff); i2c.done(); @@ -295,10 +295,10 @@ mod test { ]; let mut i2c = Mock::new(&expectations); - i2c.write(0xaa, &vec![1, 2]).unwrap(); + i2c.try_write(0xaa, &vec![1, 2]).unwrap(); let mut v = vec![0u8; 2]; - i2c.read(0xbb, &mut v).unwrap(); + i2c.try_read(0xbb, &mut v).unwrap(); assert_eq!(v, vec![3, 4]); @@ -311,7 +311,7 @@ mod test { let expectations = [Transaction::write(0xaa, vec![1, 2])]; let mut i2c = Mock::new(&expectations); - i2c.write(0xaa, &vec![1, 3]).unwrap(); + i2c.try_write(0xaa, &vec![1, 3]).unwrap(); i2c.done(); } @@ -323,7 +323,7 @@ mod test { let mut i2c = Mock::new(&expectations); let mut buff = vec![0u8; 2]; - i2c.write(0xaa, &mut buff).unwrap(); + i2c.try_write(0xaa, &mut buff).unwrap(); assert_eq!(vec![10, 12], buff); i2c.done(); @@ -337,7 +337,7 @@ mod test { let v = vec![1, 2]; let mut buff = vec![0u8; 2]; - i2c.write_read(0xaa, &v, &mut buff).unwrap(); + i2c.try_write_read(0xaa, &v, &mut buff).unwrap(); assert_eq!(vec![3, 4], buff); i2c.done(); @@ -349,7 +349,7 @@ mod test { let expectations = [Transaction::read(0xaa, vec![10, 12])]; let mut i2c = Mock::new(&expectations); - i2c.write(0xaa, &vec![10, 12]).unwrap(); + i2c.try_write(0xaa, &vec![10, 12]).unwrap(); i2c.done(); } @@ -363,7 +363,7 @@ mod test { ]; let mut i2c = Mock::new(&expectations); - i2c.write(0xaa, &vec![10, 12]).unwrap(); + i2c.try_write(0xaa, &vec![10, 12]).unwrap(); i2c.done(); } @@ -375,7 +375,7 @@ mod test { let mut i2c_clone = i2c.clone(); let mut buff = vec![0u8; 2]; - i2c.read(0xaa, &mut buff).unwrap(); + i2c.try_read(0xaa, &mut buff).unwrap(); assert_eq!(vec![1, 2], buff); i2c.done(); @@ -400,7 +400,7 @@ mod test { let mut i2c = Mock::new(&[ Transaction::write(0xaa, vec![10, 12]).with_error(expected_err.clone()) ]); - let err = i2c.write(0xaa, &vec![10, 12]).unwrap_err(); + let err = i2c.try_write(0xaa, &vec![10, 12]).unwrap_err(); assert_eq!(err, expected_err); i2c.done(); } @@ -412,7 +412,7 @@ mod test { let mut i2c = Mock::new(&[Transaction::write(0xaa, vec![10, 12]) .with_error(MockError::Io(IoErrorKind::Other))]); let mut buf = vec![0u8; 2]; - let _ = i2c.read(0xaa, &mut buf); + let _ = i2c.try_read(0xaa, &mut buf); } /// The transaction bytes should still be validated. @@ -421,7 +421,7 @@ mod test { fn write_wrong_data() { let mut i2c = Mock::new(&[Transaction::write(0xaa, vec![10, 12]) .with_error(MockError::Io(IoErrorKind::Other))]); - let _ = i2c.write(0xaa, &vec![10, 13]); + let _ = i2c.try_write(0xaa, &vec![10, 13]); } #[test] @@ -432,7 +432,7 @@ mod test { &[Transaction::read(0xaa, vec![10, 12]).with_error(expected_err.clone())], ); let mut buf = vec![0u8; 2]; - let err = i2c.read(0xaa, &mut buf).unwrap_err(); + let err = i2c.try_read(0xaa, &mut buf).unwrap_err(); assert_eq!(err, expected_err); i2c.done(); } @@ -444,7 +444,7 @@ mod test { let mut i2c = Mock::new(&[Transaction::read(0xaa, vec![10, 12]) .with_error(MockError::Io(IoErrorKind::Other))]); - let _ = i2c.write(0xaa, &vec![10, 12]); + let _ = i2c.try_write(0xaa, &vec![10, 12]); } #[test] @@ -453,7 +453,7 @@ mod test { let mut i2c = Mock::new(&[Transaction::write_read(0xaa, vec![10, 12], vec![13, 14]) .with_error(expected_err.clone())]); let mut buf = vec![0u8; 2]; - let err = i2c.write_read(0xaa, &[10, 12], &mut buf).unwrap_err(); + let err = i2c.try_write_read(0xaa, &[10, 12], &mut buf).unwrap_err(); assert_eq!(err, expected_err); i2c.done(); } @@ -464,7 +464,7 @@ mod test { fn write_read_wrong_mode() { let mut i2c = Mock::new(&[Transaction::write_read(0xaa, vec![10, 12], vec![13, 14]) .with_error(MockError::Io(IoErrorKind::Other))]); - let _ = i2c.write(0xaa, &vec![10, 12]); + let _ = i2c.try_write(0xaa, &vec![10, 12]); } /// The transaction bytes should still be validated. @@ -474,7 +474,7 @@ mod test { let mut i2c = Mock::new(&[Transaction::write_read(0xaa, vec![10, 12], vec![13, 14]) .with_error(MockError::Io(IoErrorKind::Other))]); let mut buf = vec![0u8; 2]; - let _ = i2c.write_read(0xaa, &vec![10, 13], &mut buf); + let _ = i2c.try_write_read(0xaa, &vec![10, 13], &mut buf); } } } diff --git a/src/pin.rs b/src/pin.rs index 92f54df..da2cf9e 100644 --- a/src/pin.rs +++ b/src/pin.rs @@ -1,14 +1,14 @@ //! Mock digital [`InputPin`] and [`OutputPin`] v2 implementations //! -//! [`InputPin`]: https://docs.rs/embedded-hal/0.2/embedded_hal/digital/v2/trait.InputPin.html -//! [`OutputPin`]: https://docs.rs/embedded-hal/0.2/embedded_hal/digital/v2/trait.OutputPin.html +//! [`InputPin`]: https://docs.rs/embedded-hal/1.0.0-alpha.1/embedded_hal/digital/trait.InputPin.html +//! [`OutputPin`]: https://docs.rs/embedded-hal/1.0.0-alpha.1/embedded_hal/digital/trait.OutputPin.html //! //! ``` //! use std::io::ErrorKind; //! //! use embedded_hal_mock::MockError; //! use embedded_hal_mock::pin::{Transaction as PinTransaction, Mock as PinMock, State as PinState}; -//! use embedded_hal::digital::v2::{InputPin, OutputPin}; +//! use embedded_hal::digital::{InputPin, OutputPin}; //! //! let err = MockError::Io(ErrorKind::NotConnected); //! @@ -24,11 +24,11 @@ //! let mut pin = PinMock::new(&expectations); //! //! // Run and test -//! assert_eq!(pin.is_high().unwrap(), true); -//! assert_eq!(pin.is_low().unwrap(), false); +//! assert_eq!(pin.try_is_high().unwrap(), true); +//! assert_eq!(pin.try_is_low().unwrap(), false); //! -//! pin.set_low().unwrap(); -//! pin.set_high().expect_err("expected error return"); +//! pin.try_set_low().unwrap(); +//! pin.try_set_high().expect_err("expected error return"); //! //! pin.done(); //! @@ -42,7 +42,7 @@ use crate::common::Generic; use crate::error::MockError; -use embedded_hal::digital::v2::{InputPin, OutputPin}; +use embedded_hal::digital::{InputPin, OutputPin}; /// MockPin transaction #[derive(PartialEq, Clone, Debug)] @@ -92,7 +92,7 @@ impl Transaction { /// MockPin transaction kind, either Get or Set with the associated State #[derive(PartialEq, Clone, Debug)] pub enum TransactionKind { - /// Set(true) for set_high or Set(false) for set_low + /// Set(true) for try_set_high or Set(false) for try_set_low Set(State), /// Get(true) for high value or Get(false) for low value Get(State), @@ -116,13 +116,15 @@ impl OutputPin for Mock { type Error = MockError; /// Drives the pin low - fn set_low(&mut self) -> Result<(), Self::Error> { - let Transaction { kind, err } = self.next().expect("no expectation for pin::set_low call"); + fn try_set_low(&mut self) -> Result<(), Self::Error> { + let Transaction { kind, err } = self + .next() + .expect("no expectation for pin::try_set_low call"); assert_eq!( kind, TransactionKind::Set(State::Low), - "expected pin::set_low" + "expected pin::try_set_low" ); match err { @@ -132,13 +134,15 @@ impl OutputPin for Mock { } /// Drives the pin high - fn set_high(&mut self) -> Result<(), Self::Error> { - let Transaction { kind, err } = self.next().expect("no expectation for pin::set_high call"); + fn try_set_high(&mut self) -> Result<(), Self::Error> { + let Transaction { kind, err } = self + .next() + .expect("no expectation for pin::try_set_high call"); assert_eq!( kind, TransactionKind::Set(State::High), - "expected pin::set_high" + "expected pin::try_set_high" ); match err { @@ -153,10 +157,10 @@ impl InputPin for Mock { type Error = MockError; /// Is the input pin high? - fn is_high(&self) -> Result { + fn try_is_high(&self) -> Result { let mut s = self.clone(); - let Transaction { kind, err } = s.next().expect("no expectation for pin::is_high call"); + let Transaction { kind, err } = s.next().expect("no expectation for pin::try_is_high call"); assert_eq!(kind.is_get(), true, "expected pin::get"); @@ -170,10 +174,10 @@ impl InputPin for Mock { } /// Is the input pin low? - fn is_low(&self) -> Result { + fn try_is_low(&self) -> Result { let mut s = self.clone(); - let Transaction { kind, err } = s.next().expect("no expectation for pin::is_low call"); + let Transaction { kind, err } = s.next().expect("no expectation for pin::try_is_low call"); assert_eq!(kind.is_get(), true, "expected pin::get"); @@ -193,7 +197,7 @@ mod test { use std::io::ErrorKind; use crate::error::MockError; - use embedded_hal::digital::v2::{InputPin, OutputPin}; + use embedded_hal::digital::{InputPin, OutputPin}; use crate::pin::TransactionKind::{Get, Set}; use crate::pin::{Mock, State, Transaction}; @@ -209,12 +213,12 @@ mod test { ]; let mut pin = Mock::new(&expectations); - assert_eq!(pin.is_high().unwrap(), true); - assert_eq!(pin.is_low().unwrap(), false); - assert_eq!(pin.is_high().unwrap(), false); - assert_eq!(pin.is_low().unwrap(), true); + assert_eq!(pin.try_is_high().unwrap(), true); + assert_eq!(pin.try_is_low().unwrap(), false); + assert_eq!(pin.try_is_high().unwrap(), false); + assert_eq!(pin.try_is_low().unwrap(), true); - pin.is_low().expect_err("expected error return"); + pin.try_is_low().expect_err("expected error return"); pin.done(); } @@ -228,10 +232,10 @@ mod test { ]; let mut pin = Mock::new(&expectations); - pin.set_high().unwrap(); - pin.set_low().unwrap(); + pin.try_set_high().unwrap(); + pin.try_set_low().unwrap(); - pin.set_high().expect_err("expected error return"); + pin.try_set_high().expect_err("expected error return"); pin.done(); } diff --git a/src/serial.rs b/src/serial.rs index a69dd3d..0b784b8 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -27,18 +27,18 @@ //! let mut serial = SerialMock::new(&expectations); //! //! // Expect three reads -//! assert_eq!(serial.read().unwrap(), 0x0A); -//! assert_eq!(serial.read().unwrap(), b'x'); -//! assert_eq!(serial.read().unwrap(), b'y'); +//! assert_eq!(serial.try_read().unwrap(), 0x0A); +//! assert_eq!(serial.try_read().unwrap(), b'x'); +//! assert_eq!(serial.try_read().unwrap(), b'y'); //! //! // When designing against the non-blocking serial //! // trait, we expect two separate writes. These could be //! // expressed as two separate transactions, too. See (1) above. -//! serial.write(1).unwrap(); -//! serial.write(2).unwrap(); +//! serial.try_write(1).unwrap(); +//! serial.try_write(2).unwrap(); //! //! // Finally, we expect a flush -//! serial.flush().unwrap(); +//! serial.try_flush().unwrap(); //! //! // When you believe there are no more calls on the mock, //! // call done() to assert there are no pending transactions. @@ -67,17 +67,17 @@ //! let mut serial = SerialMock::new(&expectations); //! //! // Expect three reads -//! assert_eq!(serial.read().unwrap(), 0x0A); -//! assert_eq!(serial.read().unwrap(), b'x'); -//! assert_eq!(serial.read().unwrap(), b'y'); +//! assert_eq!(serial.try_read().unwrap(), 0x0A); +//! assert_eq!(serial.try_read().unwrap(), b'x'); +//! assert_eq!(serial.try_read().unwrap(), b'y'); //! //! // We use the blocking write here, and we assert that //! // two words are written. See (2) above. -//! serial.bwrite_all(&[1, 2]).unwrap(); +//! serial.try_bwrite_all(&[1, 2]).unwrap(); //! //! // Finally, we expect a flush. Note that this is //! // a *blocking* flush from the blocking serial trait. -//! serial.bflush().unwrap(); +//! serial.try_bflush().unwrap(); //! //! // When you believe there are no more calls on the mock, //! // call done() to assert there are no pending transactions. @@ -108,18 +108,18 @@ //! let mut serial = SerialMock::new(&expectations); //! //! // The first read will succeed -//! assert_eq!(serial.read().unwrap(), 42); +//! assert_eq!(serial.try_read().unwrap(), 42); //! //! // The second read will return an error -//! assert_eq!(serial.read().unwrap_err(), nb::Error::WouldBlock); +//! assert_eq!(serial.try_read().unwrap_err(), nb::Error::WouldBlock); //! //! // The following write/flush calls will return errors as well //! assert_eq!( -//! serial.write(23).unwrap_err(), +//! serial.try_write(23).unwrap_err(), //! nb::Error::Other(MockError::Io(ErrorKind::Other)) //! ); //! assert_eq!( -//! serial.flush().unwrap_err(), +//! serial.try_flush().unwrap_err(), //! nb::Error::Other(MockError::Io(ErrorKind::Interrupted)) //! ); //! @@ -362,8 +362,10 @@ where { type Error = MockError; - fn read(&mut self) -> nb::Result { - let t = self.pop().expect("called serial::read with no expectation"); + fn try_read(&mut self) -> nb::Result { + let t = self + .pop() + .expect("called serial::try_read with no expectation"); match t { Mode::Read(word) => Ok(word.clone()), Mode::ReadError(error) => Err(error), @@ -381,15 +383,15 @@ where { type Error = MockError; - fn write(&mut self, word: Word) -> nb::Result<(), Self::Error> { + fn try_write(&mut self, word: Word) -> nb::Result<(), Self::Error> { let t = self .pop() - .expect("called serial::write with no expectation"); + .expect("called serial::try_write with no expectation"); let assert_write = |expectation: Word| { assert_eq!( expectation, word, - "serial::write expected to write {:?} but actually wrote {:?}", + "serial::try_write expected to write {:?} but actually wrote {:?}", expectation, word ); }; @@ -410,10 +412,10 @@ where } } - fn flush(&mut self) -> nb::Result<(), Self::Error> { + fn try_flush(&mut self) -> nb::Result<(), Self::Error> { let t = self .pop() - .expect("called serial::flush with no expectation"); + .expect("called serial::try_flush with no expectation"); match t { Mode::Flush => Ok(()), Mode::FlushError(error) => Err(error), @@ -453,7 +455,7 @@ mod test { fn test_serial_mock_read() { let ts = [Transaction::read(0x54)]; let mut ser = Mock::new(&ts); - let r = ser.read().expect("failed to read"); + let r = ser.try_read().expect("failed to read"); assert_eq!(r, 0x54); ser.done(); } @@ -462,7 +464,7 @@ mod test { fn test_serial_mock_write_single_value_nonblocking() { let ts = [Transaction::write(0xAB)]; let mut ser = Mock::new(&ts); - ser.write(0xAB).unwrap(); + ser.try_write(0xAB).unwrap(); ser.done(); } @@ -470,9 +472,9 @@ mod test { fn test_serial_mock_write_many_values_nonblocking() { let ts = [Transaction::write_many([0xAB, 0xCD, 0xEF])]; let mut ser = Mock::new(&ts); - ser.write(0xAB).unwrap(); - ser.write(0xCD).unwrap(); - ser.write(0xEF).unwrap(); + ser.try_write(0xAB).unwrap(); + ser.try_write(0xCD).unwrap(); + ser.try_write(0xEF).unwrap(); ser.done(); } @@ -480,16 +482,16 @@ mod test { fn test_serial_mock_blocking_write() { let ts = [Transaction::write_many([0xAB, 0xCD, 0xEF])]; let mut ser = Mock::new(&ts); - ser.bwrite_all(&[0xAB, 0xCD, 0xEF]).unwrap(); + ser.try_bwrite_all(&[0xAB, 0xCD, 0xEF]).unwrap(); ser.done(); } #[test] - #[should_panic(expected = "called serial::write with no expectation")] + #[should_panic(expected = "called serial::try_write with no expectation")] fn test_serial_mock_blocking_write_more_than_expected() { let ts = [Transaction::write_many([0xAB, 0xCD])]; let mut ser = Mock::new(&ts); - ser.bwrite_all(&[0xAB, 0xCD, 0xEF]).unwrap(); + ser.try_bwrite_all(&[0xAB, 0xCD, 0xEF]).unwrap(); ser.done(); } @@ -498,23 +500,23 @@ mod test { fn test_serial_mock_blocking_write_not_enough() { let ts = [Transaction::write_many([0xAB, 0xCD, 0xEF, 0x00])]; let mut ser = Mock::new(&ts); - ser.bwrite_all(&[0xAB, 0xCD, 0xEF]).unwrap(); + ser.try_bwrite_all(&[0xAB, 0xCD, 0xEF]).unwrap(); ser.done(); } #[test] - #[should_panic(expected = "serial::write expected to write")] + #[should_panic(expected = "serial::try_write expected to write")] fn test_serial_mock_wrong_write() { let ts = [Transaction::write(0x12)]; let mut ser = Mock::new(&ts); - ser.write(0x14).unwrap(); + ser.try_write(0x14).unwrap(); } #[test] fn test_serial_mock_flush() { let ts = [Transaction::flush()]; let mut ser: Mock = Mock::new(&ts); - ser.flush().unwrap(); + ser.try_flush().unwrap(); ser.done(); } @@ -522,7 +524,7 @@ mod test { fn test_serial_mock_blocking_flush() { let ts = [Transaction::flush()]; let mut ser: Mock = Mock::new(&ts); - ser.bflush().unwrap(); + ser.try_bflush().unwrap(); ser.done(); } @@ -539,7 +541,7 @@ mod test { fn test_serial_mock_reuse_pending_transactions() { let ts = [Transaction::read(0x54)]; let mut ser = Mock::new(&ts); - let r = ser.read().expect("failed to read"); + let r = ser.try_read().expect("failed to read"); assert_eq!(r, 0x54); ser.done(); ser.expect(&ts); @@ -551,7 +553,7 @@ mod test { fn test_serial_mock_expected_read() { let ts = [Transaction::read(0x54)]; let mut ser = Mock::new(&ts); - ser.bwrite_all(&[0x77]).unwrap(); + ser.try_bwrite_all(&[0x77]).unwrap(); } #[test] @@ -559,7 +561,7 @@ mod test { fn test_serial_mock_expected_write() { let ts = [Transaction::write(0x54)]; let mut ser = Mock::new(&ts); - ser.flush().unwrap(); + ser.try_flush().unwrap(); } #[test] @@ -567,7 +569,7 @@ mod test { fn test_serial_mock_expected_flush() { let ts = [Transaction::flush()]; let mut ser: Mock = Mock::new(&ts); - ser.read().unwrap(); + ser.try_read().unwrap(); } #[test] @@ -575,7 +577,7 @@ mod test { let error = nb::Error::WouldBlock; let ts = [Transaction::read_error(error.clone())]; let mut ser: Mock = Mock::new(&ts); - assert_eq!(ser.read().unwrap_err(), error); + assert_eq!(ser.try_read().unwrap_err(), error); } #[test] @@ -583,18 +585,18 @@ mod test { let error = nb::Error::Other(MockError::Io(io::ErrorKind::NotConnected)); let ts = [Transaction::write_error(42, error.clone())]; let mut ser: Mock = Mock::new(&ts); - assert_eq!(ser.write(42).unwrap_err(), error); + assert_eq!(ser.try_write(42).unwrap_err(), error); } #[test] - #[should_panic(expected = "serial::write expected to write 42 but actually wrote 23")] + #[should_panic(expected = "serial::try_write expected to write 42 but actually wrote 23")] fn test_serial_mock_write_error_wrong_data() { let error = nb::Error::Other(MockError::Io(io::ErrorKind::NotConnected)); let ts = [Transaction::write_error(42, error.clone())]; let mut ser: Mock = Mock::new(&ts); // The data to be written should still be verified, even if there's an // error attached. - ser.write(23).unwrap(); + ser.try_write(23).unwrap(); } #[test] @@ -602,6 +604,6 @@ mod test { let error = nb::Error::Other(MockError::Io(io::ErrorKind::TimedOut)); let ts = [Transaction::flush_error(error.clone())]; let mut ser: Mock = Mock::new(&ts); - assert_eq!(ser.flush().unwrap_err(), error); + assert_eq!(ser.try_flush().unwrap_err(), error); } } diff --git a/src/spi.rs b/src/spi.rs index b316fca..9395b8d 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -27,17 +27,17 @@ //! //! let mut spi = SpiMock::new(&expectations); //! // FullDuplex transfers -//! spi.send(0x09); -//! assert_eq!(spi.read().unwrap(), 0x0A); -//! spi.send(0xFE); -//! assert_eq!(spi.read().unwrap(), 0xFF); +//! spi.try_send(0x09); +//! assert_eq!(spi.try_read().unwrap(), 0x0A); +//! spi.try_send(0xFE); +//! assert_eq!(spi.try_read().unwrap(), 0xFF); //! //! // Writing -//! spi.write(&vec![1, 2]).unwrap(); +//! spi.try_write(&vec![1, 2]).unwrap(); //! //! // Transferring //! let mut buf = vec![3, 4]; -//! spi.transfer(&mut buf).unwrap(); +//! spi.try_transfer(&mut buf).unwrap(); //! assert_eq!(buf, vec![5, 6]); //! //! // Finalise expectations @@ -100,6 +100,7 @@ impl Transaction { response: Vec::new(), } } + /// Create a transfer transaction pub fn read(response: u8) -> Transaction { Transaction { @@ -126,7 +127,7 @@ impl spi::Write for Mock { /// spi::Write implementation for Mock /// /// This will cause an assertion if the write call does not match the next expectation - fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { + fn try_write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { let w = self.next().expect("no expectation for spi::write call"); assert_eq!(w.expected_mode, Mode::Write, "spi::write unexpected mode"); assert_eq!( @@ -139,15 +140,20 @@ impl spi::Write for Mock { impl FullDuplex for Mock { type Error = MockError; + /// spi::FullDuplex implementeation for Mock /// /// This will call the nonblocking read/write primitives. - fn send(&mut self, buffer: u8) -> nb::Result<(), Self::Error> { - let data = self.next().expect("no expectation for spi::send call"); - assert_eq!(data.expected_mode, Mode::Send, "spi::send unexpected mode"); + fn try_send(&mut self, buffer: u8) -> nb::Result<(), Self::Error> { + let data = self.next().expect("no expectation for spi::try_send call"); + assert_eq!( + data.expected_mode, + Mode::Send, + "spi::try_send unexpected mode" + ); assert_eq!( data.expected_data[0], buffer, - "spi::send data does not match expectation" + "spi::try_send data does not match expectation" ); Ok(()) } @@ -155,13 +161,13 @@ impl FullDuplex for Mock { /// spi::FullDuplex implementeation for Mock /// /// This will call the nonblocking read/write primitives. - fn read(&mut self) -> nb::Result { - let w = self.next().expect("no expectation for spi::read call"); + fn try_read(&mut self) -> nb::Result { + let w = self.next().expect("no expectation for spi::try_read call"); assert_eq!(w.expected_mode, Mode::Read, "spi::Read unexpected mode"); assert_eq!( 1, w.response.len(), - "mismatched response length for spi::read" + "mismatched response length for spi::try_read" ); let buffer: u8 = w.response[0]; Ok(buffer) @@ -173,12 +179,14 @@ impl spi::Transfer for Mock { /// spi::Transfer implementation for Mock /// /// This writes the provided response to the buffer and will cause an assertion if the written data does not match the next expectation - fn transfer<'w>(&mut self, buffer: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { - let w = self.next().expect("no expectation for spi::transfer call"); + fn try_transfer<'w>(&mut self, buffer: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { + let w = self + .next() + .expect("no expectation for spi::try_transfer call"); assert_eq!( w.expected_mode, Mode::Transfer, - "spi::transfer unexpected mode" + "spi::try_transfer unexpected mode" ); assert_eq!( &w.expected_data, &buffer, @@ -187,7 +195,7 @@ impl spi::Transfer for Mock { assert_eq!( buffer.len(), w.response.len(), - "mismatched response length for spi::transfer" + "mismatched response length for spi::try_transfer" ); buffer.copy_from_slice(&w.response); Ok(buffer) @@ -204,7 +212,7 @@ mod test { fn test_spi_mock_send() { let mut spi = Mock::new(&[Transaction::send(10)]); - let _ = spi.send(10).unwrap(); + let _ = spi.try_send(10).unwrap(); spi.done(); } @@ -213,7 +221,7 @@ mod test { fn test_spi_mock_read() { let mut spi = Mock::new(&[Transaction::read(10)]); - let ans = spi.read().unwrap(); + let ans = spi.try_read().unwrap(); assert_eq!(ans, 10); @@ -232,14 +240,14 @@ mod test { ]; let mut spi = Mock::new(&expectations); - spi.write(&vec![1, 2]).unwrap(); + spi.try_write(&vec![1, 2]).unwrap(); - let _ = spi.send(0x09); - assert_eq!(spi.read().unwrap(), 0x0a); - let _ = spi.send(0xfe); - assert_eq!(spi.read().unwrap(), 0xFF); + let _ = spi.try_send(0x09); + assert_eq!(spi.try_read().unwrap(), 0x0a); + let _ = spi.try_send(0xfe); + assert_eq!(spi.try_read().unwrap(), 0xFF); let mut v = vec![3, 4]; - spi.transfer(&mut v).unwrap(); + spi.try_transfer(&mut v).unwrap(); assert_eq!(v, vec![5, 6]); @@ -251,7 +259,7 @@ mod test { let expectations = [Transaction::write(vec![10, 12])]; let mut spi = Mock::new(&expectations); - spi.write(&vec![10, 12]).unwrap(); + spi.try_write(&vec![10, 12]).unwrap(); spi.done(); } @@ -262,7 +270,7 @@ mod test { let mut spi = Mock::new(&expectations); let mut v = vec![10, 12]; - spi.transfer(&mut v).unwrap(); + spi.try_transfer(&mut v).unwrap(); assert_eq!(v, vec![12, 13]); @@ -277,10 +285,10 @@ mod test { ]; let mut spi = Mock::new(&expectations); - spi.write(&vec![1, 2]).unwrap(); + spi.try_write(&vec![1, 2]).unwrap(); let mut v = vec![3, 4]; - spi.transfer(&mut v).unwrap(); + spi.try_transfer(&mut v).unwrap(); assert_eq!(v, vec![5, 6]); @@ -293,7 +301,7 @@ mod test { let expectations = [Transaction::write(vec![10, 12])]; let mut spi = Mock::new(&expectations); - spi.write(&vec![10, 12, 12]).unwrap(); + spi.try_write(&vec![10, 12, 12]).unwrap(); spi.done(); } @@ -305,7 +313,7 @@ mod test { let mut spi = Mock::new(&expectations); let mut v = vec![10, 12]; - spi.transfer(&mut v).unwrap(); + spi.try_transfer(&mut v).unwrap(); assert_eq!(v, vec![12, 13]); @@ -319,7 +327,7 @@ mod test { let mut spi = Mock::new(&expectations); let mut v = vec![10, 12]; - spi.transfer(&mut v).unwrap(); + spi.try_transfer(&mut v).unwrap(); assert_eq!(v, vec![12, 13]); @@ -332,7 +340,7 @@ mod test { let expectations = [Transaction::transfer(vec![10, 12], vec![])]; let mut spi = Mock::new(&expectations); - spi.write(&vec![10, 12, 12]).unwrap(); + spi.try_write(&vec![10, 12, 12]).unwrap(); spi.done(); } @@ -346,7 +354,7 @@ mod test { ]; let mut spi = Mock::new(&expectations); - spi.write(&vec![10, 12, 12]).unwrap(); + spi.try_write(&vec![10, 12, 12]).unwrap(); spi.done(); }