Implement AsRawFd/IntoRawFd for RawFd

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);
This commit is contained in:
Jörg Thalheim 2017-03-26 18:54:08 +02:00
parent 7846dbe0c8
commit 2cf686f2cd
No known key found for this signature in database
GPG Key ID: CA4106B8D7CC79FA

View File

@ -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 {
@ -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 {