diff options
-rw-r--r-- | src/fs.rs | 1 | ||||
-rw-r--r-- | tokio-fs/src/file/mod.rs | 16 | ||||
-rw-r--r-- | tokio-fs/src/file/open.rs | 9 | ||||
-rw-r--r-- | tokio-fs/src/file/open_options.rs | 103 | ||||
-rw-r--r-- | tokio-fs/src/lib.rs | 1 |
5 files changed, 124 insertions, 6 deletions
@@ -10,4 +10,5 @@ pub use tokio_fs::{ file, File, + OpenOptions, }; diff --git a/tokio-fs/src/file/mod.rs b/tokio-fs/src/file/mod.rs index 5c5c429c..f4f16861 100644 --- a/tokio-fs/src/file/mod.rs +++ b/tokio-fs/src/file/mod.rs @@ -4,9 +4,11 @@ mod create; mod open; +mod open_options; pub use self::create::CreateFuture; pub use self::open::OpenFuture; +pub use self::open_options::OpenOptions; use tokio_io::{AsyncRead, AsyncWrite}; @@ -36,16 +38,20 @@ pub struct File { impl File { /// Attempts to open a file in read-only mode. /// + /// See [`OpenOptions`] for more details. + /// + /// [`OpenOptions`]: struct.OpenOptions.html + /// /// # Errors /// /// `OpenFuture` results in an error if called from outside of the Tokio /// runtime or if the underlying [`open`] call results in an error. /// - /// [`open`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open + /// [`open`]: https://doc.rust-lang.org/std/fs/struct.File.html#method.open pub fn open<P>(path: P) -> OpenFuture<P> where P: AsRef<Path> + Send + 'static, { - OpenFuture::new(path) + OpenOptions::new().read(true).open(path) } /// Opens a file in write-only mode. @@ -53,6 +59,12 @@ impl File { /// This function will create a file if it does not exist, and will truncate /// it if it does. /// + /// See [`OpenOptions`] for more details. + /// + /// [`OpenOptions`]: struct.OpenOptions.html + /// + /// # Errors + /// /// `CreateFuture` results in an error if called from outside of the Tokio /// runtime or if the underlying [`create`] call results in an error. /// diff --git a/tokio-fs/src/file/open.rs b/tokio-fs/src/file/open.rs index dc842bd8..197ec237 100644 --- a/tokio-fs/src/file/open.rs +++ b/tokio-fs/src/file/open.rs @@ -2,21 +2,22 @@ use super::File; use futures::{Future, Poll}; -use std::fs::File as StdFile; +use std::fs::OpenOptions as StdOpenOptions; use std::io; use std::path::Path; /// Future returned by `File::open` and resolves to a `File` instance. #[derive(Debug)] pub struct OpenFuture<P> { + options: StdOpenOptions, path: P, } impl<P> OpenFuture<P> where P: AsRef<Path> + Send + 'static, { - pub(crate) fn new(path: P) -> Self { - OpenFuture { path } + pub(crate) fn new(options: StdOpenOptions, path: P) -> Self { + OpenFuture { options, path } } } @@ -28,7 +29,7 @@ where P: AsRef<Path> + Send + 'static, fn poll(&mut self) -> Poll<Self::Item, Self::Error> { let std = try_ready!(::blocking_io(|| { - StdFile::open(&self.path) + self.options.open(&self.path) })); let file = File::from_std(std); diff --git a/tokio-fs/src/file/open_options.rs b/tokio-fs/src/file/open_options.rs new file mode 100644 index 00000000..99cc71c5 --- /dev/null +++ b/tokio-fs/src/file/open_options.rs @@ -0,0 +1,103 @@ +use super::OpenFuture; + +use std::convert::From; +use std::fs::OpenOptions as StdOpenOptions; +use std::path::Path; + +/// Options and flags which can be used to configure how a file is opened. +/// +/// This is a specialized version of [`std::fs::OpenOptions`] for usage from +/// the Tokio runtime. +/// +/// `From<std::fs::OpenOptions>` is implemented for more advanced configuration +/// than the methods provided here. +/// +/// [`std::fs::OpenOptions`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html +#[derive(Clone, Debug)] +pub struct OpenOptions(StdOpenOptions); + +impl OpenOptions { + /// Creates a blank new set of options ready for configuration. + /// + /// All options are initially set to `false`. + /// + /// # Examples + /// + /// ```ignore + /// use tokio::fs::OpenOptions; + /// + /// let mut options = OpenOptions::new(); + /// let future = options.read(true).open("foo.txt"); + /// ``` + pub fn new() -> OpenOptions { + OpenOptions(StdOpenOptions::new()) + } + + /// See the underlying [`read`] call for details. + /// + /// [`read`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.read + pub fn read(&mut self, read: bool) -> &mut OpenOptions { + self.0.read(read); + self + } + + /// See the underlying [`write`] call for details. + /// + /// [`write`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.write + pub fn write(&mut self, write: bool) -> &mut OpenOptions { + self.0.write(write); + self + } + + /// See the underlying [`append`] call for details. + /// + /// [`append`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.append + pub fn append(&mut self, append: bool) -> &mut OpenOptions { + self.0.append(append); + self + } + + /// See the underlying [`truncate`] call for details. + /// + /// [`truncate`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.truncate + pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions { + self.0.truncate(truncate); + self + } + + /// See the underlying [`create`] call for details. + /// + /// [`create`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create + pub fn create(&mut self, create: bool) -> &mut OpenOptions { + self.0.create(create); + self + } + + /// See the underlying [`create_new`] call for details. + /// + /// [`create_new`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create_new + pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions { + self.0.create_new(create_new); + self + } + + /// Opens a file at `path` with the options specified by `self`. + /// + /// # Errors + /// + /// `OpenOptionsFuture` results in an error if called from outside of the + /// Tokio runtime or if the underlying [`open`] call results in an error. + /// + /// [`open`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open + pub fn open<P>(&self, path: P) -> OpenFuture<P> + where P: AsRef<Path> + Send + 'static + { + OpenFuture::new(self.0.clone(), path) + } +} + +impl From<StdOpenOptions> for OpenOptions { + fn from(options: StdOpenOptions) -> OpenOptions { + OpenOptions(options) + } +} diff --git a/tokio-fs/src/lib.rs b/tokio-fs/src/lib.rs index 216e4a28..0b6cae6e 100644 --- a/tokio-fs/src/lib.rs +++ b/tokio-fs/src/lib.rs @@ -23,6 +23,7 @@ mod stdout; mod stderr; pub use file::File; +pub use file::OpenOptions; pub use stdin::{stdin, Stdin}; pub use stdout::{stdout, Stdout}; pub use stderr::{stderr, Stderr}; |