io: implement AsyncSeek for Empty (#6663)

* io: implement `AsyncSeek` for `Empty`

* io: add more tests for seeking `Empty`

This adds more tests seeking with other kinds of `SeekFrom`. It follows
the same structure as used in
rust-lang/rust@f1cd17961c.

See <https://github.com/tokio-rs/tokio/pull/6663#discussion_r1659491016>.

* io: add inline attribute to `AsyncSeek` implementations

This follows the style of the other trait implementations of `Empty`.

See <https://github.com/tokio-rs/tokio/pull/6663#discussion_r1658835942>.

---------

Co-authored-by: Weijia Jiang <wjjiang19@gmail.com>
This commit is contained in:
Michael Macias 2024-06-30 20:50:49 -05:00 committed by GitHub
parent 68d0e3cb5f
commit dff4ecd0e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 3 deletions

View File

@ -1,8 +1,8 @@
use crate::io::util::poll_proceed_and_make_progress;
use crate::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
use crate::io::{AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
use std::fmt;
use std::io;
use std::io::{self, SeekFrom};
use std::pin::Pin;
use std::task::{Context, Poll};
@ -133,6 +133,20 @@ impl AsyncWrite for Empty {
}
}
impl AsyncSeek for Empty {
#[inline]
fn start_seek(self: Pin<&mut Self>, _position: SeekFrom) -> io::Result<()> {
Ok(())
}
#[inline]
fn poll_complete(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
ready!(crate::trace::trace_leaf(cx));
ready!(poll_proceed_and_make_progress(cx));
Poll::Ready(Ok(0))
}
}
impl fmt::Debug for Empty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad("Empty { .. }")

View File

@ -1,5 +1,5 @@
#![cfg(feature = "full")]
use tokio::io::{AsyncBufReadExt, AsyncReadExt};
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncSeekExt};
#[tokio::test]
async fn empty_read_is_cooperative() {
@ -30,3 +30,32 @@ async fn empty_buf_reads_are_cooperative() {
_ = tokio::task::yield_now() => {}
}
}
#[tokio::test]
async fn empty_seek() {
use std::io::SeekFrom;
let mut empty = tokio::io::empty();
assert!(matches!(empty.seek(SeekFrom::Start(0)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::Start(1)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::Start(u64::MAX)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::End(i64::MIN)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::End(-1)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::End(0)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::End(1)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::End(i64::MAX)).await, Ok(0)));
assert!(matches!(
empty.seek(SeekFrom::Current(i64::MIN)).await,
Ok(0)
));
assert!(matches!(empty.seek(SeekFrom::Current(-1)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::Current(0)).await, Ok(0)));
assert!(matches!(empty.seek(SeekFrom::Current(1)).await, Ok(0)));
assert!(matches!(
empty.seek(SeekFrom::Current(i64::MAX)).await,
Ok(0)
));
}