Source

Struct Source 

Source
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

Source

pub fn new() -> Self

Create a new empty Source

Source

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.

Source

pub fn error(self, e: MockError) -> Self

Add an error value to the Source.

Source

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).

Source

pub fn is_consumed(&self) -> bool

Check if all of the provided items were consumed

Source

pub fn owned_handle(&mut self) -> OwnedHandle<'_, Self>

Get an OwnedHandle containing the Source.

Trait Implementations§

Source§

impl Debug for Source

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Source

Source§

fn default() -> Source

Returns the “default value” for a type. Read more
Source§

impl ErrorType for Source

Source§

type Error = MockError

Error type of all the IO operations on this type.
Source§

impl Read for Source

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
Source§

impl Read for Source

Source§

async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

async fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more

Auto Trait Implementations§

§

impl Freeze for Source

§

impl RefUnwindSafe for Source

§

impl Send for Source

§

impl Sync for Source

§

impl Unpin for Source

§

impl UnwindSafe for Source

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.