Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing examples for Fd traits #77182

Merged
merged 2 commits into from
Oct 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions library/std/src/sys/unix/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ pub trait AsRawFd {
/// This method does **not** pass ownership of the raw file descriptor
/// to the caller. The descriptor is only guaranteed to be valid while
/// the original object has not yet been destroyed.
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use std::os::unix::io::{AsRawFd, RawFd};
///
/// fn main() -> std::io::Result<()> {
/// let mut f = File::open("foo.txt")?;
/// // Note that `raw_fd` is only valid as long as `f` exists.
/// let raw_fd: RawFd = f.as_raw_fd();
/// Ok(())
/// }
/// ```
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
#[stable(feature = "rust1", since = "1.0.0")]
fn as_raw_fd(&self) -> RawFd;
}
Expand All @@ -45,6 +59,22 @@ pub trait FromRawFd {
/// descriptor they are wrapping. Usage of this function could
/// accidentally allow violating this contract which can cause memory
/// unsafety in code that relies on it being true.
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
///
/// fn main() -> std::io::Result<()> {
/// let f = File::open("foo.txt")?;
/// let raw_fd: RawFd = f.into_raw_fd();
/// // SAFETY: no other functions should call `from_raw_fd`, so there
/// // is only one owner for the file descriptor.
/// let f = unsafe { File::from_raw_fd(raw_fd) };
/// Ok(())
/// }
/// ```
#[stable(feature = "from_raw_os", since = "1.1.0")]
unsafe fn from_raw_fd(fd: RawFd) -> Self;
}
Expand All @@ -58,6 +88,19 @@ pub trait IntoRawFd {
/// This function **transfers ownership** of the underlying file descriptor
/// to the caller. Callers are then the unique owners of the file descriptor
/// and must close the descriptor once it's no longer needed.
///
/// # Example
///
/// ```no_run
/// use std::fs::File;
/// use std::os::unix::io::{IntoRawFd, RawFd};
///
/// fn main() -> std::io::Result<()> {
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
/// let f = File::open("foo.txt")?;
/// let raw_fd: RawFd = f.into_raw_fd();
/// Ok(())
/// }
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
/// ```
#[stable(feature = "into_raw_os", since = "1.4.0")]
fn into_raw_fd(self) -> RawFd;
}
Expand Down