@@ -31,6 +31,12 @@ fn use_fd_b<Fd: AsFd>(fd: Fd) {
31
31
let _ = fd. as_fd ( ) ;
32
32
}
33
33
34
+ /// Another way to do this is to use an `impl AsFd` parameter.
35
+ #[ cfg( all( feature = "close" , not( windows) ) ) ]
36
+ fn use_fd_c ( fd : impl AsFd ) {
37
+ let _ = fd. as_fd ( ) ;
38
+ }
39
+
34
40
/// The simplest way to accept a consumed I/O resource is to simply use an
35
41
/// `OwnedFd` as an argument. Similar to `use_fd_a`, this doesn't require the
36
42
/// function to have any type parameters, and also works in FFI signatures.
@@ -51,6 +57,12 @@ fn consume_fd_b<Fd: IntoFd>(fd: Fd) {
51
57
let _ = fd. into_fd ( ) ;
52
58
}
53
59
60
+ /// Another way to do this is to use an `impl IntoFd` parameter.
61
+ #[ cfg( all( feature = "close" , not( windows) ) ) ]
62
+ fn consume_fd_c ( fd : impl IntoFd ) {
63
+ let _ = fd. into_fd ( ) ;
64
+ }
65
+
54
66
/// Now let's see how the APIs look for users.
55
67
#[ cfg( all( feature = "close" , not( windows) ) ) ]
56
68
fn main ( ) {
@@ -59,20 +71,30 @@ fn main() {
59
71
// The simple option requires an `.as_fd()` at the callsite.
60
72
use_fd_a ( f. as_fd ( ) ) ;
61
73
62
- // The other option can take a reference to any owning type directly.
74
+ // Another option can take a reference to any owning type directly.
63
75
use_fd_b ( & f) ;
64
76
65
77
// Of course, users can still pass in `BorrowedFd` values if they want to.
66
78
use_fd_b ( f. as_fd ( ) ) ;
67
79
80
+ // The other option is `impl AsFd`.
81
+ use_fd_c ( & f) ;
82
+
83
+ // Users can still pass in `BorrowedFd` values if they want to here too.
84
+ use_fd_c ( f. as_fd ( ) ) ;
85
+
68
86
let a = std:: fs:: File :: open ( "Cargo.toml" ) . unwrap ( ) ;
69
87
let b = std:: fs:: File :: open ( "Cargo.toml" ) . unwrap ( ) ;
88
+ let c = std:: fs:: File :: open ( "Cargo.toml" ) . unwrap ( ) ;
70
89
71
90
// The simple option requires an `.into_fd()` at the callsite.
72
91
consume_fd_a ( a. into_fd ( ) ) ;
73
92
74
- // The other option can take any `IntoFd` type directly.
93
+ // Another option can take any `IntoFd` type directly.
75
94
consume_fd_b ( b) ;
95
+
96
+ // The other option can take any `IntoFd` type directly.
97
+ consume_fd_c ( c) ;
76
98
}
77
99
78
100
#[ cfg( windows) ]
0 commit comments