@@ -25,6 +25,19 @@ 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::io;
34
+ /// use std::os::unix::io::{AsRawFd, RawFd};
35
+ ///
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::<(), io::Error>(())
40
+ /// ```
28
41
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
29
42
fn as_raw_fd ( & self ) -> RawFd ;
30
43
}
@@ -45,6 +58,21 @@ pub trait FromRawFd {
45
58
/// descriptor they are wrapping. Usage of this function could
46
59
/// accidentally allow violating this contract which can cause memory
47
60
/// unsafety in code that relies on it being true.
61
+ ///
62
+ /// # Example
63
+ ///
64
+ /// ```no_run
65
+ /// use std::fs::File;
66
+ /// # use std::io;
67
+ /// use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
68
+ ///
69
+ /// let f = File::open("foo.txt")?;
70
+ /// let raw_fd: RawFd = f.into_raw_fd();
71
+ /// // SAFETY: no other functions should call `from_raw_fd`, so there
72
+ /// // is only one owner for the file descriptor.
73
+ /// let f = unsafe { File::from_raw_fd(raw_fd) };
74
+ /// # Ok::<(), io::Error>(())
75
+ /// ```
48
76
#[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
49
77
unsafe fn from_raw_fd ( fd : RawFd ) -> Self ;
50
78
}
@@ -58,6 +86,18 @@ pub trait IntoRawFd {
58
86
/// This function **transfers ownership** of the underlying file descriptor
59
87
/// to the caller. Callers are then the unique owners of the file descriptor
60
88
/// and must close the descriptor once it's no longer needed.
89
+ ///
90
+ /// # Example
91
+ ///
92
+ /// ```no_run
93
+ /// use std::fs::File;
94
+ /// # use std::io;
95
+ /// use std::os::unix::io::{IntoRawFd, RawFd};
96
+ ///
97
+ /// let f = File::open("foo.txt")?;
98
+ /// let raw_fd: RawFd = f.into_raw_fd();
99
+ /// # Ok::<(), io::Error>(())
100
+ /// ```
61
101
#[ stable( feature = "into_raw_os" , since = "1.4.0" ) ]
62
102
fn into_raw_fd ( self ) -> RawFd ;
63
103
}
0 commit comments