mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00
fs: add File::options()
(#5869)
This commit is contained in:
parent
33d6d4f63c
commit
05feb2b0bb
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user