diff options
author | Carl Lerche <me@carllerche.com> | 2019-12-19 13:37:10 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-19 13:37:10 -0800 |
commit | 93ab70a9a0eca676ef718f02482d3fa0bdd760fd (patch) | |
tree | bed54e2254a406ca3fdb9277235045d4b7faeb1a /tokio/src/fs | |
parent | 58b5abdb99f113152e9953b0576c4c2fd0aaab99 (diff) |
fs: add deprecated fs::File::seek fn (#1991)
This fixes an API compatibility regression when `AsyncSeek` was added.
Fixes: #1989
Diffstat (limited to 'tokio/src/fs')
-rw-r--r-- | tokio/src/fs/file.rs | 57 |
1 files changed, 57 insertions, 0 deletions
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 |