Skip to content

Commit 7a1817c

Browse files
committed
Move custom_flags to OpenOptionsExt
And mark the new methods as unstable.
1 parent 42f4dd0 commit 7a1817c

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

src/libstd/fs.rs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -519,46 +519,13 @@ impl OpenOptions {
519519
///
520520
/// let file = OpenOptions::new().write(true).create_new(true).open("foo.txt");
521521
/// ```
522-
#[stable(feature = "expand_open_options", since = "1.7.0")]
522+
#[unstable(feature = "expand_open_options",
523+
reason = "recently added",
524+
issue = "30014")]
523525
pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
524526
self.0.create_new(create_new); self
525527
}
526528

527-
/// Pass custom open flags to the operating system.
528-
///
529-
/// Windows and the various flavours of Unix support flags that are not
530-
/// cross-platform, but that can be useful in some circumstances. On Unix they will
531-
/// be passed as the variable _flags_ to `open`, on Windows as the
532-
/// _dwFlagsAndAttributes_ parameter.
533-
///
534-
/// The cross-platform options of Rust can do magic: they can set any flag necessary
535-
/// to ensure it works as expected. For example, `.append(true)` on Unix not only
536-
/// sets the flag `O_APPEND`, but also automatically `O_WRONLY` or `O_RDWR`. This
537-
/// special treatment is not available for the custom flags.
538-
///
539-
/// Custom flags can only set flags, not remove flags set by Rusts options.
540-
///
541-
/// For the custom flags on Unix, the bits that define the access mode are masked
542-
/// out with `O_ACCMODE`, to ensure they do not interfere with the access mode set
543-
/// by Rusts options.
544-
///
545-
/// # Examples
546-
///
547-
/// ```rust,ignore
548-
/// extern crate libc;
549-
/// extern crate winapi;
550-
/// use std::fs::OpenOptions;
551-
///
552-
/// let options = OpenOptions::new().write(true);
553-
/// if cfg!(unix) { options.custom_flags(libc::O_NOFOLLOW); }
554-
/// if cfg!(windows) { options.custom_flags(winapi::FILE_FLAG_BACKUP_SEMANTICS); }
555-
/// let file = options.open("foo.txt");
556-
/// ```
557-
#[stable(feature = "expand_open_options", since = "1.7.0")]
558-
pub fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
559-
self.0.custom_flags(flags); self
560-
}
561-
562529
/// Opens a file at `path` with the options specified by `self`.
563530
///
564531
/// # Errors

src/libstd/sys/unix/ext/fs.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,40 @@ pub trait OpenOptionsExt {
104104
/// the final permissions.
105105
#[stable(feature = "fs_ext", since = "1.1.0")]
106106
fn mode(&mut self, mode: raw::mode_t) -> &mut Self;
107+
108+
/// Pass custom flags to the `flags` agument of `open`.
109+
///
110+
/// The bits that define the access mode are masked out with `O_ACCMODE`, to ensure
111+
/// they do not interfere with the access mode set by Rusts options.
112+
///
113+
/// Custom flags can only set flags, not remove flags set by Rusts options.
114+
///
115+
/// # Examples
116+
///
117+
/// ```no_run
118+
/// extern crate libc;
119+
/// use std::fs::OpenOptions;
120+
/// use std::os::unix::fs::OpenOptionsExt;
121+
///
122+
/// let options = OpenOptions::new().write(true);
123+
/// if cfg!(unix) { options.custom_flags(libc::O_NOFOLLOW); }
124+
/// let file = options.open("foo.txt");
125+
/// ```
126+
#[unstable(feature = "expand_open_options",
127+
reason = "recently added",
128+
issue = "30014")]
129+
fn custom_flags(&mut self, flags: i32) -> &mut Self;
107130
}
108131

109132
#[stable(feature = "fs_ext", since = "1.1.0")]
110133
impl OpenOptionsExt for OpenOptions {
111134
fn mode(&mut self, mode: raw::mode_t) -> &mut OpenOptions {
112135
self.as_inner_mut().mode(mode); self
113136
}
137+
138+
fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
139+
self.as_inner_mut().custom_flags(flags); self
140+
}
114141
}
115142

116143
// Hm, why are there casts here to the returned type, shouldn't the types always
@@ -265,4 +292,3 @@ impl DirBuilderExt for fs::DirBuilder {
265292
self
266293
}
267294
}
268-

src/libstd/sys/unix/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct OpenOptions {
5858
create: bool,
5959
create_new: bool,
6060
// system-specific
61-
custom_flags: u32,
61+
custom_flags: i32,
6262
mode: mode_t,
6363
}
6464

@@ -259,7 +259,7 @@ impl OpenOptions {
259259
pub fn create(&mut self, create: bool) { self.create = create; }
260260
pub fn create_new(&mut self, create_new: bool) { self.create_new = create_new; }
261261

262-
pub fn custom_flags(&mut self, flags: u32) { self.custom_flags = flags; }
262+
pub fn custom_flags(&mut self, flags: i32) { self.custom_flags = flags; }
263263
pub fn mode(&mut self, mode: raw::mode_t) { self.mode = mode as mode_t; }
264264

265265
fn get_access_mode(&self) -> io::Result<c_int> {

src/libstd/sys/windows/ext/fs.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ pub trait OpenOptionsExt {
6464
/// ```
6565
fn share_mode(&mut self, val: u32) -> &mut Self;
6666

67+
/// Sets extra flags for the `dwFileFlags` argument to the call to `CreateFile2`
68+
/// (or combines it with `attributes` and `security_qos_flags` to set the
69+
/// `dwFlagsAndAttributes` for `CreateFile`).
70+
///
71+
/// Custom flags can only set flags, not remove flags set by Rusts options.
72+
///
73+
/// # Examples
74+
///
75+
/// ```rust,ignore
76+
/// extern crate winapi;
77+
/// use std::fs::OpenOptions;
78+
/// use std::os::windows::fs::OpenOptionsExt;
79+
///
80+
/// let options = OpenOptions::new().create(true).write(true);
81+
/// if cfg!(windows) { options.custom_flags(winapi::FILE_FLAG_DELETE_ON_CLOSE); }
82+
/// let file = options.open("foo.txt");
83+
/// ```
84+
#[unstable(feature = "expand_open_options",
85+
reason = "recently added",
86+
issue = "30014")]
87+
fn custom_flags(&mut self, flags: u32) -> &mut Self;
88+
6789
/// Sets the `dwFileAttributes` argument to the call to `CreateFile2` to
6890
/// the specified value (or combines it with `custom_flags` and
6991
/// `security_qos_flags` to set the `dwFlagsAndAttributes` for `CreateFile`).
@@ -114,6 +136,10 @@ impl OpenOptionsExt for OpenOptions {
114136
self.as_inner_mut().share_mode(share); self
115137
}
116138

139+
fn custom_flags(&mut self, flags: u32) -> &mut OpenOptions {
140+
self.as_inner_mut().custom_flags(flags); self
141+
}
142+
117143
fn attributes(&mut self, attributes: u32) -> &mut OpenOptions {
118144
self.as_inner_mut().attributes(attributes); self
119145
}

0 commit comments

Comments
 (0)