Files
tokio/tokio-fs/src/file/open.rs
T

39 lines
821 B
Rust
Raw Normal View History

2018-05-02 11:19:58 -07:00
use super::File;
use futures::{Future, Poll};
2018-06-12 10:47:24 -07:00
use std::fs::OpenOptions as StdOpenOptions;
2018-05-02 11:19:58 -07:00
use std::io;
use std::path::Path;
/// Future returned by `File::open` and resolves to a `File` instance.
#[derive(Debug)]
pub struct OpenFuture<P> {
2018-06-12 10:47:24 -07:00
options: StdOpenOptions,
2018-05-02 11:19:58 -07:00
path: P,
}
impl<P> OpenFuture<P>
where P: AsRef<Path> + Send + 'static,
{
2018-06-12 10:47:24 -07:00
pub(crate) fn new(options: StdOpenOptions, path: P) -> Self {
OpenFuture { options, path }
2018-05-02 11:19:58 -07:00
}
}
impl<P> Future for OpenFuture<P>
where P: AsRef<Path> + Send + 'static,
{
type Item = File;
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let std = try_ready!(::blocking_io(|| {
2018-06-12 10:47:24 -07:00
self.options.open(&self.path)
2018-05-02 11:19:58 -07:00
}));
let file = File::from_std(std);
Ok(file.into())
}
}