diff options
Diffstat (limited to 'tests/io_write.rs')
-rw-r--r-- | tests/io_write.rs | 70 |
1 files changed, 45 insertions, 25 deletions
diff --git a/tests/io_write.rs b/tests/io_write.rs index b963444..af0d5c3 100644 --- a/tests/io_write.rs +++ b/tests/io_write.rs @@ -1,41 +1,52 @@ -use futures::io::AsyncWrite; -use futures_test::task::panic_context; -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; +#[cfg(feature = "std")] +mod mock_writer { + use futures::io::AsyncWrite; + use std::io; + use std::pin::Pin; + use std::task::{Context, Poll}; -struct MockWriter { - fun: Box<dyn FnMut(&[u8]) -> Poll<io::Result<usize>>>, -} - -impl MockWriter { - pub fn new(fun: impl FnMut(&[u8]) -> Poll<io::Result<usize>> + 'static) -> Self { - MockWriter { fun: Box::new(fun) } + pub struct MockWriter { + fun: Box<dyn FnMut(&[u8]) -> Poll<io::Result<usize>>>, } -} -impl AsyncWrite for MockWriter { - fn poll_write( - self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &[u8], - ) -> Poll<io::Result<usize>> { - (self.get_mut().fun)(buf) + impl MockWriter { + pub fn new(fun: impl FnMut(&[u8]) -> Poll<io::Result<usize>> + 'static) -> Self { + MockWriter { fun: Box::new(fun) } + } } - fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { - panic!() - } + impl AsyncWrite for MockWriter { + fn poll_write( + self: Pin<&mut Self>, + _cx: &mut Context<'_>, + buf: &[u8], + ) -> Poll<io::Result<usize>> { + (self.get_mut().fun)(buf) + } + + fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { + panic!() + } - fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { - panic!() + fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { + panic!() + } } } /// Verifies that the default implementation of `poll_write_vectored` /// calls `poll_write` with an empty slice if no buffers are provided. +#[cfg(feature = "std")] #[test] fn write_vectored_no_buffers() { + use futures::io::AsyncWrite; + use futures_test::task::panic_context; + use std::io; + use std::pin::Pin; + use std::task::Poll; + + use mock_writer::MockWriter; + let mut writer = MockWriter::new(|buf| { assert_eq!(buf, b""); Err(io::ErrorKind::BrokenPipe.into()).into() @@ -50,8 +61,17 @@ fn write_vectored_no_buffers() { /// Verifies that the default implementation of `poll_write_vectored` /// calls `poll_write` with the first non-empty buffer. +#[cfg(feature = "std")] #[test] fn write_vectored_first_non_empty() { + use futures::io::AsyncWrite; + use futures_test::task::panic_context; + use std::io; + use std::pin::Pin; + use std::task::Poll; + + use mock_writer::MockWriter; + let mut writer = MockWriter::new(|buf| { assert_eq!(buf, b"four"); Poll::Ready(Ok(4)) |