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