mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
tokio-fs: add tokio_fs::File::seek (#434)
This commit is contained in:
parent
5753553ba3
commit
ecfe2f6a05
@ -6,11 +6,13 @@ mod create;
|
|||||||
mod metadata;
|
mod metadata;
|
||||||
mod open;
|
mod open;
|
||||||
mod open_options;
|
mod open_options;
|
||||||
|
mod seek;
|
||||||
|
|
||||||
pub use self::create::CreateFuture;
|
pub use self::create::CreateFuture;
|
||||||
pub use self::metadata::MetadataFuture;
|
pub use self::metadata::MetadataFuture;
|
||||||
pub use self::open::OpenFuture;
|
pub use self::open::OpenFuture;
|
||||||
pub use self::open_options::OpenOptions;
|
pub use self::open_options::OpenOptions;
|
||||||
|
pub use self::seek::SeekFuture;
|
||||||
|
|
||||||
use tokio_io::{AsyncRead, AsyncWrite};
|
use tokio_io::{AsyncRead, AsyncWrite};
|
||||||
|
|
||||||
@ -100,6 +102,16 @@ impl File {
|
|||||||
::blocking_io(|| self.std().seek(pos))
|
::blocking_io(|| self.std().seek(pos))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Seek to an offset, in bytes, in a stream.
|
||||||
|
///
|
||||||
|
/// Similar to `poll_seek`, but returning a `Future`.
|
||||||
|
///
|
||||||
|
/// This method consumes the `File` and returns it back when the future
|
||||||
|
/// completes.
|
||||||
|
pub fn seek(self, pos: io::SeekFrom) -> SeekFuture {
|
||||||
|
SeekFuture::new(self, pos)
|
||||||
|
}
|
||||||
|
|
||||||
/// 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
|
||||||
|
37
tokio-fs/src/file/seek.rs
Normal file
37
tokio-fs/src/file/seek.rs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
use super::File;
|
||||||
|
|
||||||
|
use futures::{Future, Poll};
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
/// Future returned by `File::seek`
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SeekFuture {
|
||||||
|
inner: Option<File>,
|
||||||
|
pos: io::SeekFrom,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SeekFuture {
|
||||||
|
pub(crate) fn new(file: File, pos: io::SeekFrom) -> Self {
|
||||||
|
Self {
|
||||||
|
pos,
|
||||||
|
inner: Some(file),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Future for SeekFuture {
|
||||||
|
type Item = (File, u64);
|
||||||
|
type Error = io::Error;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
let pos = try_ready!(
|
||||||
|
self.inner
|
||||||
|
.as_mut()
|
||||||
|
.expect("Cannot poll `SeekFuture` after it resolves")
|
||||||
|
.poll_seek(self.pos)
|
||||||
|
);
|
||||||
|
let inner = self.inner.take().unwrap();
|
||||||
|
Ok((inner, pos).into())
|
||||||
|
}
|
||||||
|
}
|
@ -16,7 +16,7 @@ use rand::{thread_rng, Rng};
|
|||||||
use tempdir::TempDir;
|
use tempdir::TempDir;
|
||||||
|
|
||||||
use std::fs::File as StdFile;
|
use std::fs::File as StdFile;
|
||||||
use std::io::Read;
|
use std::io::{Read, SeekFrom};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn read_write() {
|
fn read_write() {
|
||||||
@ -103,3 +103,39 @@ fn metadata() {
|
|||||||
|
|
||||||
rx.wait().unwrap();
|
rx.wait().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn seek() {
|
||||||
|
let dir = TempDir::new("tokio-fs-tests").unwrap();
|
||||||
|
let file_path = dir.path().join("seek.txt");
|
||||||
|
|
||||||
|
let pool = Builder::new().pool_size(1).build();
|
||||||
|
|
||||||
|
let (tx, rx) = oneshot::channel();
|
||||||
|
|
||||||
|
pool.spawn(
|
||||||
|
OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.read(true)
|
||||||
|
.write(true)
|
||||||
|
.open(file_path)
|
||||||
|
.and_then(|file| io::write_all(file, "Hello, world!"))
|
||||||
|
.and_then(|(file, _)| file.seek(SeekFrom::End(-6)))
|
||||||
|
.and_then(|(file, _)| io::read_exact(file, vec![0; 5]))
|
||||||
|
.and_then(|(file, buf)| {
|
||||||
|
assert_eq!(buf, b"world");
|
||||||
|
file.seek(SeekFrom::Start(0))
|
||||||
|
})
|
||||||
|
.and_then(|(file, _)| io::read_exact(file, vec![0; 5]))
|
||||||
|
.and_then(|(_, buf)| {
|
||||||
|
assert_eq!(buf, b"Hello");
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.then(|r| {
|
||||||
|
let _ = r.unwrap();
|
||||||
|
tx.send(())
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
rx.wait().unwrap();
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user