From 87ecfb74357b669308a6e337ebc766af8a03b554 Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Wed, 26 Jun 2013 09:37:48 -0700 Subject: converted TCP interface to newtype structs --- src/libstd/rt/io/net/tcp.rs | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/rt/io/net/tcp.rs b/src/libstd/rt/io/net/tcp.rs index 3607f781da3..947fade096b 100644 --- a/src/libstd/rt/io/net/tcp.rs +++ b/src/libstd/rt/io/net/tcp.rs @@ -18,15 +18,11 @@ use rt::rtio::{IoFactory, IoFactoryObject, RtioTcpStream, RtioTcpStreamObject}; use rt::local::Local; -pub struct TcpStream { - rtstream: ~RtioTcpStreamObject -} +pub struct TcpStream(~RtioTcpStreamObject); impl TcpStream { fn new(s: ~RtioTcpStreamObject) -> TcpStream { - TcpStream { - rtstream: s - } + TcpStream(s) } pub fn connect(addr: IpAddr) -> Option { @@ -38,13 +34,11 @@ impl TcpStream { }; match stream { - Ok(s) => { - Some(TcpStream::new(s)) - } + Ok(s) => Some(TcpStream::new(s)), Err(ioerr) => { rtdebug!("failed to connect: %?", ioerr); io_error::cond.raise(ioerr); - return None; + None } } } @@ -52,8 +46,7 @@ impl TcpStream { impl Reader for TcpStream { fn read(&mut self, buf: &mut [u8]) -> Option { - let bytes_read = self.rtstream.read(buf); - match bytes_read { + match (**self).read(buf) { Ok(read) => Some(read), Err(ioerr) => { // EOF is indicated by returning None @@ -70,8 +63,7 @@ impl Reader for TcpStream { impl Writer for TcpStream { fn write(&mut self, buf: &[u8]) { - let res = self.rtstream.write(buf); - match res { + match (**self).write(buf) { Ok(_) => (), Err(ioerr) => { io_error::cond.raise(ioerr); @@ -82,9 +74,7 @@ impl Writer for TcpStream { fn flush(&mut self) { fail!() } } -pub struct TcpListener { - rtlistener: ~RtioTcpListenerObject, -} +pub struct TcpListener(~RtioTcpListenerObject); impl TcpListener { pub fn bind(addr: IpAddr) -> Option { @@ -93,11 +83,7 @@ impl TcpListener { (*io).tcp_bind(addr) }; match listener { - Ok(l) => { - Some(TcpListener { - rtlistener: l - }) - } + Ok(l) => Some(TcpListener(l)), Err(ioerr) => { io_error::cond.raise(ioerr); return None; @@ -108,8 +94,7 @@ impl TcpListener { impl Listener for TcpListener { fn accept(&mut self) -> Option { - let rtstream = self.rtlistener.accept(); - match rtstream { + match (**self).accept() { Ok(s) => { Some(TcpStream::new(s)) } -- cgit 1.4.1-3-g733a5