mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
fs: add deprecated fs::File::seek fn (#1991)
This fixes an API compatibility regression when `AsyncSeek` was added. Fixes: #1989
This commit is contained in:
+1
-1
@@ -12,5 +12,5 @@ jobs:
|
|||||||
cargo clippy --version
|
cargo clippy --version
|
||||||
displayName: Install clippy
|
displayName: Install clippy
|
||||||
- script: |
|
- 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
|
displayName: cargo clippy --all
|
||||||
|
|||||||
@@ -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.
|
/// Attempts to sync all OS-internal metadata to disk.
|
||||||
///
|
///
|
||||||
/// This function will attempt to ensure that all in-core data reaches the
|
/// This function will attempt to ensure that all in-core data reaches the
|
||||||
|
|||||||
Reference in New Issue
Block a user