diff options
| author | bors <bors@rust-lang.org> | 2014-11-26 08:42:09 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-11-26 08:42:09 +0000 |
| commit | 61af40278909eb899f1bdfbb8c45d4e4fb3dad5d (patch) | |
| tree | 87d901e82bb5e19ed4e49216f2114c9ca060aee6 /src/libstd/sys/unix | |
| parent | 8d7b3199d9a285b66b4f9a49d97234c956cb5e6c (diff) | |
| parent | 1e661642105a1033f1c155ceb1b2335dd11cb40a (diff) | |
| download | rust-61af40278909eb899f1bdfbb8c45d4e4fb3dad5d.tar.gz rust-61af40278909eb899f1bdfbb8c45d4e4fb3dad5d.zip | |
auto merge of #19169 : aturon/rust/fds, r=alexcrichton
This PR adds some internal infrastructure to allow the private `std::sys` module to access internal representation details of `std::io`. It then exposes those details in two new, platform-specific API surfaces: `std::os::unix` and `std::os::windows`. To start with, these will provide the ability to extract file descriptors, HANDLEs, SOCKETs, and so on from `std::io` types. More functionality, and more specific platforms (e.g. `std::os::linux`) will be added over time. Closes #18897
Diffstat (limited to 'src/libstd/sys/unix')
| -rw-r--r-- | src/libstd/sys/unix/ext.rs | 107 | ||||
| -rw-r--r-- | src/libstd/sys/unix/mod.rs | 1 | ||||
| -rw-r--r-- | src/libstd/sys/unix/pipe.rs | 6 | ||||
| -rw-r--r-- | src/libstd/sys/unix/process.rs | 6 |
4 files changed, 114 insertions, 6 deletions
diff --git a/src/libstd/sys/unix/ext.rs b/src/libstd/sys/unix/ext.rs new file mode 100644 index 00000000000..ae3c939bf78 --- /dev/null +++ b/src/libstd/sys/unix/ext.rs @@ -0,0 +1,107 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Experimental extensions to `std` for Unix platforms. +//! +//! For now, this module is limited to extracting file descriptors, +//! but its functionality will grow over time. +//! +//! # Example +//! +//! ```rust,ignore +//! #![feature(globs)] +//! +//! use std::io::fs::File; +//! use std::os::unix::prelude::*; +//! +//! fn main() { +//! let f = File::create(&Path::new("foo.txt")).unwrap(); +//! let fd = f.as_raw_fd(); +//! +//! // use fd with native unix bindings +//! } +//! ``` + +#![experimental] + +use sys_common::AsInner; +use libc; + +use io; + +/// Raw file descriptors. +pub type Fd = libc::c_int; + +/// Extract raw file descriptor +pub trait AsRawFd { + /// Extract the raw file descriptor, without taking any ownership. + fn as_raw_fd(&self) -> Fd; +} + +impl AsRawFd for io::fs::File { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +impl AsRawFd for io::pipe::PipeStream { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +impl AsRawFd for io::net::pipe::UnixStream { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +impl AsRawFd for io::net::pipe::UnixListener { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +impl AsRawFd for io::net::pipe::UnixAcceptor { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +impl AsRawFd for io::net::tcp::TcpStream { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +impl AsRawFd for io::net::tcp::TcpListener { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +impl AsRawFd for io::net::tcp::TcpAcceptor { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +impl AsRawFd for io::net::udp::UdpSocket { + fn as_raw_fd(&self) -> Fd { + self.as_inner().fd() + } +} + +/// A prelude for conveniently writing platform-specific code. +/// +/// Includes all extension traits, and some important type definitions. +pub mod prelude { + pub use super::{Fd, AsRawFd}; +} diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index 7a5317b578d..af238905119 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -33,6 +33,7 @@ macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => ( ) ) pub mod c; +pub mod ext; pub mod fs; pub mod helper_signal; pub mod os; diff --git a/src/libstd/sys/unix/pipe.rs b/src/libstd/sys/unix/pipe.rs index 4d3469a9c24..3f70fb5c1a5 100644 --- a/src/libstd/sys/unix/pipe.rs +++ b/src/libstd/sys/unix/pipe.rs @@ -133,7 +133,7 @@ impl UnixStream { } } - fn fd(&self) -> fd_t { self.inner.fd } + pub fn fd(&self) -> fd_t { self.inner.fd } #[cfg(target_os = "linux")] fn lock_nonblocking(&self) {} @@ -222,7 +222,7 @@ impl UnixListener { }) } - fn fd(&self) -> fd_t { self.inner.fd } + pub fn fd(&self) -> fd_t { self.inner.fd } pub fn listen(self) -> IoResult<UnixAcceptor> { match unsafe { libc::listen(self.fd(), 128) } { @@ -260,7 +260,7 @@ struct AcceptorInner { } impl UnixAcceptor { - fn fd(&self) -> fd_t { self.inner.listener.fd() } + pub fn fd(&self) -> fd_t { self.inner.listener.fd() } pub fn accept(&mut self) -> IoResult<UnixStream> { let deadline = if self.deadline == 0 {None} else {Some(self.deadline)}; diff --git a/src/libstd/sys/unix/process.rs b/src/libstd/sys/unix/process.rs index 81bc138ca91..76c316076f9 100644 --- a/src/libstd/sys/unix/process.rs +++ b/src/libstd/sys/unix/process.rs @@ -24,7 +24,7 @@ use hash::Hash; use sys::{mod, retry, c, wouldblock, set_nonblocking, ms_to_timeval}; use sys::fs::FileDesc; use sys_common::helper_thread::Helper; -use sys_common::{AsFileDesc, mkerr_libc, timeout}; +use sys_common::{AsInner, mkerr_libc, timeout}; pub use sys_common::ProcessConfig; @@ -56,7 +56,7 @@ impl Process { pub fn spawn<K, V, C, P>(cfg: &C, in_fd: Option<P>, out_fd: Option<P>, err_fd: Option<P>) -> IoResult<Process> - where C: ProcessConfig<K, V>, P: AsFileDesc, + where C: ProcessConfig<K, V>, P: AsInner<FileDesc>, K: BytesContainer + Eq + Hash, V: BytesContainer { use libc::funcs::posix88::unistd::{fork, dup2, close, chdir, execvp}; @@ -183,7 +183,7 @@ impl Process { libc::open(devnull.as_ptr(), flags, 0) } Some(obj) => { - let fd = obj.as_fd().fd(); + let fd = obj.as_inner().fd(); // Leak the memory and the file descriptor. We're in the // child now an all our resources are going to be // cleaned up very soon |
