From 4156bc44176d93296a0f1834690dd9792390cec6 Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Thu, 20 Nov 2014 18:26:47 -0800 Subject: [PATCH 1/3] sys: reveal std::io representation to sys module This commit adds a `AsInner` trait to `sys_common` and provides implementations on many `std::io` types. This is a building block for exposing platform-specific APIs that hook into `std::io` types. --- src/libstd/io/fs.rs | 4 ++-- src/libstd/io/net/pipe.rs | 20 ++++++++++++++++++++ src/libstd/io/net/tcp.rs | 20 ++++++++++++++++++++ src/libstd/io/net/udp.rs | 7 +++++++ src/libstd/io/pipe.rs | 4 ++-- src/libstd/sys/common/mod.rs | 9 ++++----- src/libstd/sys/unix/pipe.rs | 6 +++--- src/libstd/sys/unix/process.rs | 6 +++--- src/libstd/sys/windows/pipe.rs | 10 +++++++++- src/libstd/sys/windows/process.rs | 6 +++--- 10 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index cd4141e045cb5..6d29f3d25382c 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -88,8 +88,8 @@ pub struct File { last_nread: int, } -impl sys_common::AsFileDesc for File { - fn as_fd(&self) -> &fs_imp::FileDesc { +impl sys_common::AsInner for File { + fn as_inner(&self) -> &fs_imp::FileDesc { &self.fd } } diff --git a/src/libstd/io/net/pipe.rs b/src/libstd/io/net/pipe.rs index 8e934d221d22c..2984fa5963147 100644 --- a/src/libstd/io/net/pipe.rs +++ b/src/libstd/io/net/pipe.rs @@ -33,6 +33,8 @@ use sys::pipe::UnixStream as UnixStreamImp; use sys::pipe::UnixListener as UnixListenerImp; use sys::pipe::UnixAcceptor as UnixAcceptorImp; +use sys_common; + /// A stream which communicates over a named pipe. pub struct UnixStream { inner: UnixStreamImp, @@ -145,6 +147,12 @@ impl Writer for UnixStream { } } +impl sys_common::AsInner for UnixStream { + fn as_inner(&self) -> &UnixStreamImp { + &self.inner + } +} + /// A value that can listen for incoming named pipe connection requests. pub struct UnixListener { /// The internal, opaque runtime Unix listener. @@ -186,6 +194,12 @@ impl Listener for UnixListener { } } +impl sys_common::AsInner for UnixListener { + fn as_inner(&self) -> &UnixListenerImp { + &self.inner + } +} + /// A value that can accept named pipe connections, returned from `listen()`. pub struct UnixAcceptor { /// The internal, opaque runtime Unix acceptor. @@ -247,6 +261,12 @@ impl Clone for UnixAcceptor { } } +impl sys_common::AsInner for UnixAcceptor { + fn as_inner(&self) -> &UnixAcceptorImp { + &self.inner + } +} + #[cfg(test)] #[allow(experimental)] mod tests { diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs index cab54d82e1c5f..dc7970be8f54d 100644 --- a/src/libstd/io/net/tcp.rs +++ b/src/libstd/io/net/tcp.rs @@ -31,6 +31,8 @@ use sys::tcp::TcpStream as TcpStreamImp; use sys::tcp::TcpListener as TcpListenerImp; use sys::tcp::TcpAcceptor as TcpAcceptorImp; +use sys_common; + /// A structure which represents a TCP stream between a local socket and a /// remote socket. /// @@ -256,6 +258,12 @@ impl Writer for TcpStream { } } +impl sys_common::AsInner for TcpStream { + fn as_inner(&self) -> &TcpStreamImp { + &self.inner + } +} + /// A structure representing a socket server. This listener is used to create a /// `TcpAcceptor` which can be used to accept sockets on a local port. /// @@ -325,6 +333,12 @@ impl Listener for TcpListener { } } +impl sys_common::AsInner for TcpListener { + fn as_inner(&self) -> &TcpListenerImp { + &self.inner + } +} + /// The accepting half of a TCP socket server. This structure is created through /// a `TcpListener`'s `listen` method, and this object can be used to accept new /// `TcpStream` instances. @@ -452,6 +466,12 @@ impl Clone for TcpAcceptor { } } +impl sys_common::AsInner for TcpAcceptor { + fn as_inner(&self) -> &TcpAcceptorImp { + &self.inner + } +} + #[cfg(test)] #[allow(experimental)] mod test { diff --git a/src/libstd/io/net/udp.rs b/src/libstd/io/net/udp.rs index 567e7da0c000a..a7239ca0f2f98 100644 --- a/src/libstd/io/net/udp.rs +++ b/src/libstd/io/net/udp.rs @@ -21,6 +21,7 @@ use io::{Reader, Writer, IoResult}; use option::Option; use result::{Ok, Err}; use sys::udp::UdpSocket as UdpSocketImp; +use sys_common; /// A User Datagram Protocol socket. /// @@ -184,6 +185,12 @@ impl Clone for UdpSocket { } } +impl sys_common::AsInner for UdpSocket { + fn as_inner(&self) -> &UdpSocketImp { + &self.inner + } +} + /// A type that allows convenient usage of a UDP stream connected to one /// address via the `Reader` and `Writer` traits. /// diff --git a/src/libstd/io/pipe.rs b/src/libstd/io/pipe.rs index 8c20ea0886385..41676cdf6e9cc 100644 --- a/src/libstd/io/pipe.rs +++ b/src/libstd/io/pipe.rs @@ -86,8 +86,8 @@ impl PipeStream { } } -impl sys_common::AsFileDesc for PipeStream { - fn as_fd(&self) -> &sys::fs::FileDesc { +impl sys_common::AsInner for PipeStream { + fn as_inner(&self) -> &sys::fs::FileDesc { &*self.inner } } diff --git a/src/libstd/sys/common/mod.rs b/src/libstd/sys/common/mod.rs index cacb128faa560..769278eda9c16 100644 --- a/src/libstd/sys/common/mod.rs +++ b/src/libstd/sys/common/mod.rs @@ -13,7 +13,7 @@ use io::{mod, IoError, IoResult}; use prelude::*; -use sys::{last_error, retry, fs}; +use sys::{last_error, retry}; use c_str::CString; use num::Int; use path::BytesContainer; @@ -83,10 +83,9 @@ pub fn keep_going(data: &[u8], f: |*const u8, uint| -> i64) -> i64 { return (origamt - amt) as i64; } -// traits for extracting representations from - -pub trait AsFileDesc { - fn as_fd(&self) -> &fs::FileDesc; +// A trait for extracting representations from std::io types +pub trait AsInner { + fn as_inner(&self) -> &Inner; } pub trait ProcessConfig { diff --git a/src/libstd/sys/unix/pipe.rs b/src/libstd/sys/unix/pipe.rs index 4d3469a9c24a8..3f70fb5c1a56c 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 { 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 { 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 81bc138ca9195..76c316076f93e 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(cfg: &C, in_fd: Option

, out_fd: Option

, err_fd: Option

) -> IoResult - where C: ProcessConfig, P: AsFileDesc, + where C: ProcessConfig, P: AsInner, 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 diff --git a/src/libstd/sys/windows/pipe.rs b/src/libstd/sys/windows/pipe.rs index a623c2cd8e297..60bd2b1370f55 100644 --- a/src/libstd/sys/windows/pipe.rs +++ b/src/libstd/sys/windows/pipe.rs @@ -329,7 +329,7 @@ impl UnixStream { } } - fn handle(&self) -> libc::HANDLE { self.inner.handle } + pub fn handle(&self) -> libc::HANDLE { self.inner.handle } fn read_closed(&self) -> bool { self.inner.read_closed.load(atomic::SeqCst) @@ -585,6 +585,10 @@ impl UnixListener { }), }) } + + pub fn handle(&self) -> libc::HANDLE { + self.handle + } } impl Drop for UnixListener { @@ -729,6 +733,10 @@ impl UnixAcceptor { Ok(()) } } + + pub fn handle(&self) -> libc::HANDLE { + self.event.ref0 + } } impl Clone for UnixAcceptor { diff --git a/src/libstd/sys/windows/process.rs b/src/libstd/sys/windows/process.rs index 3fb5ee34356fe..eddb89c673d3e 100644 --- a/src/libstd/sys/windows/process.rs +++ b/src/libstd/sys/windows/process.rs @@ -26,7 +26,7 @@ use sys::fs; use sys::{mod, retry, c, wouldblock, set_nonblocking, ms_to_timeval, timer}; use sys::fs::FileDesc; use sys_common::helper_thread::Helper; -use sys_common::{AsFileDesc, mkerr_libc, timeout}; +use sys_common::{AsInner, mkerr_libc, timeout}; use io::fs::PathExtensions; use string::String; @@ -105,7 +105,7 @@ impl Process { pub fn spawn(cfg: &C, in_fd: Option

, out_fd: Option

, err_fd: Option

) -> IoResult - where C: ProcessConfig, P: AsFileDesc, + where C: ProcessConfig, P: AsInner, K: BytesContainer + Eq + Hash, V: BytesContainer { use libc::types::os::arch::extra::{DWORD, HANDLE, STARTUPINFO}; @@ -195,7 +195,7 @@ impl Process { } } Some(ref fd) => { - let orig = get_osfhandle(fd.as_fd().fd()) as HANDLE; + let orig = get_osfhandle(fd.as_inner().fd()) as HANDLE; if orig == INVALID_HANDLE_VALUE { return Err(super::last_error()) } From af0c446d3b434a26c6d0482569bbf67e4d9b7df4 Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Thu, 20 Nov 2014 18:30:12 -0800 Subject: [PATCH 2/3] libs: add std::os::unix module The new `std::os::unix` module exposes several extension traits for extracting file descriptors from `std::io` types. --- src/libstd/sys/unix/ext.rs | 107 +++++++++++++++++++++++++++++++++++++ src/libstd/sys/unix/mod.rs | 1 + 2 files changed, 108 insertions(+) create mode 100644 src/libstd/sys/unix/ext.rs diff --git a/src/libstd/sys/unix/ext.rs b/src/libstd/sys/unix/ext.rs new file mode 100644 index 0000000000000..ae3c939bf78bd --- /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 or the MIT license +// , 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 664a6a1e70c76..d800c76642603 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 os; pub mod tcp; From 1e661642105a1033f1c155ceb1b2335dd11cb40a Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Thu, 20 Nov 2014 18:30:46 -0800 Subject: [PATCH 3/3] libs: add std::os::windows module The new `std::os::windows` module exposes several extension traits for extracting file descriptors, sockets, and handles from `std::io` types. --- src/libstd/os.rs | 5 ++ src/libstd/sys/windows/ext.rs | 100 +++++++++++++++++++++++++++++++++ src/libstd/sys/windows/mod.rs | 1 + src/libstd/sys/windows/pipe.rs | 2 +- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/libstd/sys/windows/ext.rs diff --git a/src/libstd/os.rs b/src/libstd/os.rs index d7ba4877086ea..2ba03ac5d6044 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -62,6 +62,11 @@ use vec::Vec; #[cfg(unix)] use c_str::ToCStr; #[cfg(unix)] use libc::c_char; +#[cfg(unix)] +pub use sys::ext as unix; +#[cfg(windows)] +pub use sys::ext as windows; + /// Get the number of cores available pub fn num_cpus() -> uint { unsafe { diff --git a/src/libstd/sys/windows/ext.rs b/src/libstd/sys/windows/ext.rs new file mode 100644 index 0000000000000..2c58ee69e8b7c --- /dev/null +++ b/src/libstd/sys/windows/ext.rs @@ -0,0 +1,100 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Experimental extensions to `std` for Windows. +//! +//! For now, this module is limited to extracting handles, file +//! descriptors, and sockets, but its functionality will grow over +//! time. + +#![experimental] + +use sys_common::AsInner; +use libc; + +use io; + +/// Raw HANDLEs. +pub type Handle = libc::HANDLE; + +/// Raw SOCKETs. +pub type Socket = libc::SOCKET; + +/// Extract raw handles. +pub trait AsRawHandle { + /// Extract the raw handle, without taking any ownership. + fn as_raw_handle(&self) -> Handle; +} + +impl AsRawHandle for io::fs::File { + fn as_raw_handle(&self) -> Handle { + self.as_inner().handle() + } +} + +impl AsRawHandle for io::pipe::PipeStream { + fn as_raw_handle(&self) -> Handle { + self.as_inner().handle() + } +} + +impl AsRawHandle for io::net::pipe::UnixStream { + fn as_raw_handle(&self) -> Handle { + self.as_inner().handle() + } +} + +impl AsRawHandle for io::net::pipe::UnixListener { + fn as_raw_handle(&self) -> Handle { + self.as_inner().handle() + } +} + +impl AsRawHandle for io::net::pipe::UnixAcceptor { + fn as_raw_handle(&self) -> Handle { + self.as_inner().handle() + } +} + +/// Extract raw sockets. +pub trait AsRawSocket { + fn as_raw_socket(&self) -> Socket; +} + +impl AsRawSocket for io::net::tcp::TcpStream { + fn as_raw_socket(&self) -> Socket { + self.as_inner().fd() + } +} + +impl AsRawSocket for io::net::tcp::TcpListener { + fn as_raw_socket(&self) -> Socket { + self.as_inner().fd() + } +} + +impl AsRawSocket for io::net::tcp::TcpAcceptor { + fn as_raw_socket(&self) -> Socket { + self.as_inner().fd() + } +} + +impl AsRawSocket for io::net::udp::UdpSocket { + fn as_raw_socket(&self) -> Socket { + 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::{Socket, Handle, AsRawSocket, AsRawHandle}; +} diff --git a/src/libstd/sys/windows/mod.rs b/src/libstd/sys/windows/mod.rs index 815ace21f879d..33e7094612e39 100644 --- a/src/libstd/sys/windows/mod.rs +++ b/src/libstd/sys/windows/mod.rs @@ -34,6 +34,7 @@ macro_rules! helper_init( (static $name:ident: Helper<$m:ty>) => ( ) ) pub mod c; +pub mod ext; pub mod fs; pub mod os; pub mod tcp; diff --git a/src/libstd/sys/windows/pipe.rs b/src/libstd/sys/windows/pipe.rs index 60bd2b1370f55..ca7985aa35bf8 100644 --- a/src/libstd/sys/windows/pipe.rs +++ b/src/libstd/sys/windows/pipe.rs @@ -735,7 +735,7 @@ impl UnixAcceptor { } pub fn handle(&self) -> libc::HANDLE { - self.event.ref0 + self.listener.handle() } }