Skip to content

Commit

Permalink
Implement AsRawFd/IntoRawFd for RawFd
Browse files Browse the repository at this point in the history
This is useful to build os abstraction like the nix crate does.
It allows to define functions, which accepts generic arguments
of data structures convertible to RawFd, including RawFd itself.
For example:

  fn write<FD: AsRawFd>(fd: FD, buf: &[u8]) -> Result<usize>

instead of:

  fn write(fd: RawFd, buf: &[u8]) -> Result<usize>
  write(foo.as_raw_fd(), buf);
  • Loading branch information
Mic92 committed Mar 26, 2017
1 parent 7846dbe commit 2cf686f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/libstd/sys/unix/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ pub trait IntoRawFd {
fn into_raw_fd(self) -> RawFd;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for RawFd {
fn as_raw_fd(&self) -> RawFd {
*self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for fs::File {
fn as_raw_fd(&self) -> RawFd {
Expand All @@ -84,6 +91,14 @@ impl FromRawFd for fs::File {
fs::File::from_inner(sys::fs::File::from_inner(fd))
}
}

#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for RawFd {
fn into_raw_fd(self) -> RawFd {
self
}
}

#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for fs::File {
fn into_raw_fd(self) -> RawFd {
Expand Down

0 comments on commit 2cf686f

Please sign in to comment.