Skip to content

Commit 87ecfb7

Browse files
author
Eric Reed
committed
converted TCP interface to newtype structs
1 parent d0dc697 commit 87ecfb7

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

src/libstd/rt/io/net/tcp.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ use rt::rtio::{IoFactory, IoFactoryObject,
1818
RtioTcpStream, RtioTcpStreamObject};
1919
use rt::local::Local;
2020

21-
pub struct TcpStream {
22-
rtstream: ~RtioTcpStreamObject
23-
}
21+
pub struct TcpStream(~RtioTcpStreamObject);
2422

2523
impl TcpStream {
2624
fn new(s: ~RtioTcpStreamObject) -> TcpStream {
27-
TcpStream {
28-
rtstream: s
29-
}
25+
TcpStream(s)
3026
}
3127

3228
pub fn connect(addr: IpAddr) -> Option<TcpStream> {
@@ -38,22 +34,19 @@ impl TcpStream {
3834
};
3935

4036
match stream {
41-
Ok(s) => {
42-
Some(TcpStream::new(s))
43-
}
37+
Ok(s) => Some(TcpStream::new(s)),
4438
Err(ioerr) => {
4539
rtdebug!("failed to connect: %?", ioerr);
4640
io_error::cond.raise(ioerr);
47-
return None;
41+
None
4842
}
4943
}
5044
}
5145
}
5246

5347
impl Reader for TcpStream {
5448
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
55-
let bytes_read = self.rtstream.read(buf);
56-
match bytes_read {
49+
match (**self).read(buf) {
5750
Ok(read) => Some(read),
5851
Err(ioerr) => {
5952
// EOF is indicated by returning None
@@ -70,8 +63,7 @@ impl Reader for TcpStream {
7063

7164
impl Writer for TcpStream {
7265
fn write(&mut self, buf: &[u8]) {
73-
let res = self.rtstream.write(buf);
74-
match res {
66+
match (**self).write(buf) {
7567
Ok(_) => (),
7668
Err(ioerr) => {
7769
io_error::cond.raise(ioerr);
@@ -82,9 +74,7 @@ impl Writer for TcpStream {
8274
fn flush(&mut self) { fail!() }
8375
}
8476

85-
pub struct TcpListener {
86-
rtlistener: ~RtioTcpListenerObject,
87-
}
77+
pub struct TcpListener(~RtioTcpListenerObject);
8878

8979
impl TcpListener {
9080
pub fn bind(addr: IpAddr) -> Option<TcpListener> {
@@ -93,11 +83,7 @@ impl TcpListener {
9383
(*io).tcp_bind(addr)
9484
};
9585
match listener {
96-
Ok(l) => {
97-
Some(TcpListener {
98-
rtlistener: l
99-
})
100-
}
86+
Ok(l) => Some(TcpListener(l)),
10187
Err(ioerr) => {
10288
io_error::cond.raise(ioerr);
10389
return None;
@@ -108,8 +94,7 @@ impl TcpListener {
10894

10995
impl Listener<TcpStream> for TcpListener {
11096
fn accept(&mut self) -> Option<TcpStream> {
111-
let rtstream = self.rtlistener.accept();
112-
match rtstream {
97+
match (**self).accept() {
11398
Ok(s) => {
11499
Some(TcpStream::new(s))
115100
}

0 commit comments

Comments
 (0)