|
3 | 3 | use futures_io::AsyncRead;
|
4 | 4 |
|
5 | 5 | pub use super::interleave_pending::InterleavePending;
|
| 6 | +pub use super::limited::Limited; |
6 | 7 |
|
7 | 8 | /// Additional combinators for testing async readers.
|
8 | 9 | pub trait AsyncReadTestExt: AsyncRead {
|
@@ -72,6 +73,41 @@ pub trait AsyncReadTestExt: AsyncRead {
|
72 | 73 | {
|
73 | 74 | InterleavePending::new(self)
|
74 | 75 | }
|
| 76 | + |
| 77 | + /// Limit the number of bytes allowed to be read on each call to `poll_read`. |
| 78 | + /// |
| 79 | + /// # Examples |
| 80 | + /// |
| 81 | + /// ``` |
| 82 | + /// #![feature(async_await)] |
| 83 | + /// use futures::task::Poll; |
| 84 | + /// use futures::io::AsyncRead; |
| 85 | + /// use futures_test::task::noop_context; |
| 86 | + /// use futures_test::io::AsyncReadTestExt; |
| 87 | + /// use pin_utils::pin_mut; |
| 88 | + /// |
| 89 | + /// let reader = std::io::Cursor::new(&[1, 2, 3, 4, 5]).limited(2); |
| 90 | + /// pin_mut!(reader); |
| 91 | + /// |
| 92 | + /// let mut cx = noop_context(); |
| 93 | + /// |
| 94 | + /// let mut buf = [0; 10]; |
| 95 | + /// |
| 96 | + /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf)?, Poll::Ready(2)); |
| 97 | + /// assert_eq!(&buf[..2], &[1, 2]); |
| 98 | + /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf)?, Poll::Ready(2)); |
| 99 | + /// assert_eq!(&buf[..2], &[3, 4]); |
| 100 | + /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf)?, Poll::Ready(1)); |
| 101 | + /// assert_eq!(&buf[..1], &[5]); |
| 102 | + /// |
| 103 | + /// # Ok::<(), std::io::Error>(()) |
| 104 | + /// ``` |
| 105 | + fn limited(self, limit: usize) -> Limited<Self> |
| 106 | + where |
| 107 | + Self: Sized, |
| 108 | + { |
| 109 | + Limited::new(self, limit) |
| 110 | + } |
75 | 111 | }
|
76 | 112 |
|
77 | 113 | impl<R> AsyncReadTestExt for R where R: AsyncRead {}
|
0 commit comments