Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add File::with_options #65429

Merged
merged 2 commits into from
Nov 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/libstd/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::time::SystemTime;
///
/// # Examples
///
/// Creates a new file and write bytes to it:
/// Creates a new file and write bytes to it (you can also use [`write`]):
///
/// ```no_run
/// use std::fs::File;
Expand All @@ -42,7 +42,7 @@ use crate::time::SystemTime;
/// }
/// ```
///
/// Read the contents of a file into a [`String`]:
/// Read the contents of a file into a [`String`] (you can also use [`read`]):
///
/// ```no_run
/// use std::fs::File;
Expand Down Expand Up @@ -89,6 +89,8 @@ use crate::time::SystemTime;
/// [`Write`]: ../io/trait.Write.html
/// [`BufReader<R>`]: ../io/struct.BufReader.html
/// [`sync_all`]: struct.File.html#method.sync_all
/// [`read`]: fn.read.html
/// [`write`]: fn.write.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct File {
inner: fs_imp::File,
Expand Down Expand Up @@ -397,6 +399,37 @@ impl File {
OpenOptions::new().write(true).create(true).truncate(true).open(path.as_ref())
}

/// Returns a new OpenOptions object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add link for this

Suggested change
/// Returns a new OpenOptions object.
/// 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.
Comment on lines +404 to +406
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove theses lines.

///
/// It is equivalent to `OpenOptions::new()` but allows you to write more
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// It is equivalent to `OpenOptions::new()` but allows you to write more
/// It is equivalent to [`OpenOptions::new()`] but allows you to write more

/// readable code. Instead of `OpenOptions::new().read(true).open("foo.txt")`
/// you can write `File::with_options().read(true).open("foo.txt"). This
/// also avoids the need to import `OpenOptions`.
///
/// See the [`OpenOptions::new`] function for more details.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// See the [`OpenOptions::new`] function for more details.

///
/// [`OpenOptions::new`]: struct.OpenOptions.html#method.new
///
/// # Examples
///
/// ```no_run
/// #![feature(with_options)]
/// use std::fs::File;
///
/// fn main() -> std::io::Result<()> {
/// let mut f = File::with_options().read(true).open("foo.txt")?;
/// Ok(())
/// }
/// ```
#[unstable(feature = "with_options", issue = "65439")]
pub fn with_options() -> OpenOptions {
OpenOptions::new()
}

/// Attempts to sync all OS-internal metadata to disk.
///
/// This function will attempt to ensure that all in-memory data reaches the
Expand Down