OwnedHandle

Struct OwnedHandle 

Source
pub struct OwnedHandle<'a, T> { /* private fields */ }
Expand description

An owned handle to a Source or Sink.

It’s common to want an object which owns a type implementing Read or Write. But for testing purposes, the object under test can’t consume the mock, as we might want to verify some expectations on it afterwards. To get around this, an OwnedHandle can be taken from a Source or Sink, which also implements the underlying traits but only contains a mutable reference to the underlying mock.

Once the object under test is dropped, the mock object itself still exists and can be used to verify any expectations.

§Example

use embedded_io::Write;

struct MySerialProtocol<T: embedded_io::Write> {
    serial: T,
}

impl<T: embedded_io::Write> MySerialProtocol<T> {
    fn hello(&mut self) -> Result<(), T::Error> {
        self.serial.write_all("hello".as_bytes())
    }
}

let mut mock_sink = Sink::new().accept_data(64);

// MySerialProtocol requires an owned `serial` type here
let mut my_protocol = MySerialProtocol {
    serial: mock_sink.owned_handle(),
};

let res = my_protocol.hello();
assert!(res.is_ok());

// Because we constructed `my_protocol` with an `OwnedHandle`, we still have access to the
// original `mock_sink` here to verify that the correct bytes were written.
let written = mock_sink.into_inner_data();
assert_eq!(written, "hello".as_bytes());

Trait Implementations§

Source§

impl<'a, T: Debug> Debug for OwnedHandle<'a, T>

Source§

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

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

impl<T: ErrorType> ErrorType for OwnedHandle<'_, T>

Source§

type Error = <T as ErrorType>::Error

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

impl<T: Read> Read for OwnedHandle<'_, T>

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<T: Read> Read for OwnedHandle<'_, T>

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
Source§

impl<T: Write> Write for OwnedHandle<'_, T>

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<T: Write> Write for OwnedHandle<'_, T>

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<'a, T> Freeze for OwnedHandle<'a, T>

§

impl<'a, T> RefUnwindSafe for OwnedHandle<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for OwnedHandle<'a, T>
where T: Send,

§

impl<'a, T> Sync for OwnedHandle<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for OwnedHandle<'a, T>

§

impl<'a, T> !UnwindSafe for OwnedHandle<'a, T>

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.