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