@@ -2751,6 +2751,178 @@ impl DirEntry {
27512751 pub fn file_name ( & self ) -> OsString {
27522752 self . 0 . file_name ( )
27532753 }
2754+
2755+ /// Attempts to open the file represented by `self` in read-only mode.
2756+ ///
2757+ /// # Errors
2758+ ///
2759+ /// This function will error whenever [`File::open`] would error, including when `self`
2760+ /// represents a directory.
2761+ ///
2762+ /// # Examples
2763+ ///
2764+ /// ```no_run
2765+ /// #![feature(dirfd)]
2766+ /// use std::{fs, io::read_to_string};
2767+ ///
2768+ /// if let Ok(entries) = fs::read_dir(".") {
2769+ /// for entry in entries {
2770+ /// if let Ok(entry) = entry {
2771+ /// // Here, `entry` is a `DirEntry`.
2772+ /// if let Ok(file) = entry.open_file() && let Ok(text) = read_to_string(file) {
2773+ /// println!("{}", text);
2774+ /// }
2775+ /// }
2776+ /// }
2777+ /// }
2778+ /// ```
2779+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
2780+ pub fn open_file ( & self ) -> io:: Result < File > {
2781+ self . 0 . open_file ( ) . map ( |inner| File { inner } )
2782+ }
2783+
2784+ /// Attempts to open the file represented by `self` with the options specified by `opts`.
2785+ ///
2786+ /// # Errors
2787+ ///
2788+ /// This function will error whenever [`OpenOptions::open`] would error, including when `self`
2789+ /// represents a directory.
2790+ ///
2791+ /// # Examples
2792+ ///
2793+ /// ```no_run
2794+ /// #![feature(dirfd)]
2795+ /// use std::{fs, io::Write};
2796+ ///
2797+ /// if let Ok(entries) = fs::read_dir(".") {
2798+ /// for entry in entries {
2799+ /// if let Ok(entry) = entry {
2800+ /// // Here, `entry` is a `DirEntry`.
2801+ /// if let Ok(mut file) = entry.open_file_with(fs::OpenOptions::new().write(true)) {
2802+ /// let _ = file.write(b"foo");
2803+ /// }
2804+ /// }
2805+ /// }
2806+ /// }
2807+ /// ```
2808+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
2809+ pub fn open_file_with ( & self , opts : & OpenOptions ) -> io:: Result < File > {
2810+ self . 0 . open_file_with ( & opts. 0 ) . map ( |inner| File { inner } )
2811+ }
2812+
2813+ /// Attempts to open the directory represented by `self` in read-only mode.
2814+ ///
2815+ /// # Errors
2816+ ///
2817+ /// This function will error whenever [`Dir::new`] would error, including when `self`
2818+ /// represents a file.
2819+ ///
2820+ /// # Examples
2821+ ///
2822+ /// ```no_run
2823+ /// #![feature(dirfd)]
2824+ /// use std::{fs, io::read_to_string};
2825+ ///
2826+ /// if let Ok(entries) = fs::read_dir(".") {
2827+ /// for entry in entries {
2828+ /// if let Ok(entry) = entry {
2829+ /// // Here, `entry` is a `DirEntry`.
2830+ /// if let Ok(dir) = entry.open_dir() && let Ok(file) = dir.open("foo") && let Ok(text) = read_to_string(file) {
2831+ /// println!("{}", text);
2832+ /// }
2833+ /// }
2834+ /// }
2835+ /// }
2836+ /// ```
2837+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
2838+ pub fn open_dir ( & self ) -> io:: Result < Dir > {
2839+ self . 0 . open_dir ( ) . map ( |inner| Dir { inner } )
2840+ }
2841+
2842+ /// Attempts to open the directory represented by `self` with the options specified by `opts`.
2843+ ///
2844+ /// # Errors
2845+ ///
2846+ /// This function will error whenever [`Dir::new_with`] would error, including when `self`
2847+ /// represents a file.
2848+ ///
2849+ /// # Examples
2850+ ///
2851+ /// ```no_run
2852+ /// #![feature(dirfd)]
2853+ /// use std::fs;
2854+ ///
2855+ /// if let Ok(entries) = fs::read_dir(".") {
2856+ /// for entry in entries {
2857+ /// if let Ok(entry) = entry {
2858+ /// // Here, `entry` is a `DirEntry`.
2859+ /// if let Ok(dir) = entry.open_dir_with(fs::OpenOptions::new().write(true)) {
2860+ /// let _ = dir.remove_file("foo");
2861+ /// }
2862+ /// }
2863+ /// }
2864+ /// }
2865+ /// ```
2866+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
2867+ pub fn open_dir_with ( & self , opts : & OpenOptions ) -> io:: Result < Dir > {
2868+ self . 0 . open_dir_with ( & opts. 0 ) . map ( |inner| Dir { inner } )
2869+ }
2870+
2871+ /// Attempts to remove the file represented by `self`.
2872+ ///
2873+ /// # Errors
2874+ ///
2875+ /// This function will return an error whenever [`remove_file`] would error.
2876+ ///
2877+ /// # Examples
2878+ ///
2879+ /// ```no_run
2880+ /// #![feature(dirfd)]
2881+ /// use std::fs;
2882+ ///
2883+ /// if let Ok(entries) = fs::read_dir(".") {
2884+ /// for entry in entries {
2885+ /// if let Ok(entry) = entry {
2886+ /// // Here, `entry` is a `DirEntry`.
2887+ /// if let Ok(ty) = entry.file_type() && ty.is_file() {
2888+ /// let _ = entry.remove_file();
2889+ /// }
2890+ /// }
2891+ /// }
2892+ /// }
2893+ /// ```
2894+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
2895+ pub fn remove_file ( & self ) -> io:: Result < ( ) > {
2896+ self . 0 . remove_file ( )
2897+ }
2898+
2899+ /// Attempts to remove the directory represented by `self`.
2900+ ///
2901+ /// # Errors
2902+ ///
2903+ /// This function will return an error whenever [`remove_dir`] would error.
2904+ ///
2905+ /// # Examples
2906+ ///
2907+ /// ```no_run
2908+ /// #![feature(dirfd)]
2909+ /// use std::fs;
2910+ ///
2911+ /// if let Ok(entries) = fs::read_dir(".") {
2912+ /// for entry in entries {
2913+ /// if let Ok(entry) = entry {
2914+ /// // Here, `entry` is a `DirEntry`.
2915+ /// if let Ok(ty) = entry.file_type() && ty.is_dir() {
2916+ /// let _ = entry.remove_dir();
2917+ /// }
2918+ /// }
2919+ /// }
2920+ /// }
2921+ /// ```
2922+ #[ unstable( feature = "dirfd" , issue = "120426" ) ]
2923+ pub fn remove_dir ( & self ) -> io:: Result < ( ) > {
2924+ self . 0 . remove_dir ( )
2925+ }
27542926}
27552927
27562928#[ stable( feature = "dir_entry_debug" , since = "1.13.0" ) ]
0 commit comments