diff options
author | Taiki Endo <te316e89@gmail.com> | 2019-07-15 15:21:09 +0900 |
---|---|---|
committer | Sean McArthur <sean@seanmonstar.com> | 2019-07-15 11:52:13 -0700 |
commit | 0cfa120ba8c93822d6a84f2e7392008d84622010 (patch) | |
tree | 6926d3b627186f8e6134c779a8629d242f068913 /tokio/tests/io_read_until.rs | |
parent | 5774a9cd64b56d703f743fe07ff3671c1af56d08 (diff) |
tokio: add AsyncBufReadExt::read_until
Diffstat (limited to 'tokio/tests/io_read_until.rs')
-rw-r--r-- | tokio/tests/io_read_until.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tokio/tests/io_read_until.rs b/tokio/tests/io_read_until.rs new file mode 100644 index 00000000..22945f55 --- /dev/null +++ b/tokio/tests/io_read_until.rs @@ -0,0 +1,56 @@ +#![deny(warnings, rust_2018_idioms)] +#![feature(async_await)] + +use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncRead}; +use tokio_test::assert_ok; + +use std::io; +use std::pin::Pin; +use std::task::{Context, Poll}; + +#[tokio::test] +async fn read_until() { + struct Rd { + val: &'static [u8], + } + + impl AsyncRead for Rd { + fn poll_read( + self: Pin<&mut Self>, + _: &mut Context<'_>, + _: &mut [u8], + ) -> Poll<io::Result<usize>> { + unimplemented!() + } + } + + impl AsyncBufRead for Rd { + fn poll_fill_buf<'a>( + self: Pin<&'a mut Self>, + _: &mut Context<'_>, + ) -> Poll<io::Result<&'a [u8]>> { + Poll::Ready(Ok(self.val)) + } + + fn consume(mut self: Pin<&mut Self>, amt: usize) { + self.val = &self.val[amt..]; + } + } + + let mut buf = vec![]; + let mut rd = Rd { + val: b"hello world", + }; + + let n = assert_ok!(rd.read_until(b' ', &mut buf).await); + assert_eq!(n, 6); + assert_eq!(buf, b"hello "); + buf.clear(); + let n = assert_ok!(rd.read_until(b' ', &mut buf).await); + assert_eq!(n, 5); + assert_eq!(buf, b"world"); + buf.clear(); + let n = assert_ok!(rd.read_until(b' ', &mut buf).await); + assert_eq!(n, 0); + assert_eq!(buf, []); +} |