pub struct Source { /* private fields */ }Expand description
A mock which can act as a data source.
An instance of the mock can be constructed using the builder-style methods. Each item added by
the builder methods will be returned in-order when data is read from the Source.
Items can then be read from it using the embedded_io::Read or embedded_io_async::Read
traits.
§Blocking Example
use embedded_io::Read;
let data_bytes = "hello world!".as_bytes();
let mut mock_source = Source::new()
.data(data_bytes)
.error(MockError(embedded_io::ErrorKind::BrokenPipe));
let mut buf: [u8; 64] = [0; 64];
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)));§Async Example
use embedded_io_async::Read;
let data_bytes = "hello world!".as_bytes();
let mut mock_source = Source::new()
.data(data_bytes)
.error(MockError(embedded_io::ErrorKind::BrokenPipe));
let mut buf: [u8; 64] = [0; 64];
let res = mock_source.read(&mut buf).await;
assert!(res.is_ok_and(|n| &buf[0..n] == data_bytes));
let res = mock_source.read(&mut buf).await;
assert!(res.is_err_and(|e| e == MockError(embedded_io::ErrorKind::BrokenPipe)));Implementations§
Source§impl Source
impl Source
Sourcepub fn data<T: Into<Vec<u8>>>(self, data: T) -> Self
pub fn data<T: Into<Vec<u8>>>(self, data: T) -> Self
Add data to the source. This can be returned to the caller either in one chunk or
incrementally - for example if 20 bytes of data are added, the caller could read all 20
bytes in one call, or read 10 bytes twice before the Source will return the following
item.
Sourcepub fn closed(self) -> Self
pub fn closed(self) -> Self
Add a “connection closed” item to the Source. When read, this will return Ok(0) to the
caller (which might then result in an error value if they used the read_exact method
instead of read).
Sourcepub fn is_consumed(&self) -> bool
pub fn is_consumed(&self) -> bool
Check if all of the provided items were consumed
Sourcepub fn owned_handle(&mut self) -> OwnedHandle<'_, Self>
pub fn owned_handle(&mut self) -> OwnedHandle<'_, Self>
Get an OwnedHandle containing the Source.
Trait Implementations§
Source§impl Read for Source
impl Read for Source
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
Source§fn read_exact(
&mut self,
buf: &mut [u8],
) -> Result<(), ReadExactError<Self::Error>>
fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
buf. Read moreSource§impl Read for Source
impl Read for Source
Source§async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
Source§async fn read_exact(
&mut self,
buf: &mut [u8],
) -> Result<(), ReadExactError<Self::Error>>
async fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
buf. Read more