Skip to content

Commit

Permalink
io: implement AsyncSeek for Empty (#6663)
Browse files Browse the repository at this point in the history
* 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@f1cd179.

See <#6663 (comment)>.

* io: add inline attribute to `AsyncSeek` implementations

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

See <#6663 (comment)>.

---------

Co-authored-by: Weijia Jiang <[email protected]>
  • Loading branch information
zaeleus and wathenjiang committed Jul 1, 2024
1 parent 68d0e3c commit dff4ecd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
18 changes: 16 additions & 2 deletions tokio/src/io/util/empty.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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 { .. }")
Expand Down
31 changes: 30 additions & 1 deletion tokio/tests/io_util_empty.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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)
));
}

0 comments on commit dff4ecd

Please sign in to comment.