Skip to content

Commit ccc020a

Browse files
Rollup merge of #77182 - GuillaumeGomez:missing-examples-fd-traits, r=pickfire
Add missing examples for Fd traits Not sure what happened here... This is a reopening of #77142 r? @Dylan-DPC
2 parents 389f7cf + d6b838b commit ccc020a

File tree

1 file changed

+40
-0
lines changed
  • library/std/src/sys/unix/ext

1 file changed

+40
-0
lines changed

Diff for: library/std/src/sys/unix/ext/io.rs

+40
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ 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::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+
/// ```
2841
#[stable(feature = "rust1", since = "1.0.0")]
2942
fn as_raw_fd(&self) -> RawFd;
3043
}
@@ -45,6 +58,21 @@ pub trait FromRawFd {
4558
/// descriptor they are wrapping. Usage of this function could
4659
/// accidentally allow violating this contract which can cause memory
4760
/// 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+
/// ```
4876
#[stable(feature = "from_raw_os", since = "1.1.0")]
4977
unsafe fn from_raw_fd(fd: RawFd) -> Self;
5078
}
@@ -58,6 +86,18 @@ pub trait IntoRawFd {
5886
/// This function **transfers ownership** of the underlying file descriptor
5987
/// to the caller. Callers are then the unique owners of the file descriptor
6088
/// 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+
/// ```
61101
#[stable(feature = "into_raw_os", since = "1.4.0")]
62102
fn into_raw_fd(self) -> RawFd;
63103
}

0 commit comments

Comments
 (0)