mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00
tokio-fs: add tokio_fs::metadata (#433)
This commit is contained in:
parent
b2f77dcebe
commit
04a4bfd455
@ -31,11 +31,33 @@ pub use stdin::{stdin, Stdin};
|
|||||||
pub use stdout::{stdout, Stdout};
|
pub use stdout::{stdout, Stdout};
|
||||||
pub use stderr::{stderr, Stderr};
|
pub use stderr::{stderr, Stderr};
|
||||||
|
|
||||||
use futures::Poll;
|
use futures::{Future, Poll};
|
||||||
use futures::Async::*;
|
use futures::Async::*;
|
||||||
|
|
||||||
|
use std::fs::{self, Metadata};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::ErrorKind::{Other, WouldBlock};
|
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>
|
fn blocking_io<F, T>(f: F) -> Poll<T, io::Error>
|
||||||
where F: FnOnce() -> io::Result<T>,
|
where F: FnOnce() -> io::Result<T>,
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user