Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpened-nacho authored Jul 1, 2024
2 parents 1e9afc7 + dff4ecd commit 2d9edcd
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 2d9edcd

Please sign in to comment.