summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2019-12-19 13:37:10 -0800
committerGitHub <noreply@github.com>2019-12-19 13:37:10 -0800
commit93ab70a9a0eca676ef718f02482d3fa0bdd760fd (patch)
treebed54e2254a406ca3fdb9277235045d4b7faeb1a
parent58b5abdb99f113152e9953b0576c4c2fd0aaab99 (diff)
fs: add deprecated fs::File::seek fn (#1991)
This fixes an API compatibility regression when `AsyncSeek` was added. Fixes: #1989
-rw-r--r--ci/azure-clippy.yml2
-rw-r--r--tokio/src/fs/file.rs57
2 files changed, 58 insertions, 1 deletions
diff --git a/ci/azure-clippy.yml b/ci/azure-clippy.yml
index c4361556..349cd5d6 100644
--- a/ci/azure-clippy.yml
+++ b/ci/azure-clippy.yml
@@ -12,5 +12,5 @@ jobs:
cargo clippy --version
displayName: Install clippy
- script: |
- cargo clippy --all --all-features -- -A clippy::mutex-atomic
+ cargo clippy --all --all-features -- -A clippy::mutex-atomic -A clippy::needless-doctest-main
displayName: cargo clippy --all
diff --git a/tokio/src/fs/file.rs b/tokio/src/fs/file.rs
index 20139ec1..a2f64e56 100644
--- a/tokio/src/fs/file.rs
+++ b/tokio/src/fs/file.rs
@@ -176,6 +176,63 @@ impl File {
}
}
+ /// Seek to an offset, in bytes, in a stream.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use tokio::fs::File;
+ /// use tokio::prelude::*;
+ ///
+ /// use std::io::SeekFrom;
+ ///
+ /// # async fn dox() -> std::io::Result<()> {
+ /// let mut file = File::open("foo.txt").await?;
+ /// file.seek(SeekFrom::Start(6)).await?;
+ ///
+ /// let mut contents = vec![0u8; 10];
+ /// file.read_exact(&mut contents).await?;
+ /// # Ok(())
+ /// # }
+ /// ```
+ pub async fn seek(&mut self, mut pos: SeekFrom) -> io::Result<u64> {
+ self.complete_inflight().await;
+
+ let mut buf = match self.state {
+ Idle(ref mut buf_cell) => buf_cell.take().unwrap(),
+ _ => unreachable!(),
+ };
+
+ // Factor in any unread data from the buf
+ if !buf.is_empty() {
+ let n = buf.discard_read();
+
+ if let SeekFrom::Current(ref mut offset) = pos {
+ *offset += n;
+ }
+ }
+
+ let std = self.std.clone();
+
+ // Start the operation
+ self.state = Busy(sys::run(move || {
+ let res = (&*std).seek(pos);
+ (Operation::Seek(res), buf)
+ }));
+
+ let (op, buf) = match self.state {
+ Idle(_) => unreachable!(),
+ Busy(ref mut rx) => rx.await.unwrap(),
+ };
+
+ self.state = Idle(Some(buf));
+
+ match op {
+ Operation::Seek(res) => res,
+ _ => unreachable!(),
+ }
+ }
+
/// Attempts to sync all OS-internal metadata to disk.
///
/// This function will attempt to ensure that all in-core data reaches the