@@ -5,6 +5,9 @@ use futures_io::AsyncWrite;
5
5
mod interleave_pending_write;
6
6
pub use self::interleave_pending_write::InterleavePendingWrite;
7
7
8
+ mod limited_write;
9
+ pub use self::limited_write::LimitedWrite;
10
+
8
11
/// Additional combinators for testing async writers.
9
12
pub trait AsyncWriteTestExt: AsyncWrite {
10
13
/// Introduces an extra [`Poll::Pending`](futures_core::task::Poll::Pending)
@@ -48,6 +51,40 @@ pub trait AsyncWriteTestExt: AsyncWrite {
48
51
{
49
52
InterleavePendingWrite::new(self)
50
53
}
54
+
55
+ /// Limit the number of bytes allowed to be written on each call to `poll_write`.
56
+ ///
57
+ /// # Examples
58
+ ///
59
+ /// ```
60
+ /// #![feature(async_await)]
61
+ /// use futures::task::Poll;
62
+ /// use futures::io::AsyncWrite;
63
+ /// use futures_test::task::noop_context;
64
+ /// use futures_test::io::AsyncWriteTestExt;
65
+ /// use pin_utils::pin_mut;
66
+ ///
67
+ /// let writer = std::io::Cursor::new([0u8; 4]).limited_write(2);
68
+ /// pin_mut!(writer);
69
+ ///
70
+ /// let mut cx = noop_context();
71
+ ///
72
+ /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[1, 2])?, Poll::Ready(2));
73
+ /// assert_eq!(writer.get_ref().get_ref(), &[1, 2, 0, 0]);
74
+ /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[3])?, Poll::Ready(1));
75
+ /// assert_eq!(writer.get_ref().get_ref(), &[1, 2, 3, 0]);
76
+ /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[4, 5])?, Poll::Ready(1));
77
+ /// assert_eq!(writer.get_ref().get_ref(), &[1, 2, 3, 4]);
78
+ /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[5])?, Poll::Ready(0));
79
+ ///
80
+ /// # Ok::<(), std::io::Error>(())
81
+ /// ```
82
+ fn limited_write(self, limit: usize) -> LimitedWrite<Self>
83
+ where
84
+ Self: Sized,
85
+ {
86
+ LimitedWrite::new(self, limit)
87
+ }
51
88
}
52
89
53
90
impl<W> AsyncWriteTestExt for W where W: AsyncWrite {}
0 commit comments