Sink

Struct Sink 

Source
pub struct Sink { /* private fields */ }
Expand description

A mock which can act as a data sink.

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 written to the Sink.

Data can then be written to it using the embedded_io::Write or embedded_io_async::Write traits.

§Blocking Example

use embedded_io::Write;

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

let data_bytes = "hello world!".as_bytes();
let res = mock_sink.write_all(data_bytes);
assert!(res.is_ok());

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

§Async Example

use embedded_io_async::Write;

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

let data_bytes = "hello world!".as_bytes();
let res = mock_sink.write_all(data_bytes).await;
assert!(res.is_ok());

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

Implementations§

Source§

impl Sink

Source

pub fn new() -> Self

Create a new empty Sink.

Source

pub fn accept_data(self, n: usize) -> Self

Accept n bytes of data written to the Sink

Source

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

Add an error value to the Sink

Source

pub fn closed(self) -> Self

Add a “connection closed” item to the Sink. When written, this will return Ok(0) to the caller (which might then result in an error value if they used the write_all method instead of write).

Source

pub fn is_consumed(&self) -> bool

Check if all of the provided items were consumed

Source

pub fn into_inner_data(self) -> Vec<u8>

Get the inner data that has been received from the writer

Source

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

Get an OwnedHandle containing the Sink

Trait Implementations§

Source§

impl Debug for Sink

Source§

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

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

impl Default for Sink

Source§

fn default() -> Sink

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

impl ErrorType for Sink

Source§

type Error = MockError

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

impl Write for Sink

Source§

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

Write a buffer into this writer, returning how many bytes were written. Read more
Source§

fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, blocking until all intermediately buffered contents reach their destination.
Source§

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

Write an entire buffer into this writer. Read more
Source§

fn write_fmt( &mut self, fmt: Arguments<'_>, ) -> Result<(), WriteFmtError<Self::Error>>

Write a formatted string into this writer, returning any error encountered. Read more
Source§

impl Write for Sink

Source§

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

Write a buffer into this writer, returning how many bytes were written. Read more
Source§

async fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
Source§

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

Write an entire buffer into this writer. Read more

Auto Trait Implementations§

§

impl Freeze for Sink

§

impl RefUnwindSafe for Sink

§

impl Send for Sink

§

impl Sync for Sink

§

impl Unpin for Sink

§

impl UnwindSafe for Sink

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.