Skip to content

Commit

Permalink
Auto merge of #43459 - ids1024:asrawfd, r=alexcrichton
Browse files Browse the repository at this point in the history
Implement AsRawFd for Stdin, Stdout, and Stderr

rust-lang/rfcs#2074
  • Loading branch information
bors committed Aug 4, 2017
2 parents 1d2a6df + 64e426e commit eae446c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/libstd/sys/redox/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use fs;
use net;
use sys;
use io;
use sys_common::{self, AsInner, FromInner, IntoInner};

/// Raw file descriptors.
Expand Down Expand Up @@ -109,6 +110,21 @@ impl AsRawFd for net::UdpSocket {
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stdin {
fn as_raw_fd(&self) -> RawFd { 0 }
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stdout {
fn as_raw_fd(&self) -> RawFd { 1 }
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stderr {
fn as_raw_fd(&self) -> RawFd { 2 }
}

#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::TcpStream {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
Expand Down
17 changes: 17 additions & 0 deletions src/libstd/sys/unix/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use fs;
use net;
use os::raw;
use sys;
use io;
use sys_common::{self, AsInner, FromInner, IntoInner};
use libc;

/// Raw file descriptors.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -104,6 +106,21 @@ impl AsRawFd for net::UdpSocket {
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stdin {
fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stdout {
fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawFd for io::Stderr {
fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }
}

#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::TcpStream {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
Expand Down
22 changes: 22 additions & 0 deletions src/libstd/sys/windows/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use os::windows::raw;
use net;
use sys_common::{self, AsInner, FromInner, IntoInner};
use sys;
use io;
use sys::c;

/// Raw HANDLEs.
Expand Down Expand Up @@ -71,6 +72,27 @@ impl AsRawHandle for fs::File {
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawHandle for io::Stdin {
fn as_raw_handle(&self) -> RawHandle {
unsafe { c::GetStdHandle(c::STD_INPUT_HANDLE) as RawHandle }
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawHandle for io::Stdout {
fn as_raw_handle(&self) -> RawHandle {
unsafe { c::GetStdHandle(c::STD_OUTPUT_HANDLE) as RawHandle }
}
}

#[stable(feature = "asraw_stdio", since = "1.21.0")]
impl AsRawHandle for io::Stderr {
fn as_raw_handle(&self) -> RawHandle {
unsafe { c::GetStdHandle(c::STD_ERROR_HANDLE) as RawHandle }
}
}

#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawHandle for fs::File {
unsafe fn from_raw_handle(handle: RawHandle) -> fs::File {
Expand Down

0 comments on commit eae446c

Please sign in to comment.