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>
impl<'a, T: Debug> Debug for OwnedHandle<'a, T>
Source§impl<T: ErrorType> ErrorType for OwnedHandle<'_, T>
impl<T: ErrorType> ErrorType for OwnedHandle<'_, T>
Source§impl<T: Read> Read for OwnedHandle<'_, T>
impl<T: Read> Read for OwnedHandle<'_, T>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
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>>
fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
Read the exact number of bytes required to fill
buf. Read moreSource§impl<T: Read> Read for OwnedHandle<'_, T>
impl<T: Read> Read for OwnedHandle<'_, T>
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>
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>>
async fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
Read the exact number of bytes required to fill
buf. Read moreSource§impl<T: Write> Write for OwnedHandle<'_, T>
impl<T: Write> Write for OwnedHandle<'_, T>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>
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>
fn flush(&mut self) -> Result<(), Self::Error>
Flush this output stream, blocking until all intermediately buffered contents reach their destination.
Source§impl<T: Write> Write for OwnedHandle<'_, T>
impl<T: Write> Write for OwnedHandle<'_, T>
Source§async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>
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
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more