tokio_fs/file/open_options.rs
1use super::OpenFuture;
2
3use std::convert::From;
4use std::fs::OpenOptions as StdOpenOptions;
5use std::path::Path;
6
7/// Options and flags which can be used to configure how a file is opened.
8///
9/// This is a specialized version of [`std::fs::OpenOptions`] for usage from
10/// the Tokio runtime.
11///
12/// `From<std::fs::OpenOptions>` is implemented for more advanced configuration
13/// than the methods provided here.
14///
15/// [`std::fs::OpenOptions`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html
16#[derive(Clone, Debug)]
17pub struct OpenOptions(StdOpenOptions);
18
19impl OpenOptions {
20 /// Creates a blank new set of options ready for configuration.
21 ///
22 /// All options are initially set to `false`.
23 ///
24 /// # Examples
25 ///
26 /// ```ignore
27 /// use tokio::fs::OpenOptions;
28 ///
29 /// let mut options = OpenOptions::new();
30 /// let future = options.read(true).open("foo.txt");
31 /// ```
32 pub fn new() -> OpenOptions {
33 OpenOptions(StdOpenOptions::new())
34 }
35
36 /// See the underlying [`read`] call for details.
37 ///
38 /// [`read`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.read
39 pub fn read(&mut self, read: bool) -> &mut OpenOptions {
40 self.0.read(read);
41 self
42 }
43
44 /// See the underlying [`write`] call for details.
45 ///
46 /// [`write`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.write
47 pub fn write(&mut self, write: bool) -> &mut OpenOptions {
48 self.0.write(write);
49 self
50 }
51
52 /// See the underlying [`append`] call for details.
53 ///
54 /// [`append`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.append
55 pub fn append(&mut self, append: bool) -> &mut OpenOptions {
56 self.0.append(append);
57 self
58 }
59
60 /// See the underlying [`truncate`] call for details.
61 ///
62 /// [`truncate`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.truncate
63 pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
64 self.0.truncate(truncate);
65 self
66 }
67
68 /// See the underlying [`create`] call for details.
69 ///
70 /// [`create`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create
71 pub fn create(&mut self, create: bool) -> &mut OpenOptions {
72 self.0.create(create);
73 self
74 }
75
76 /// See the underlying [`create_new`] call for details.
77 ///
78 /// [`create_new`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create_new
79 pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
80 self.0.create_new(create_new);
81 self
82 }
83
84 /// Opens a file at `path` with the options specified by `self`.
85 ///
86 /// # Errors
87 ///
88 /// `OpenOptionsFuture` results in an error if called from outside of the
89 /// Tokio runtime or if the underlying [`open`] call results in an error.
90 ///
91 /// [`open`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open
92 pub fn open<P>(&self, path: P) -> OpenFuture<P>
93 where
94 P: AsRef<Path> + Send + 'static,
95 {
96 OpenFuture::new(self.0.clone(), path)
97 }
98}
99
100impl From<StdOpenOptions> for OpenOptions {
101 fn from(options: StdOpenOptions) -> OpenOptions {
102 OpenOptions(options)
103 }
104}