mock-embedded-io

Crates.iomock-embedded-io
lib.rsmock-embedded-io
version0.1.0
created_at2025-05-05 11:34:44.722375+00
updated_at2025-05-05 11:34:44.722375+00
descriptionMock implementations of the embedded_io and embedded_io_async traits
homepagehttps://github.com/matt-rodgers/mock-embedded-io/
repositoryhttps://github.com/matt-rodgers/mock-embedded-io/
max_upload_size
id1660604
size27,628
(matt-rodgers)

documentation

README

mock-embedded-io

Mock implementation of embedded-io and embedded-io-async traits.

This is intended for testing higher level protocols or applications written on top of these traits.

The main types of interest are:

  • Source : mock object implementing both blocking and async Read traits.
  • Sink : mock object implementing both blocking and async Write traits.

These types can be constructed using the builder-style methods to return a desired sequence of return values and data. In the case of the Sink, the data written to it is stored for later inspection.

Example

use embedded_io::{Read, Write};

let data_bytes = "hello world!".as_bytes();
let mut buf: [u8; 64] = [0; 64];

let mut mock_source = Source::new()
                          .data(data_bytes)
                          .error(MockError(embedded_io::ErrorKind::BrokenPipe));

let res = mock_source.read(&mut buf);
assert!(res.is_ok_and(|n| &buf[0..n] == data_bytes));

let res = mock_source.read(&mut buf);
assert!(res.is_err_and(|e| e == MockError(embedded_io::ErrorKind::BrokenPipe)));

let mut mock_sink = Sink::new()
                        .accept_data(12)
                        .error(MockError(embedded_io::ErrorKind::BrokenPipe));

let res = mock_sink.write(data_bytes);
assert!(res.is_ok_and(|n| n == data_bytes.len()));

let res = mock_sink.write(data_bytes);
assert!(res.is_err_and(|e| e == MockError(embedded_io::ErrorKind::BrokenPipe)));

let written = mock_sink.into_inner_data();
assert_eq!(written, data_bytes);
Commit count: 3

cargo fmt