Skip to content

Commit 2cf686f

Browse files
committed
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);
1 parent 7846dbe commit 2cf686f

File tree

1 file changed

+15
-0
lines changed
  • src/libstd/sys/unix/ext

1 file changed

+15
-0
lines changed

src/libstd/sys/unix/ext/io.rs

+15
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ pub trait IntoRawFd {
7272
fn into_raw_fd(self) -> RawFd;
7373
}
7474

75+
#[stable(feature = "rust1", since = "1.0.0")]
76+
impl AsRawFd for RawFd {
77+
fn as_raw_fd(&self) -> RawFd {
78+
*self
79+
}
80+
}
81+
7582
#[stable(feature = "rust1", since = "1.0.0")]
7683
impl AsRawFd for fs::File {
7784
fn as_raw_fd(&self) -> RawFd {
@@ -84,6 +91,14 @@ impl FromRawFd for fs::File {
8491
fs::File::from_inner(sys::fs::File::from_inner(fd))
8592
}
8693
}
94+
95+
#[stable(feature = "into_raw_os", since = "1.4.0")]
96+
impl IntoRawFd for RawFd {
97+
fn into_raw_fd(self) -> RawFd {
98+
self
99+
}
100+
}
101+
87102
#[stable(feature = "into_raw_os", since = "1.4.0")]
88103
impl IntoRawFd for fs::File {
89104
fn into_raw_fd(self) -> RawFd {

0 commit comments

Comments
 (0)