Move custom_flags to OpenOptionsExt

And mark the new methods as unstable.
This commit is contained in:
Paul Dicker 2016-01-13 21:47:46 +01:00
parent 42f4dd047a
commit 7a1817c9d4
4 changed files with 58 additions and 39 deletions

View File

@ -519,46 +519,13 @@ impl OpenOptions {
///
/// let file = OpenOptions::new().write(true).create_new(true).open("foo.txt");
/// ```
#[stable(feature = "expand_open_options", since = "1.7.0")]
#[unstable(feature = "expand_open_options",
reason = "recently added",
issue = "30014")]
pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
self.0.create_new(create_new); self
}
/// Pass custom open flags to the operating system.
///
/// Windows and the various flavours of Unix support flags that are not
/// cross-platform, but that can be useful in some circumstances. On Unix they will
/// be passed as the variable _flags_ to `open`, on Windows as the
/// _dwFlagsAndAttributes_ parameter.
///
/// The cross-platform options of Rust can do magic: they can set any flag necessary
/// to ensure it works as expected. For example, `.append(true)` on Unix not only
/// sets the flag `O_APPEND`, but also automatically `O_WRONLY` or `O_RDWR`. This
/// special treatment is not available for the custom flags.
///
/// Custom flags can only set flags, not remove flags set by Rusts options.
///
/// For the custom flags on Unix, the bits that define the access mode are masked
/// out with `O_ACCMODE`, to ensure they do not interfere with the access mode set
/// by Rusts options.
///
/// # Examples
///
/// ```rust,ignore
/// extern crate libc;
/// extern crate winapi;
/// use std::fs::OpenOptions;
///
/// let options = OpenOptions::new().write(true);
/// if cfg!(unix) { options.custom_flags(libc::O_NOFOLLOW); }
/// if cfg!(windows) { options.custom_flags(winapi::FILE_FLAG_BACKUP_SEMANTICS); }
/// let file = options.open("foo.txt");
/// ```
#[stable(feature = "expand_open_options", since = "1.7.0")]
pub fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
self.0.custom_flags(flags); self
}
/// Opens a file at `path` with the options specified by `self`.
///
/// # Errors

View File

@ -104,6 +104,29 @@ pub trait OpenOptionsExt {
/// the final permissions.
#[stable(feature = "fs_ext", since = "1.1.0")]
fn mode(&mut self, mode: raw::mode_t) -> &mut Self;
/// Pass custom flags to the `flags` agument of `open`.
///
/// The bits that define the access mode are masked out with `O_ACCMODE`, to ensure
/// they do not interfere with the access mode set by Rusts options.
///
/// Custom flags can only set flags, not remove flags set by Rusts options.
///
/// # Examples
///
/// ```no_run
/// extern crate libc;
/// use std::fs::OpenOptions;
/// use std::os::unix::fs::OpenOptionsExt;
///
/// let options = OpenOptions::new().write(true);
/// if cfg!(unix) { options.custom_flags(libc::O_NOFOLLOW); }
/// let file = options.open("foo.txt");
/// ```
#[unstable(feature = "expand_open_options",
reason = "recently added",
issue = "30014")]
fn custom_flags(&mut self, flags: i32) -> &mut Self;
}
#[stable(feature = "fs_ext", since = "1.1.0")]
@ -111,6 +134,10 @@ impl OpenOptionsExt for OpenOptions {
fn mode(&mut self, mode: raw::mode_t) -> &mut OpenOptions {
self.as_inner_mut().mode(mode); self
}
fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
self.as_inner_mut().custom_flags(flags); self
}
}
// Hm, why are there casts here to the returned type, shouldn't the types always
@ -265,4 +292,3 @@ impl DirBuilderExt for fs::DirBuilder {
self
}
}

View File

@ -58,7 +58,7 @@ pub struct OpenOptions {
create: bool,
create_new: bool,
// system-specific
custom_flags: u32,
custom_flags: i32,
mode: mode_t,
}
@ -259,7 +259,7 @@ impl OpenOptions {
pub fn create(&mut self, create: bool) { self.create = create; }
pub fn create_new(&mut self, create_new: bool) { self.create_new = create_new; }
pub fn custom_flags(&mut self, flags: u32) { self.custom_flags = flags; }
pub fn custom_flags(&mut self, flags: i32) { self.custom_flags = flags; }
pub fn mode(&mut self, mode: raw::mode_t) { self.mode = mode as mode_t; }
fn get_access_mode(&self) -> io::Result<c_int> {

View File

@ -64,6 +64,28 @@ pub trait OpenOptionsExt {
/// ```
fn share_mode(&mut self, val: u32) -> &mut Self;
/// Sets extra flags for the `dwFileFlags` argument to the call to `CreateFile2`
/// (or combines it with `attributes` and `security_qos_flags` to set the
/// `dwFlagsAndAttributes` for `CreateFile`).
///
/// Custom flags can only set flags, not remove flags set by Rusts options.
///
/// # Examples
///
/// ```rust,ignore
/// extern crate winapi;
/// use std::fs::OpenOptions;
/// use std::os::windows::fs::OpenOptionsExt;
///
/// let options = OpenOptions::new().create(true).write(true);
/// if cfg!(windows) { options.custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE); }
/// let file = options.open("foo.txt");
/// ```
#[unstable(feature = "expand_open_options",
reason = "recently added",
issue = "30014")]
fn custom_flags(&mut self, flags: u32) -> &mut Self;
/// Sets the `dwFileAttributes` argument to the call to `CreateFile2` to
/// the specified value (or combines it with `custom_flags` and
/// `security_qos_flags` to set the `dwFlagsAndAttributes` for `CreateFile`).
@ -114,6 +136,10 @@ impl OpenOptionsExt for OpenOptions {
self.as_inner_mut().share_mode(share); self
}
fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
self.as_inner_mut().custom_flags(flags); self
}
fn attributes(&mut self, attributes: u32) -> &mut OpenOptions {
self.as_inner_mut().attributes(attributes); self
}