@@ -25,6 +25,20 @@ pub trait AsRawFd {
2525 /// This method does **not** pass ownership of the raw file descriptor
2626 /// to the caller. The descriptor is only guaranteed to be valid while
2727 /// 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+ /// ```
2842 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2943 fn as_raw_fd ( & self ) -> RawFd ;
3044}
@@ -45,6 +59,22 @@ pub trait FromRawFd {
4559 /// descriptor they are wrapping. Usage of this function could
4660 /// accidentally allow violating this contract which can cause memory
4761 /// 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+ /// ```
4878 #[ stable( feature = "from_raw_os" , since = "1.1.0" ) ]
4979 unsafe fn from_raw_fd ( fd : RawFd ) -> Self ;
5080}
@@ -58,6 +88,19 @@ pub trait IntoRawFd {
5888 /// This function **transfers ownership** of the underlying file descriptor
5989 /// to the caller. Callers are then the unique owners of the file descriptor
6090 /// 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+ /// ```
61104 #[ stable( feature = "into_raw_os" , since = "1.4.0" ) ]
62105 fn into_raw_fd ( self ) -> RawFd ;
63106}
0 commit comments