2019-06-26 14:42:19 -07:00
|
|
|
#![deny(warnings, rust_2018_idioms)]
|
2019-06-26 17:06:56 -07:00
|
|
|
#![feature(async_await)]
|
2019-06-26 14:42:19 -07:00
|
|
|
|
2019-08-02 15:23:44 -04:00
|
|
|
use tokio_io::{AsyncRead, AsyncReadExt};
|
2019-06-26 17:06:56 -07:00
|
|
|
use tokio_test::assert_ok;
|
2019-06-26 14:42:19 -07:00
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
2019-06-26 17:06:56 -07:00
|
|
|
#[tokio::test]
|
|
|
|
|
async fn read_exact() {
|
2019-06-26 14:42:19 -07:00
|
|
|
struct Rd {
|
|
|
|
|
val: &'static [u8; 11],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsyncRead for Rd {
|
|
|
|
|
fn poll_read(
|
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
|
_cx: &mut Context<'_>,
|
2019-06-27 00:05:01 -07:00
|
|
|
buf: &mut [u8],
|
2019-06-26 14:42:19 -07:00
|
|
|
) -> Poll<io::Result<usize>> {
|
|
|
|
|
let me = &mut *self;
|
|
|
|
|
let len = buf.len();
|
|
|
|
|
|
|
|
|
|
buf[..].copy_from_slice(&me.val[..len]);
|
|
|
|
|
Poll::Ready(Ok(buf.len()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut buf = Box::new([0; 8]);
|
2019-06-27 00:05:01 -07:00
|
|
|
let mut rd = Rd {
|
|
|
|
|
val: b"hello world",
|
|
|
|
|
};
|
2019-06-26 14:42:19 -07:00
|
|
|
|
2019-06-26 17:06:56 -07:00
|
|
|
let n = assert_ok!(rd.read_exact(&mut buf[..]).await);
|
|
|
|
|
assert_eq!(n, 8);
|
|
|
|
|
assert_eq!(buf[..], b"hello wo"[..]);
|
2019-06-26 14:42:19 -07:00
|
|
|
}
|