-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
tokio: update inner.pos for tokio::fs::File for read, write operations
#7297
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c0021ef
tokio: initial
suryakantsoni-cb db8b43b
tokio:unit tests: add pos update check
Suryakant-Soni ae1663b
tokio:cargo.toml change: revert
Suryakant-Soni 9d00d4a
tokio:issue-6374: changes for write case
Suryakant-Soni c11869b
tokio:issue-6374: small fixes in write
Suryakant-Soni 5f5ab68
tokio:issue-6374: test fix for write
Suryakant-Soni 8ddb487
tokio:issue-6374: clippy ci change
Suryakant-Soni 597e2dc
tokio:issue-6374: change test to use stream_position
Suryakant-Soni 4b1b248
tokio:issue-6374: remove extra log
Suryakant-Soni f1a2d06
tokio:issue-6374: trivial
Suryakant-Soni 542f28e
tokio:issue-6374: adding negative test cases
Suryakant-Soni e5e4389
tokio:issue-6374: trivial
Suryakant-Soni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,7 +113,7 @@ enum State { | |
| #[derive(Debug)] | ||
| enum Operation { | ||
| Read(io::Result<usize>), | ||
| Write(io::Result<()>), | ||
| Write(io::Result<usize>), | ||
| Seek(io::Result<u64>), | ||
| } | ||
|
|
||
|
|
@@ -590,14 +590,14 @@ impl AsyncRead for File { | |
|
|
||
| let me = self.get_mut(); | ||
| let inner = me.inner.get_mut(); | ||
|
|
||
| loop { | ||
| match inner.state { | ||
| State::Idle(ref mut buf_cell) => { | ||
| let mut buf = buf_cell.take().unwrap(); | ||
|
|
||
| if !buf.is_empty() || dst.remaining() == 0 { | ||
| buf.copy_to(dst); | ||
| let copied = buf.copy_to(dst); | ||
| // Update position based on bytes copied into the user's buffer | ||
| inner.pos += copied as u64; | ||
| *buf_cell = Some(buf); | ||
| return Poll::Ready(Ok(())); | ||
| } | ||
|
|
@@ -617,9 +617,11 @@ impl AsyncRead for File { | |
| let (op, mut buf) = ready!(Pin::new(rx).poll(cx))?; | ||
|
|
||
| match op { | ||
| Operation::Read(Ok(_)) => { | ||
| Operation::Read(Ok(n)) => { | ||
| buf.copy_to(dst); | ||
| inner.state = State::Idle(Some(buf)); | ||
| // Update position by `n` bytes returned from the read operation | ||
| inner.pos += n as u64; | ||
| return Poll::Ready(Ok(())); | ||
| } | ||
| Operation::Read(Err(e)) => { | ||
|
|
@@ -628,7 +630,7 @@ impl AsyncRead for File { | |
| inner.state = State::Idle(Some(buf)); | ||
| return Poll::Ready(Err(e)); | ||
| } | ||
| Operation::Write(Ok(())) => { | ||
| Operation::Write(Ok(_)) => { | ||
| assert!(buf.is_empty()); | ||
| inner.state = State::Idle(Some(buf)); | ||
| continue; | ||
|
|
@@ -742,14 +744,18 @@ impl AsyncWrite for File { | |
| None | ||
| }; | ||
|
|
||
| let n = buf.copy_from(src, me.max_buf_size); | ||
| _ = buf.copy_from(src, me.max_buf_size); | ||
| let std = me.std.clone(); | ||
|
|
||
| let blocking_task_join_handle = spawn_mandatory_blocking(move || { | ||
| let n = buf.len(); | ||
| let res = if let Some(seek) = seek { | ||
| (&*std).seek(seek).and_then(|_| buf.write_to(&mut &*std)) | ||
| (&*std) | ||
| .seek(seek) | ||
| .and_then(|_| buf.write_to(&mut &*std)) | ||
| .map(|_| n) | ||
| } else { | ||
| buf.write_to(&mut &*std) | ||
| buf.write_to(&mut &*std).map(|_| n) | ||
| }; | ||
|
|
||
| (Operation::Write(res), buf) | ||
|
|
@@ -759,8 +765,6 @@ impl AsyncWrite for File { | |
| })?; | ||
|
|
||
| inner.state = State::Busy(blocking_task_join_handle); | ||
|
|
||
| return Poll::Ready(Ok(n)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning Poll::Ready(Ok(n)) is now deferred to the next polling iterations, allowing inner.pos to be updated post spawn task completion |
||
| } | ||
| State::Busy(ref mut rx) => { | ||
| let (op, buf) = ready!(Pin::new(rx).poll(cx))?; | ||
|
|
@@ -774,10 +778,15 @@ impl AsyncWrite for File { | |
| continue; | ||
| } | ||
| Operation::Write(res) => { | ||
| // If the previous write was successful, continue. | ||
| // If the previous write was successful, Update position & return | ||
| // Otherwise, error. | ||
| res?; | ||
| continue; | ||
| match res { | ||
| Ok(n) => { | ||
| inner.pos += n as u64; | ||
| return Poll::Ready(Ok(n)); | ||
| } | ||
| Err(e) => return Poll::Ready(Err(e)), | ||
| } | ||
| } | ||
| Operation::Seek(_) => { | ||
| // Ignore the seek | ||
|
|
@@ -813,14 +822,18 @@ impl AsyncWrite for File { | |
| None | ||
| }; | ||
|
|
||
| let n = buf.copy_from_bufs(bufs, me.max_buf_size); | ||
| _ = buf.copy_from_bufs(bufs, me.max_buf_size); | ||
| let std = me.std.clone(); | ||
|
|
||
| let blocking_task_join_handle = spawn_mandatory_blocking(move || { | ||
| let n = buf.len(); | ||
| let res = if let Some(seek) = seek { | ||
| (&*std).seek(seek).and_then(|_| buf.write_to(&mut &*std)) | ||
| (&*std) | ||
| .seek(seek) | ||
| .and_then(|_| buf.write_to(&mut &*std)) | ||
| .map(|_| n) | ||
| } else { | ||
| buf.write_to(&mut &*std) | ||
| buf.write_to(&mut &*std).map(|_| n) | ||
| }; | ||
|
|
||
| (Operation::Write(res), buf) | ||
|
|
@@ -830,8 +843,6 @@ impl AsyncWrite for File { | |
| })?; | ||
|
|
||
| inner.state = State::Busy(blocking_task_join_handle); | ||
|
|
||
| return Poll::Ready(Ok(n)); | ||
| } | ||
| State::Busy(ref mut rx) => { | ||
| let (op, buf) = ready!(Pin::new(rx).poll(cx))?; | ||
|
|
@@ -845,10 +856,15 @@ impl AsyncWrite for File { | |
| continue; | ||
| } | ||
| Operation::Write(res) => { | ||
| // If the previous write was successful, continue. | ||
| // If the previous write was successful, Update position and return | ||
| // Otherwise, error. | ||
| res?; | ||
| continue; | ||
| match res { | ||
| Ok(n) => { | ||
| inner.pos += n as u64; | ||
| return Poll::Ready(Ok(n)); | ||
| } | ||
| Err(e) => return Poll::Ready(Err(e)), | ||
| } | ||
| } | ||
| Operation::Seek(_) => { | ||
| // Ignore the seek | ||
|
|
@@ -973,7 +989,7 @@ impl Inner { | |
|
|
||
| match op { | ||
| Operation::Read(_) => Poll::Ready(Ok(())), | ||
| Operation::Write(res) => Poll::Ready(res), | ||
| Operation::Write(_) => Poll::Ready(Ok(())), | ||
| Operation::Seek(_) => Poll::Ready(Ok(())), | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changing Write enum variant from Write(io::Result<()>) to Write(io::Result) so that the pos usize value can be propagated out from spawn_blocking closure