Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: implement AsyncSeek for Empty #6663

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 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,18 @@ impl AsyncWrite for Empty {
}
}

impl AsyncSeek for Empty {
fn start_seek(self: Pin<&mut Self>, _position: SeekFrom) -> io::Result<()> {
zaeleus marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}

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
16 changes: 15 additions & 1 deletion tokio/tests/io_util_empty.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(feature = "full")]
use tokio::io::{AsyncBufReadExt, AsyncReadExt};
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncSeekExt};
use tokio_test::assert_ok;

#[tokio::test]
async fn empty_read_is_cooperative() {
Expand Down Expand Up @@ -30,3 +31,16 @@ async fn empty_buf_reads_are_cooperative() {
_ = tokio::task::yield_now() => {}
}
}

#[tokio::test]
async fn empty_seek() {
zaeleus marked this conversation as resolved.
Show resolved Hide resolved
use std::io::SeekFrom;

let mut empty = tokio::io::empty();

let pos = assert_ok!(empty.seek(SeekFrom::Start(0)).await);
assert_eq!(pos, 0);

let pos = assert_ok!(empty.seek(SeekFrom::Start(8)).await);
assert_eq!(pos, 0);
}
Loading