@@ -345,6 +345,33 @@ pub trait OpenOptionsExt {
345345 /// ```
346346 #[ stable( feature = "open_options_ext" , since = "1.10.0" ) ]
347347 fn custom_flags ( & mut self , flags : i32 ) -> & mut Self ;
348+
349+ /// Get the flags of this OpenOptions as libc::c_int.
350+ ///
351+ /// This method allows the reuse of the OpenOptions as flags argument for `libc::open()`.
352+ ///
353+ /// # Examples
354+ ///
355+ /// ```no_run
356+ /// # #![feature(rustc_private)]
357+ /// extern crate libc;
358+ /// use std::ffi::CString;
359+ /// use std::fs::OpenOptions;
360+ /// use std::os::unix::fs::OpenOptionsExt;
361+ ///
362+ /// # fn main() {
363+ /// let mut options = OpenOptions::new();
364+ /// options.write(true).read(true);
365+ /// if cfg!(unix) {
366+ /// options.custom_flags(libc::O_NOFOLLOW);
367+ /// }
368+ /// let file_name = CString::new("foo.txt").unwrap();
369+ /// let file = unsafe{ libc::open(file_name.as_c_str().as_ptr(), options.as_flags().unwrap()) };
370+ ///
371+ /// # }
372+ /// ```
373+ #[ stable( feature = "open_options_ext_as_flags" , since = "1.47.0" ) ]
374+ fn as_flags ( & self ) -> io:: Result < libc:: c_int > ;
348375}
349376
350377#[ stable( feature = "fs_ext" , since = "1.1.0" ) ]
@@ -358,6 +385,9 @@ impl OpenOptionsExt for OpenOptions {
358385 self . as_inner_mut ( ) . custom_flags ( flags) ;
359386 self
360387 }
388+ fn as_flags ( & self ) -> io:: Result < libc:: c_int > {
389+ self . as_inner ( ) . as_flags ( )
390+ }
361391}
362392
363393/// Unix-specific extensions to [`fs::Metadata`].
0 commit comments