mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-02 00:00:11 +02:00
fs: empty reads on File should not start a background read (#7139)
This commit is contained in:
@@ -587,6 +587,7 @@ impl AsyncRead for File {
|
|||||||
dst: &mut ReadBuf<'_>,
|
dst: &mut ReadBuf<'_>,
|
||||||
) -> Poll<io::Result<()>> {
|
) -> Poll<io::Result<()>> {
|
||||||
ready!(crate::trace::trace_leaf(cx));
|
ready!(crate::trace::trace_leaf(cx));
|
||||||
|
|
||||||
let me = self.get_mut();
|
let me = self.get_mut();
|
||||||
let inner = me.inner.get_mut();
|
let inner = me.inner.get_mut();
|
||||||
|
|
||||||
@@ -595,7 +596,7 @@ impl AsyncRead for File {
|
|||||||
State::Idle(ref mut buf_cell) => {
|
State::Idle(ref mut buf_cell) => {
|
||||||
let mut buf = buf_cell.take().unwrap();
|
let mut buf = buf_cell.take().unwrap();
|
||||||
|
|
||||||
if !buf.is_empty() {
|
if !buf.is_empty() || dst.remaining() == 0 {
|
||||||
buf.copy_to(dst);
|
buf.copy_to(dst);
|
||||||
*buf_cell = Some(buf);
|
*buf_cell = Some(buf);
|
||||||
return Poll::Ready(Ok(()));
|
return Poll::Ready(Ok(()));
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#![warn(rust_2018_idioms)]
|
#![warn(rust_2018_idioms)]
|
||||||
#![cfg(all(feature = "full", not(target_os = "wasi")))] // WASI does not support all fs operations
|
#![cfg(all(feature = "full", not(target_os = "wasi")))] // WASI does not support all fs operations
|
||||||
|
|
||||||
|
use futures::future::FutureExt;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::io::IoSlice;
|
use std::io::IoSlice;
|
||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
@@ -176,6 +177,24 @@ async fn read_file_from_std() {
|
|||||||
assert_eq!(&buf[..n], HELLO);
|
assert_eq!(&buf[..n], HELLO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn empty_read() {
|
||||||
|
let mut tempfile = tempfile();
|
||||||
|
tempfile.write_all(HELLO).unwrap();
|
||||||
|
|
||||||
|
let mut file = File::open(tempfile.path()).await.unwrap();
|
||||||
|
|
||||||
|
// Perform an empty read and get a length of zero.
|
||||||
|
assert!(matches!(file.read(&mut []).now_or_never(), Some(Ok(0))));
|
||||||
|
|
||||||
|
// Check that we don't get EOF on the next read.
|
||||||
|
let mut buf = [0; 1024];
|
||||||
|
let n = file.read(&mut buf).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(n, HELLO.len());
|
||||||
|
assert_eq!(&buf[..n], HELLO);
|
||||||
|
}
|
||||||
|
|
||||||
fn tempfile() -> NamedTempFile {
|
fn tempfile() -> NamedTempFile {
|
||||||
NamedTempFile::new().unwrap()
|
NamedTempFile::new().unwrap()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user