tokio-fs: add tokio_fs::metadata (#433)

This commit is contained in:
Laurențiu Nicola 2018-06-20 22:12:06 +02:00 committed by Carl Lerche
parent b2f77dcebe
commit 04a4bfd455
2 changed files with 53 additions and 1 deletions

View File

@ -31,11 +31,33 @@ pub use stdin::{stdin, Stdin};
pub use stdout::{stdout, Stdout};
pub use stderr::{stderr, Stderr};
use futures::Poll;
use futures::{Future, Poll};
use futures::Async::*;
use std::fs::{self, Metadata};
use std::io;
use std::io::ErrorKind::{Other, WouldBlock};
use std::path::PathBuf;
/// Queries the file system metadata for a path
pub fn metadata(path: PathBuf) -> MetadataFuture {
MetadataFuture { path }
}
/// Future returned by `metadata`
#[derive(Debug)]
pub struct MetadataFuture {
path: PathBuf,
}
impl Future for MetadataFuture {
type Item = Metadata;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
blocking_io(|| fs::metadata(&self.path))
}
}
fn blocking_io<F, T>(f: F) -> Poll<T, io::Error>
where F: FnOnce() -> io::Result<T>,

View File

@ -73,3 +73,33 @@ fn read_write() {
})
});
}
#[test]
fn metadata() {
let dir = TempDir::new("tokio-fs-tests").unwrap();
let file_path = dir.path().join("metadata.txt");
let pool = Builder::new().pool_size(1).build();
let (tx, rx) = oneshot::channel();
pool.spawn({
let file_path = file_path.clone();
let file_path2 = file_path.clone();
let file_path3 = file_path.clone();
tokio_fs::metadata(file_path)
.then(|r| {
let _ = r.err().unwrap();
Ok(())
})
.and_then(|_| File::create(file_path2))
.and_then(|_| tokio_fs::metadata(file_path3))
.then(|r| {
assert!(r.unwrap().is_file());
tx.send(())
})
});
rx.wait().unwrap();
}