mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-19 00:00:09 +02:00
io: implement AsyncBufRead for &[u8] and Cursor (#1397)
* `impl AsyncRead for &[u8]` * `impl AsyncBufRead for &[u8]` * `impl<T: AsRef<[u8]> + Unpin> AsyncRead for Cursor<T>` * `impl<T: AsRef<[u8]> + Unpin> AsyncBufRead for Cursor<T>`
This commit is contained in:
@@ -92,3 +92,29 @@ where
|
||||
self.get_mut().as_mut().consume(amt)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncBufRead for &[u8] {
|
||||
fn poll_fill_buf<'a>(
|
||||
self: Pin<&'a mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
) -> Poll<io::Result<&'a [u8]>> {
|
||||
Poll::Ready(Ok(*self))
|
||||
}
|
||||
|
||||
fn consume(mut self: Pin<&mut Self>, amt: usize) {
|
||||
*self = &self[amt..];
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsRef<[u8]> + Unpin> AsyncBufRead for io::Cursor<T> {
|
||||
fn poll_fill_buf<'a>(
|
||||
self: Pin<&'a mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
) -> Poll<io::Result<&'a [u8]>> {
|
||||
Poll::Ready(io::BufRead::fill_buf(self.get_mut()))
|
||||
}
|
||||
|
||||
fn consume(self: Pin<&mut Self>, amt: usize) {
|
||||
io::BufRead::consume(self.get_mut(), amt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,3 +155,31 @@ where
|
||||
self.get_mut().as_mut().poll_read(cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncRead for &[u8] {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [u8]) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
Poll::Ready(io::Read::read(self.get_mut(), buf))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AsRef<[u8]> + Unpin> AsyncRead for io::Cursor<T> {
|
||||
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [u8]) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
Poll::Ready(io::Read::read(self.get_mut(), buf))
|
||||
}
|
||||
}
|
||||
|
||||
+2
-35
@@ -2,45 +2,12 @@
|
||||
#![feature(async_await)]
|
||||
|
||||
use futures_util::StreamExt;
|
||||
use tokio_io::{AsyncBufRead, AsyncBufReadExt, AsyncRead};
|
||||
use tokio_io::AsyncBufReadExt;
|
||||
use tokio_test::assert_ok;
|
||||
|
||||
use std::io;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
#[tokio::test]
|
||||
async fn lines() {
|
||||
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 rd = Rd {
|
||||
val: b"hello\r\nworld\n\n",
|
||||
};
|
||||
let rd: &[u8] = b"hello\r\nworld\n\n";
|
||||
let mut st = rd.lines();
|
||||
|
||||
let b = assert_ok!(st.next().await.unwrap());
|
||||
|
||||
@@ -1,37 +1,13 @@
|
||||
#![deny(warnings, rust_2018_idioms)]
|
||||
#![feature(async_await)]
|
||||
|
||||
use tokio_io::{AsyncRead, AsyncReadExt};
|
||||
use tokio_io::AsyncReadExt;
|
||||
use tokio_test::assert_ok;
|
||||
|
||||
use std::io;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_exact() {
|
||||
struct Rd {
|
||||
val: &'static [u8; 11],
|
||||
}
|
||||
|
||||
impl AsyncRead for Rd {
|
||||
fn poll_read(
|
||||
mut self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> 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]);
|
||||
let mut rd = Rd {
|
||||
val: b"hello world",
|
||||
};
|
||||
let mut rd: &[u8] = b"hello world";
|
||||
|
||||
let n = assert_ok!(rd.read_exact(&mut buf[..]).await);
|
||||
assert_eq!(n, 8);
|
||||
|
||||
@@ -1,46 +1,14 @@
|
||||
#![deny(warnings, rust_2018_idioms)]
|
||||
#![feature(async_await)]
|
||||
|
||||
use tokio_io::{AsyncBufRead, AsyncBufReadExt, AsyncRead};
|
||||
use std::io::Cursor;
|
||||
use tokio_io::AsyncBufReadExt;
|
||||
use tokio_test::assert_ok;
|
||||
|
||||
use std::io;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_line() {
|
||||
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 = String::new();
|
||||
let mut rd = Rd {
|
||||
val: b"hello\nworld\n\n",
|
||||
};
|
||||
let mut rd = Cursor::new(b"hello\nworld\n\n");
|
||||
|
||||
let n = assert_ok!(rd.read_line(&mut buf).await);
|
||||
assert_eq!(n, 6);
|
||||
|
||||
@@ -1,38 +1,13 @@
|
||||
#![deny(warnings, rust_2018_idioms)]
|
||||
#![feature(async_await)]
|
||||
|
||||
use tokio_io::{AsyncRead, AsyncReadExt};
|
||||
use tokio_io::AsyncReadExt;
|
||||
use tokio_test::assert_ok;
|
||||
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::{cmp, io};
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_to_end() {
|
||||
struct Rd {
|
||||
val: &'static [u8],
|
||||
}
|
||||
|
||||
impl AsyncRead for Rd {
|
||||
fn poll_read(
|
||||
mut self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
let me = &mut *self;
|
||||
let len = cmp::min(buf.len(), me.val.len());
|
||||
|
||||
buf[..len].copy_from_slice(&me.val[..len]);
|
||||
me.val = &me.val[len..];
|
||||
Poll::Ready(Ok(len))
|
||||
}
|
||||
}
|
||||
|
||||
let mut buf = vec![];
|
||||
let mut rd = Rd {
|
||||
val: b"hello world",
|
||||
};
|
||||
let mut rd: &[u8] = b"hello world";
|
||||
|
||||
let n = assert_ok!(rd.read_to_end(&mut buf).await);
|
||||
assert_eq!(n, 11);
|
||||
|
||||
@@ -1,38 +1,13 @@
|
||||
#![deny(warnings, rust_2018_idioms)]
|
||||
#![feature(async_await)]
|
||||
|
||||
use tokio_io::{AsyncRead, AsyncReadExt};
|
||||
use tokio_io::AsyncReadExt;
|
||||
use tokio_test::assert_ok;
|
||||
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
use std::{cmp, io};
|
||||
|
||||
#[tokio::test]
|
||||
async fn read_to_string() {
|
||||
struct Rd {
|
||||
val: &'static [u8],
|
||||
}
|
||||
|
||||
impl AsyncRead for Rd {
|
||||
fn poll_read(
|
||||
mut self: Pin<&mut Self>,
|
||||
_cx: &mut Context<'_>,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<io::Result<usize>> {
|
||||
let me = &mut *self;
|
||||
let len = cmp::min(buf.len(), me.val.len());
|
||||
|
||||
buf[..len].copy_from_slice(&me.val[..len]);
|
||||
me.val = &me.val[len..];
|
||||
Poll::Ready(Ok(len))
|
||||
}
|
||||
}
|
||||
|
||||
let mut buf = String::new();
|
||||
let mut rd = Rd {
|
||||
val: b"hello world",
|
||||
};
|
||||
let mut rd: &[u8] = b"hello world";
|
||||
|
||||
let n = assert_ok!(rd.read_to_string(&mut buf).await);
|
||||
assert_eq!(n, 11);
|
||||
|
||||
@@ -1,46 +1,13 @@
|
||||
#![deny(warnings, rust_2018_idioms)]
|
||||
#![feature(async_await)]
|
||||
|
||||
use tokio_io::{AsyncBufRead, AsyncBufReadExt, AsyncRead};
|
||||
use tokio_io::AsyncBufReadExt;
|
||||
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 mut rd: &[u8] = b"hello world";
|
||||
|
||||
let n = assert_ok!(rd.read_until(b' ', &mut buf).await);
|
||||
assert_eq!(n, 6);
|
||||
|
||||
Reference in New Issue
Block a user