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

Fix futures_io::AsyncSeek implementaion for Compat #5783

Merged
merged 7 commits into from
Jun 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions tokio-util/src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,18 @@ impl<T: tokio::io::AsyncSeek> futures_io::AsyncSeek for Compat<T> {
cx: &mut Context<'_>,
pos: io::SeekFrom,
) -> Poll<io::Result<u64>> {
if self.seek_pos != Some(pos) {
self.as_mut().project().inner.start_seek(pos)?;
*self.as_mut().project().seek_pos = Some(pos);
match self.as_mut().project().inner.poll_complete(cx) {
dhruv9vats marked this conversation as resolved.
Show resolved Hide resolved
Poll::Ready(_) => {
if self.seek_pos != Some(pos) {
self.as_mut().project().inner.start_seek(pos)?;
*self.as_mut().project().seek_pos = Some(pos);
}
let res = ready!(self.as_mut().project().inner.poll_complete(cx));
*self.as_mut().project().seek_pos = None;
Poll::Ready(res.map(|p| p as u64))
}
Poll::Pending => Poll::Pending,
}
let res = ready!(self.as_mut().project().inner.poll_complete(cx));
*self.as_mut().project().seek_pos = None;
Poll::Ready(res.map(|p| p as u64))
}
}

Expand Down
44 changes: 44 additions & 0 deletions tokio-util/tests/compat.rs
dhruv9vats marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![cfg(feature = "compat")]
#![warn(rust_2018_idioms)]

use futures_util::io;

async fn tokio_stream_position() -> io::Result<()> {
use tokio::fs::File;
use tokio::io::{AsyncSeekExt, AsyncWriteExt};
let path = "../target/tokio-seek.test";

let mut file = File::create(path).await?;
let buffer = [0u8; 16];
file.write_all(&buffer).await?;

let pos = file.stream_position().await?;
println!("tokio got pos {}", pos);

Ok(())
}

async fn compat_stream_position() -> io::Result<()> {
use futures::io::{AsyncSeekExt, AsyncWriteExt};
use tokio::fs::File;
use tokio_util::compat::TokioAsyncWriteCompatExt;
let path = "../target/compat-seek.test";

let mut file = File::create(path).await?.compat_write();
let buffer = [0u8; 16];
file.write_all(&buffer).await?;

// Error: other file operation is pending,
// call poll_complete before start_seek
let pos = file.stream_position().await?;
println!("futures got pos {}", pos);

Ok(())
}

#[tokio::test]
async fn combined() -> io::Result<()> {
tokio_stream_position().await?;
compat_stream_position().await?;
Ok(())
}