diff --git a/tokio/src/fs/file.rs b/tokio/src/fs/file.rs index 74f91958d0d..2f1046ce18f 100644 --- a/tokio/src/fs/file.rs +++ b/tokio/src/fs/file.rs @@ -3,7 +3,7 @@ //! [`File`]: File use self::State::*; -use crate::fs::asyncify; +use crate::fs::{asyncify, OpenOptions}; use crate::io::blocking::Buf; use crate::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf}; use crate::sync::Mutex; @@ -124,8 +124,6 @@ impl File { /// /// See [`OpenOptions`] for more details. /// - /// [`OpenOptions`]: super::OpenOptions - /// /// # Errors /// /// This function will return an error if called from outside of the Tokio @@ -167,8 +165,6 @@ impl File { /// /// See [`OpenOptions`] for more details. /// - /// [`OpenOptions`]: super::OpenOptions - /// /// # Errors /// /// Results in an error if called from outside of the Tokio runtime or if @@ -199,6 +195,37 @@ impl File { Ok(File::from_std(std_file)) } + /// Returns a new [`OpenOptions`] object. + /// + /// This function returns a new `OpenOptions` object that you can use to + /// open or create a file with specific options if `open()` or `create()` + /// are not appropriate. + /// + /// It is equivalent to `OpenOptions::new()`, but allows you to write more + /// readable code. Instead of + /// `OpenOptions::new().append(true).open("example.log")`, + /// you can write `File::options().append(true).open("example.log")`. This + /// also avoids the need to import `OpenOptions`. + /// + /// See the [`OpenOptions::new`] function for more details. + /// + /// # Examples + /// + /// ```no_run + /// use tokio::fs::File; + /// use tokio::io::AsyncWriteExt; + /// + /// # async fn dox() -> std::io::Result<()> { + /// let mut f = File::options().append(true).open("example.log").await?; + /// f.write_all(b"new line\n").await?; + /// # Ok(()) + /// # } + /// ``` + #[must_use] + pub fn options() -> OpenOptions { + OpenOptions::new() + } + /// Converts a [`std::fs::File`][std] to a [`tokio::fs::File`][file]. /// /// [std]: std::fs::File