io: add AsyncReadExt::{chain, take} (#1484)

This commit is contained in:
Taiki Endo
2019-08-20 20:09:07 -07:00
committed by Carl Lerche
parent a791f4a758
commit 24fb33e012
6 changed files with 320 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
#![warn(rust_2018_idioms)]
#![feature(async_await)]
use tokio_io::AsyncReadExt;
use tokio_test::assert_ok;
#[tokio::test]
async fn chain() {
let mut buf = Vec::new();
let rd1: &[u8] = b"hello ";
let rd2: &[u8] = b"world";
let mut rd = rd1.chain(rd2);
assert_ok!(rd.read_to_end(&mut buf).await);
assert_eq!(buf, b"hello world");
}
+16
View File
@@ -0,0 +1,16 @@
#![warn(rust_2018_idioms)]
#![feature(async_await)]
use tokio_io::AsyncReadExt;
use tokio_test::assert_ok;
#[tokio::test]
async fn take() {
let mut buf = [0; 6];
let rd: &[u8] = b"hello world";
let mut rd = rd.take(4);
let n = assert_ok!(rd.read(&mut buf).await);
assert_eq!(n, 4);
assert_eq!(&buf, &b"hell\0\0"[..]);
}