about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJörg Thalheim <joerg@thalheim.io>2017-03-26 18:54:08 +0200
committerJörg Thalheim <joerg@thalheim.io>2017-03-26 19:14:03 +0200
commit2cf686f2cdd6446a3cd47df0305ead40fabe85df (patch)
treefa62e1fd3d0c6319d3439eb98e40376f8e8e28c8
parent7846dbe0c8de17f59cdfc3d2b914d58faad7eade (diff)
downloadrust-2cf686f2cdd6446a3cd47df0305ead40fabe85df.tar.gz
rust-2cf686f2cdd6446a3cd47df0305ead40fabe85df.zip
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);
-rw-r--r--src/libstd/sys/unix/ext/io.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/libstd/sys/unix/ext/io.rs b/src/libstd/sys/unix/ext/io.rs
index 296235e173d..75aa72e3cff 100644
--- a/src/libstd/sys/unix/ext/io.rs
+++ b/src/libstd/sys/unix/ext/io.rs
@@ -73,6 +73,13 @@ pub trait IntoRawFd {
 }
 
 #[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 {
         self.as_inner().fd().raw()
@@ -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 {