From 019b131e12f49f1953a52865c1f19b025c73c06e Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 2 Oct 2013 18:52:03 -0700 Subject: [PATCH] Stop using newtype wrappers in std::rt::io UnboundedPipeStream is still a newtype since process::set_stdio needs to look into its internals. Closes #9667 --- src/libstd/rt/io/native/file.rs | 9 +++++--- src/libstd/rt/io/net/tcp.rs | 32 +++++++++++++++++------------ src/libstd/rt/io/net/udp.rs | 16 ++++++++------- src/libstd/rt/io/pipe.rs | 13 ++++++++---- src/libstd/rt/io/timer.rs | 8 +++++--- src/test/run-pass/rtio-processes.rs | 2 +- 6 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/libstd/rt/io/native/file.rs b/src/libstd/rt/io/native/file.rs index f5f77f4e853..47ae89ccf9f 100644 --- a/src/libstd/rt/io/native/file.rs +++ b/src/libstd/rt/io/native/file.rs @@ -17,8 +17,9 @@ use libc::{c_int, FILE}; #[allow(non_camel_case_types)] pub type fd_t = c_int; -// Make this a newtype so we can't do I/O on arbitrary integers -pub struct FileDesc(fd_t); +pub struct FileDesc { + priv fd: fd_t +} impl FileDesc { /// Create a `FileDesc` from an open C file descriptor. @@ -46,7 +47,9 @@ impl Seek for FileDesc { fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail2!() } } -pub struct CFile(*FILE); +pub struct CFile { + priv file: *FILE +} impl CFile { /// Create a `CFile` from an open `FILE` pointer. diff --git a/src/libstd/rt/io/net/tcp.rs b/src/libstd/rt/io/net/tcp.rs index c1cda5ad681..ff2802c75b0 100644 --- a/src/libstd/rt/io/net/tcp.rs +++ b/src/libstd/rt/io/net/tcp.rs @@ -20,11 +20,13 @@ use rt::rtio::{IoFactory, IoFactoryObject, RtioTcpStream, RtioTcpStreamObject}; use rt::local::Local; -pub struct TcpStream(~RtioTcpStreamObject); +pub struct TcpStream { + priv obj: ~RtioTcpStreamObject +} impl TcpStream { fn new(s: ~RtioTcpStreamObject) -> TcpStream { - TcpStream(s) + TcpStream { obj: s } } pub fn connect(addr: SocketAddr) -> Option { @@ -46,7 +48,7 @@ impl TcpStream { } pub fn peer_name(&mut self) -> Option { - match (**self).peer_name() { + match self.obj.peer_name() { Ok(pn) => Some(pn), Err(ioerr) => { rtdebug!("failed to get peer name: {:?}", ioerr); @@ -57,7 +59,7 @@ impl TcpStream { } pub fn socket_name(&mut self) -> Option { - match (**self).socket_name() { + match self.obj.socket_name() { Ok(sn) => Some(sn), Err(ioerr) => { rtdebug!("failed to get socket name: {:?}", ioerr); @@ -70,7 +72,7 @@ impl TcpStream { impl Reader for TcpStream { fn read(&mut self, buf: &mut [u8]) -> Option { - match (**self).read(buf) { + match self.obj.read(buf) { Ok(read) => Some(read), Err(ioerr) => { // EOF is indicated by returning None @@ -87,7 +89,7 @@ impl Reader for TcpStream { impl Writer for TcpStream { fn write(&mut self, buf: &[u8]) { - match (**self).write(buf) { + match self.obj.write(buf) { Ok(_) => (), Err(ioerr) => io_error::cond.raise(ioerr), } @@ -96,7 +98,9 @@ impl Writer for TcpStream { fn flush(&mut self) { /* no-op */ } } -pub struct TcpListener(~RtioTcpListenerObject); +pub struct TcpListener { + priv obj: ~RtioTcpListenerObject +} impl TcpListener { pub fn bind(addr: SocketAddr) -> Option { @@ -105,7 +109,7 @@ impl TcpListener { (*io).tcp_bind(addr) }; match listener { - Ok(l) => Some(TcpListener(l)), + Ok(l) => Some(TcpListener { obj: l }), Err(ioerr) => { io_error::cond.raise(ioerr); return None; @@ -114,7 +118,7 @@ impl TcpListener { } pub fn socket_name(&mut self) -> Option { - match (**self).socket_name() { + match self.obj.socket_name() { Ok(sn) => Some(sn), Err(ioerr) => { rtdebug!("failed to get socket name: {:?}", ioerr); @@ -127,8 +131,8 @@ impl TcpListener { impl Listener for TcpListener { fn listen(self) -> Option { - match (**self).listen() { - Ok(acceptor) => Some(TcpAcceptor(acceptor)), + match self.obj.listen() { + Ok(acceptor) => Some(TcpAcceptor { obj: acceptor }), Err(ioerr) => { io_error::cond.raise(ioerr); None @@ -137,11 +141,13 @@ impl Listener for TcpListener { } } -pub struct TcpAcceptor(~RtioTcpAcceptorObject); +pub struct TcpAcceptor { + priv obj: ~RtioTcpAcceptorObject +} impl Acceptor for TcpAcceptor { fn accept(&mut self) -> Option { - match (**self).accept() { + match self.obj.accept() { Ok(s) => Some(TcpStream::new(s)), Err(ioerr) => { io_error::cond.raise(ioerr); diff --git a/src/libstd/rt/io/net/udp.rs b/src/libstd/rt/io/net/udp.rs index e47b9b9e925..2f9babf5789 100644 --- a/src/libstd/rt/io/net/udp.rs +++ b/src/libstd/rt/io/net/udp.rs @@ -16,7 +16,9 @@ use rt::io::{io_error, read_error, EndOfFile}; use rt::rtio::{RtioSocket, RtioUdpSocketObject, RtioUdpSocket, IoFactory, IoFactoryObject}; use rt::local::Local; -pub struct UdpSocket(~RtioUdpSocketObject); +pub struct UdpSocket { + priv obj: ~RtioUdpSocketObject +} impl UdpSocket { pub fn bind(addr: SocketAddr) -> Option { @@ -25,7 +27,7 @@ impl UdpSocket { (*factory).udp_bind(addr) }; match socket { - Ok(s) => Some(UdpSocket(s)), + Ok(s) => Some(UdpSocket { obj: s }), Err(ioerr) => { io_error::cond.raise(ioerr); None @@ -34,7 +36,7 @@ impl UdpSocket { } pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, SocketAddr)> { - match (**self).recvfrom(buf) { + match self.obj.recvfrom(buf) { Ok((nread, src)) => Some((nread, src)), Err(ioerr) => { // EOF is indicated by returning None @@ -47,7 +49,7 @@ impl UdpSocket { } pub fn sendto(&mut self, buf: &[u8], dst: SocketAddr) { - match (**self).sendto(buf, dst) { + match self.obj.sendto(buf, dst) { Ok(_) => (), Err(ioerr) => io_error::cond.raise(ioerr), } @@ -58,7 +60,7 @@ impl UdpSocket { } pub fn socket_name(&mut self) -> Option { - match (***self).socket_name() { + match self.obj.socket_name() { Ok(sn) => Some(sn), Err(ioerr) => { rtdebug!("failed to get socket name: {:?}", ioerr); @@ -70,8 +72,8 @@ impl UdpSocket { } pub struct UdpStream { - socket: UdpSocket, - connectedTo: SocketAddr + priv socket: UdpSocket, + priv connectedTo: SocketAddr } impl UdpStream { diff --git a/src/libstd/rt/io/pipe.rs b/src/libstd/rt/io/pipe.rs index 4186cce8c8d..251795ab238 100644 --- a/src/libstd/rt/io/pipe.rs +++ b/src/libstd/rt/io/pipe.rs @@ -20,7 +20,12 @@ use rt::local::Local; use rt::rtio::{RtioPipe, RtioPipeObject, IoFactoryObject, IoFactory}; use rt::rtio::RtioUnboundPipeObject; -pub struct PipeStream(RtioPipeObject); +pub struct PipeStream { + priv obj: RtioPipeObject +} + +// This should not be a newtype, but rt::uv::process::set_stdio needs to reach +// into the internals of this :( pub struct UnboundPipeStream(~RtioUnboundPipeObject); impl PipeStream { @@ -41,13 +46,13 @@ impl PipeStream { } pub fn bind(inner: RtioPipeObject) -> PipeStream { - PipeStream(inner) + PipeStream { obj: inner } } } impl Reader for PipeStream { fn read(&mut self, buf: &mut [u8]) -> Option { - match (**self).read(buf) { + match self.obj.read(buf) { Ok(read) => Some(read), Err(ioerr) => { // EOF is indicated by returning None @@ -64,7 +69,7 @@ impl Reader for PipeStream { impl Writer for PipeStream { fn write(&mut self, buf: &[u8]) { - match (**self).write(buf) { + match self.obj.write(buf) { Ok(_) => (), Err(ioerr) => { io_error::cond.raise(ioerr); diff --git a/src/libstd/rt/io/timer.rs b/src/libstd/rt/io/timer.rs index 19b33feacbd..c2cf9be3980 100644 --- a/src/libstd/rt/io/timer.rs +++ b/src/libstd/rt/io/timer.rs @@ -15,7 +15,9 @@ use rt::rtio::{IoFactory, IoFactoryObject, RtioTimer, RtioTimerObject}; use rt::local::Local; -pub struct Timer(~RtioTimerObject); +pub struct Timer { + priv obj: ~RtioTimerObject +} /// Sleep the current task for `msecs` milliseconds. pub fn sleep(msecs: u64) { @@ -34,7 +36,7 @@ impl Timer { (*io).timer_init() }; match timer { - Ok(t) => Some(Timer(t)), + Ok(t) => Some(Timer { obj: t }), Err(ioerr) => { rtdebug!("Timer::init: failed to init: {:?}", ioerr); io_error::cond.raise(ioerr); @@ -44,7 +46,7 @@ impl Timer { } pub fn sleep(&mut self, msecs: u64) { - (**self).sleep(msecs); + self.obj.sleep(msecs); } } diff --git a/src/test/run-pass/rtio-processes.rs b/src/test/run-pass/rtio-processes.rs index e4d3b0ed872..de8e10b2bc6 100644 --- a/src/test/run-pass/rtio-processes.rs +++ b/src/test/run-pass/rtio-processes.rs @@ -84,7 +84,7 @@ fn read_all(input: &mut Reader) -> ~str { let mut buf = [0, ..1024]; loop { match input.read(buf) { - None | Some(0) => { break } + None => { break } Some(n) => { ret = ret + str::from_utf8(buf.slice_to(n)); } } }