@@ -25,6 +25,20 @@ pub trait AsRawFd {
25
25
/// This method does **not** pass ownership of the raw file descriptor
26
26
/// to the caller. The descriptor is only guaranteed to be valid while
27
27
/// the original object has not yet been destroyed.
28
+ ///
29
+ /// # Example
30
+ ///
31
+ /// ```no_run
32
+ /// use std::fs::File;
33
+ /// use std::os::unix::io::{AsRawFd, RawFd};
34
+ ///
35
+ /// fn main() -> std::io::Result<()> {
36
+ /// let mut f = File::open("foo.txt")?;
37
+ /// // Note that `raw_fd` is only valid as long as `f` exists.
38
+ /// let raw_fd: RawFd = f.as_raw_fd();
39
+ /// Ok(())
40
+ /// }
41
+ /// ```
28
42
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
29
43
fn as_raw_fd ( & self ) -> RawFd ;
30
44
}
@@ -45,6 +59,22 @@ pub trait FromRawFd {
45
59
/// descriptor they are wrapping. Usage of this function could
46
60
/// accidentally allow violating this contract which can cause memory
47
61
/// unsafety in code that relies on it being true.
62
+ ///
63
+ /// # Example
64
+ ///
65
+ /// ```no_run
66
+ /// use std::fs::File;
67
+ /// use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
68
+ ///
69
+ /// fn main() -> std::io::Result<()> {
70
+ /// let f = File::open("foo.txt")?;
71
+ /// let raw_fd: RawFd = f.into_raw_fd();
72
+ /// // SAFETY: no other functions should call `from_raw_fd`, so there
73
+ /// // is only one owner for the file descriptor.
74
+ /// let f = unsafe { File::from_raw_fd(raw_fd) };
75
+ /// Ok(())
76
+ /// }
77
+ /// ```
48
78
#[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
49
79
unsafe fn from_raw_fd ( fd : RawFd ) -> Self ;
50
80
}
@@ -58,6 +88,19 @@ pub trait IntoRawFd {
58
88
/// This function **transfers ownership** of the underlying file descriptor
59
89
/// to the caller. Callers are then the unique owners of the file descriptor
60
90
/// and must close the descriptor once it's no longer needed.
91
+ ///
92
+ /// # Example
93
+ ///
94
+ /// ```no_run
95
+ /// use std::fs::File;
96
+ /// use std::os::unix::io::{IntoRawFd, RawFd};
97
+ ///
98
+ /// fn main() -> std::io::Result<()> {
99
+ /// let f = File::open("foo.txt")?;
100
+ /// let raw_fd: RawFd = f.into_raw_fd();
101
+ /// Ok(())
102
+ /// }
103
+ /// ```
61
104
#[ stable( feature = "into_raw_os" , since = "1.4.0" ) ]
62
105
fn into_raw_fd ( self ) -> RawFd ;
63
106
}
0 commit comments