Skip to content

Commit 4156bc4

Browse files
committed
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.
1 parent c9f6d69 commit 4156bc4

File tree

10 files changed

+73
-19
lines changed

10 files changed

+73
-19
lines changed

src/libstd/io/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ pub struct File {
8888
last_nread: int,
8989
}
9090

91-
impl sys_common::AsFileDesc for File {
92-
fn as_fd(&self) -> &fs_imp::FileDesc {
91+
impl sys_common::AsInner<fs_imp::FileDesc> for File {
92+
fn as_inner(&self) -> &fs_imp::FileDesc {
9393
&self.fd
9494
}
9595
}

src/libstd/io/net/pipe.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ use sys::pipe::UnixStream as UnixStreamImp;
3333
use sys::pipe::UnixListener as UnixListenerImp;
3434
use sys::pipe::UnixAcceptor as UnixAcceptorImp;
3535

36+
use sys_common;
37+
3638
/// A stream which communicates over a named pipe.
3739
pub struct UnixStream {
3840
inner: UnixStreamImp,
@@ -145,6 +147,12 @@ impl Writer for UnixStream {
145147
}
146148
}
147149

150+
impl sys_common::AsInner<UnixStreamImp> for UnixStream {
151+
fn as_inner(&self) -> &UnixStreamImp {
152+
&self.inner
153+
}
154+
}
155+
148156
/// A value that can listen for incoming named pipe connection requests.
149157
pub struct UnixListener {
150158
/// The internal, opaque runtime Unix listener.
@@ -186,6 +194,12 @@ impl Listener<UnixStream, UnixAcceptor> for UnixListener {
186194
}
187195
}
188196

197+
impl sys_common::AsInner<UnixListenerImp> for UnixListener {
198+
fn as_inner(&self) -> &UnixListenerImp {
199+
&self.inner
200+
}
201+
}
202+
189203
/// A value that can accept named pipe connections, returned from `listen()`.
190204
pub struct UnixAcceptor {
191205
/// The internal, opaque runtime Unix acceptor.
@@ -247,6 +261,12 @@ impl Clone for UnixAcceptor {
247261
}
248262
}
249263

264+
impl sys_common::AsInner<UnixAcceptorImp> for UnixAcceptor {
265+
fn as_inner(&self) -> &UnixAcceptorImp {
266+
&self.inner
267+
}
268+
}
269+
250270
#[cfg(test)]
251271
#[allow(experimental)]
252272
mod tests {

src/libstd/io/net/tcp.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use sys::tcp::TcpStream as TcpStreamImp;
3131
use sys::tcp::TcpListener as TcpListenerImp;
3232
use sys::tcp::TcpAcceptor as TcpAcceptorImp;
3333

34+
use sys_common;
35+
3436
/// A structure which represents a TCP stream between a local socket and a
3537
/// remote socket.
3638
///
@@ -256,6 +258,12 @@ impl Writer for TcpStream {
256258
}
257259
}
258260

261+
impl sys_common::AsInner<TcpStreamImp> for TcpStream {
262+
fn as_inner(&self) -> &TcpStreamImp {
263+
&self.inner
264+
}
265+
}
266+
259267
/// A structure representing a socket server. This listener is used to create a
260268
/// `TcpAcceptor` which can be used to accept sockets on a local port.
261269
///
@@ -325,6 +333,12 @@ impl Listener<TcpStream, TcpAcceptor> for TcpListener {
325333
}
326334
}
327335

336+
impl sys_common::AsInner<TcpListenerImp> for TcpListener {
337+
fn as_inner(&self) -> &TcpListenerImp {
338+
&self.inner
339+
}
340+
}
341+
328342
/// The accepting half of a TCP socket server. This structure is created through
329343
/// a `TcpListener`'s `listen` method, and this object can be used to accept new
330344
/// `TcpStream` instances.
@@ -452,6 +466,12 @@ impl Clone for TcpAcceptor {
452466
}
453467
}
454468

469+
impl sys_common::AsInner<TcpAcceptorImp> for TcpAcceptor {
470+
fn as_inner(&self) -> &TcpAcceptorImp {
471+
&self.inner
472+
}
473+
}
474+
455475
#[cfg(test)]
456476
#[allow(experimental)]
457477
mod test {

src/libstd/io/net/udp.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use io::{Reader, Writer, IoResult};
2121
use option::Option;
2222
use result::{Ok, Err};
2323
use sys::udp::UdpSocket as UdpSocketImp;
24+
use sys_common;
2425

2526
/// A User Datagram Protocol socket.
2627
///
@@ -184,6 +185,12 @@ impl Clone for UdpSocket {
184185
}
185186
}
186187

188+
impl sys_common::AsInner<UdpSocketImp> for UdpSocket {
189+
fn as_inner(&self) -> &UdpSocketImp {
190+
&self.inner
191+
}
192+
}
193+
187194
/// A type that allows convenient usage of a UDP stream connected to one
188195
/// address via the `Reader` and `Writer` traits.
189196
///

src/libstd/io/pipe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ impl PipeStream {
8686
}
8787
}
8888

89-
impl sys_common::AsFileDesc for PipeStream {
90-
fn as_fd(&self) -> &sys::fs::FileDesc {
89+
impl sys_common::AsInner<sys::fs::FileDesc> for PipeStream {
90+
fn as_inner(&self) -> &sys::fs::FileDesc {
9191
&*self.inner
9292
}
9393
}

src/libstd/sys/common/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use io::{mod, IoError, IoResult};
1515
use prelude::*;
16-
use sys::{last_error, retry, fs};
16+
use sys::{last_error, retry};
1717
use c_str::CString;
1818
use num::Int;
1919
use path::BytesContainer;
@@ -83,10 +83,9 @@ pub fn keep_going(data: &[u8], f: |*const u8, uint| -> i64) -> i64 {
8383
return (origamt - amt) as i64;
8484
}
8585

86-
// traits for extracting representations from
87-
88-
pub trait AsFileDesc {
89-
fn as_fd(&self) -> &fs::FileDesc;
86+
// A trait for extracting representations from std::io types
87+
pub trait AsInner<Inner> {
88+
fn as_inner(&self) -> &Inner;
9089
}
9190

9291
pub trait ProcessConfig<K: BytesContainer, V: BytesContainer> {

src/libstd/sys/unix/pipe.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl UnixStream {
133133
}
134134
}
135135

136-
fn fd(&self) -> fd_t { self.inner.fd }
136+
pub fn fd(&self) -> fd_t { self.inner.fd }
137137

138138
#[cfg(target_os = "linux")]
139139
fn lock_nonblocking(&self) {}
@@ -222,7 +222,7 @@ impl UnixListener {
222222
})
223223
}
224224

225-
fn fd(&self) -> fd_t { self.inner.fd }
225+
pub fn fd(&self) -> fd_t { self.inner.fd }
226226

227227
pub fn listen(self) -> IoResult<UnixAcceptor> {
228228
match unsafe { libc::listen(self.fd(), 128) } {
@@ -260,7 +260,7 @@ struct AcceptorInner {
260260
}
261261

262262
impl UnixAcceptor {
263-
fn fd(&self) -> fd_t { self.inner.listener.fd() }
263+
pub fn fd(&self) -> fd_t { self.inner.listener.fd() }
264264

265265
pub fn accept(&mut self) -> IoResult<UnixStream> {
266266
let deadline = if self.deadline == 0 {None} else {Some(self.deadline)};

src/libstd/sys/unix/process.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use hash::Hash;
2424
use sys::{mod, retry, c, wouldblock, set_nonblocking, ms_to_timeval};
2525
use sys::fs::FileDesc;
2626
use sys_common::helper_thread::Helper;
27-
use sys_common::{AsFileDesc, mkerr_libc, timeout};
27+
use sys_common::{AsInner, mkerr_libc, timeout};
2828

2929
pub use sys_common::ProcessConfig;
3030

@@ -56,7 +56,7 @@ impl Process {
5656
pub fn spawn<K, V, C, P>(cfg: &C, in_fd: Option<P>,
5757
out_fd: Option<P>, err_fd: Option<P>)
5858
-> IoResult<Process>
59-
where C: ProcessConfig<K, V>, P: AsFileDesc,
59+
where C: ProcessConfig<K, V>, P: AsInner<FileDesc>,
6060
K: BytesContainer + Eq + Hash, V: BytesContainer
6161
{
6262
use libc::funcs::posix88::unistd::{fork, dup2, close, chdir, execvp};
@@ -183,7 +183,7 @@ impl Process {
183183
libc::open(devnull.as_ptr(), flags, 0)
184184
}
185185
Some(obj) => {
186-
let fd = obj.as_fd().fd();
186+
let fd = obj.as_inner().fd();
187187
// Leak the memory and the file descriptor. We're in the
188188
// child now an all our resources are going to be
189189
// cleaned up very soon

src/libstd/sys/windows/pipe.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl UnixStream {
329329
}
330330
}
331331

332-
fn handle(&self) -> libc::HANDLE { self.inner.handle }
332+
pub fn handle(&self) -> libc::HANDLE { self.inner.handle }
333333

334334
fn read_closed(&self) -> bool {
335335
self.inner.read_closed.load(atomic::SeqCst)
@@ -585,6 +585,10 @@ impl UnixListener {
585585
}),
586586
})
587587
}
588+
589+
pub fn handle(&self) -> libc::HANDLE {
590+
self.handle
591+
}
588592
}
589593

590594
impl Drop for UnixListener {
@@ -729,6 +733,10 @@ impl UnixAcceptor {
729733
Ok(())
730734
}
731735
}
736+
737+
pub fn handle(&self) -> libc::HANDLE {
738+
self.event.ref0
739+
}
732740
}
733741

734742
impl Clone for UnixAcceptor {

src/libstd/sys/windows/process.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use sys::fs;
2626
use sys::{mod, retry, c, wouldblock, set_nonblocking, ms_to_timeval, timer};
2727
use sys::fs::FileDesc;
2828
use sys_common::helper_thread::Helper;
29-
use sys_common::{AsFileDesc, mkerr_libc, timeout};
29+
use sys_common::{AsInner, mkerr_libc, timeout};
3030

3131
use io::fs::PathExtensions;
3232
use string::String;
@@ -105,7 +105,7 @@ impl Process {
105105
pub fn spawn<K, V, C, P>(cfg: &C, in_fd: Option<P>,
106106
out_fd: Option<P>, err_fd: Option<P>)
107107
-> IoResult<Process>
108-
where C: ProcessConfig<K, V>, P: AsFileDesc,
108+
where C: ProcessConfig<K, V>, P: AsInner<FileDesc>,
109109
K: BytesContainer + Eq + Hash, V: BytesContainer
110110
{
111111
use libc::types::os::arch::extra::{DWORD, HANDLE, STARTUPINFO};
@@ -195,7 +195,7 @@ impl Process {
195195
}
196196
}
197197
Some(ref fd) => {
198-
let orig = get_osfhandle(fd.as_fd().fd()) as HANDLE;
198+
let orig = get_osfhandle(fd.as_inner().fd()) as HANDLE;
199199
if orig == INVALID_HANDLE_VALUE {
200200
return Err(super::last_error())
201201
}

0 commit comments

Comments
 (0)