Skip to content

Commit 019b131

Browse files
committed
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
1 parent 371a7ec commit 019b131

File tree

6 files changed

+49
-31
lines changed

6 files changed

+49
-31
lines changed

src/libstd/rt/io/native/file.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use libc::{c_int, FILE};
1717
#[allow(non_camel_case_types)]
1818
pub type fd_t = c_int;
1919

20-
// Make this a newtype so we can't do I/O on arbitrary integers
21-
pub struct FileDesc(fd_t);
20+
pub struct FileDesc {
21+
priv fd: fd_t
22+
}
2223

2324
impl FileDesc {
2425
/// Create a `FileDesc` from an open C file descriptor.
@@ -46,7 +47,9 @@ impl Seek for FileDesc {
4647
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail2!() }
4748
}
4849

49-
pub struct CFile(*FILE);
50+
pub struct CFile {
51+
priv file: *FILE
52+
}
5053

5154
impl CFile {
5255
/// Create a `CFile` from an open `FILE` pointer.

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ use rt::rtio::{IoFactory, IoFactoryObject,
2020
RtioTcpStream, RtioTcpStreamObject};
2121
use rt::local::Local;
2222

23-
pub struct TcpStream(~RtioTcpStreamObject);
23+
pub struct TcpStream {
24+
priv obj: ~RtioTcpStreamObject
25+
}
2426

2527
impl TcpStream {
2628
fn new(s: ~RtioTcpStreamObject) -> TcpStream {
27-
TcpStream(s)
29+
TcpStream { obj: s }
2830
}
2931

3032
pub fn connect(addr: SocketAddr) -> Option<TcpStream> {
@@ -46,7 +48,7 @@ impl TcpStream {
4648
}
4749

4850
pub fn peer_name(&mut self) -> Option<SocketAddr> {
49-
match (**self).peer_name() {
51+
match self.obj.peer_name() {
5052
Ok(pn) => Some(pn),
5153
Err(ioerr) => {
5254
rtdebug!("failed to get peer name: {:?}", ioerr);
@@ -57,7 +59,7 @@ impl TcpStream {
5759
}
5860

5961
pub fn socket_name(&mut self) -> Option<SocketAddr> {
60-
match (**self).socket_name() {
62+
match self.obj.socket_name() {
6163
Ok(sn) => Some(sn),
6264
Err(ioerr) => {
6365
rtdebug!("failed to get socket name: {:?}", ioerr);
@@ -70,7 +72,7 @@ impl TcpStream {
7072

7173
impl Reader for TcpStream {
7274
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
73-
match (**self).read(buf) {
75+
match self.obj.read(buf) {
7476
Ok(read) => Some(read),
7577
Err(ioerr) => {
7678
// EOF is indicated by returning None
@@ -87,7 +89,7 @@ impl Reader for TcpStream {
8789

8890
impl Writer for TcpStream {
8991
fn write(&mut self, buf: &[u8]) {
90-
match (**self).write(buf) {
92+
match self.obj.write(buf) {
9193
Ok(_) => (),
9294
Err(ioerr) => io_error::cond.raise(ioerr),
9395
}
@@ -96,7 +98,9 @@ impl Writer for TcpStream {
9698
fn flush(&mut self) { /* no-op */ }
9799
}
98100

99-
pub struct TcpListener(~RtioTcpListenerObject);
101+
pub struct TcpListener {
102+
priv obj: ~RtioTcpListenerObject
103+
}
100104

101105
impl TcpListener {
102106
pub fn bind(addr: SocketAddr) -> Option<TcpListener> {
@@ -105,7 +109,7 @@ impl TcpListener {
105109
(*io).tcp_bind(addr)
106110
};
107111
match listener {
108-
Ok(l) => Some(TcpListener(l)),
112+
Ok(l) => Some(TcpListener { obj: l }),
109113
Err(ioerr) => {
110114
io_error::cond.raise(ioerr);
111115
return None;
@@ -114,7 +118,7 @@ impl TcpListener {
114118
}
115119

116120
pub fn socket_name(&mut self) -> Option<SocketAddr> {
117-
match (**self).socket_name() {
121+
match self.obj.socket_name() {
118122
Ok(sn) => Some(sn),
119123
Err(ioerr) => {
120124
rtdebug!("failed to get socket name: {:?}", ioerr);
@@ -127,8 +131,8 @@ impl TcpListener {
127131

128132
impl Listener<TcpStream, TcpAcceptor> for TcpListener {
129133
fn listen(self) -> Option<TcpAcceptor> {
130-
match (**self).listen() {
131-
Ok(acceptor) => Some(TcpAcceptor(acceptor)),
134+
match self.obj.listen() {
135+
Ok(acceptor) => Some(TcpAcceptor { obj: acceptor }),
132136
Err(ioerr) => {
133137
io_error::cond.raise(ioerr);
134138
None
@@ -137,11 +141,13 @@ impl Listener<TcpStream, TcpAcceptor> for TcpListener {
137141
}
138142
}
139143

140-
pub struct TcpAcceptor(~RtioTcpAcceptorObject);
144+
pub struct TcpAcceptor {
145+
priv obj: ~RtioTcpAcceptorObject
146+
}
141147

142148
impl Acceptor<TcpStream> for TcpAcceptor {
143149
fn accept(&mut self) -> Option<TcpStream> {
144-
match (**self).accept() {
150+
match self.obj.accept() {
145151
Ok(s) => Some(TcpStream::new(s)),
146152
Err(ioerr) => {
147153
io_error::cond.raise(ioerr);

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use rt::io::{io_error, read_error, EndOfFile};
1616
use rt::rtio::{RtioSocket, RtioUdpSocketObject, RtioUdpSocket, IoFactory, IoFactoryObject};
1717
use rt::local::Local;
1818

19-
pub struct UdpSocket(~RtioUdpSocketObject);
19+
pub struct UdpSocket {
20+
priv obj: ~RtioUdpSocketObject
21+
}
2022

2123
impl UdpSocket {
2224
pub fn bind(addr: SocketAddr) -> Option<UdpSocket> {
@@ -25,7 +27,7 @@ impl UdpSocket {
2527
(*factory).udp_bind(addr)
2628
};
2729
match socket {
28-
Ok(s) => Some(UdpSocket(s)),
30+
Ok(s) => Some(UdpSocket { obj: s }),
2931
Err(ioerr) => {
3032
io_error::cond.raise(ioerr);
3133
None
@@ -34,7 +36,7 @@ impl UdpSocket {
3436
}
3537

3638
pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, SocketAddr)> {
37-
match (**self).recvfrom(buf) {
39+
match self.obj.recvfrom(buf) {
3840
Ok((nread, src)) => Some((nread, src)),
3941
Err(ioerr) => {
4042
// EOF is indicated by returning None
@@ -47,7 +49,7 @@ impl UdpSocket {
4749
}
4850

4951
pub fn sendto(&mut self, buf: &[u8], dst: SocketAddr) {
50-
match (**self).sendto(buf, dst) {
52+
match self.obj.sendto(buf, dst) {
5153
Ok(_) => (),
5254
Err(ioerr) => io_error::cond.raise(ioerr),
5355
}
@@ -58,7 +60,7 @@ impl UdpSocket {
5860
}
5961

6062
pub fn socket_name(&mut self) -> Option<SocketAddr> {
61-
match (***self).socket_name() {
63+
match self.obj.socket_name() {
6264
Ok(sn) => Some(sn),
6365
Err(ioerr) => {
6466
rtdebug!("failed to get socket name: {:?}", ioerr);
@@ -70,8 +72,8 @@ impl UdpSocket {
7072
}
7173

7274
pub struct UdpStream {
73-
socket: UdpSocket,
74-
connectedTo: SocketAddr
75+
priv socket: UdpSocket,
76+
priv connectedTo: SocketAddr
7577
}
7678

7779
impl UdpStream {

src/libstd/rt/io/pipe.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ use rt::local::Local;
2020
use rt::rtio::{RtioPipe, RtioPipeObject, IoFactoryObject, IoFactory};
2121
use rt::rtio::RtioUnboundPipeObject;
2222

23-
pub struct PipeStream(RtioPipeObject);
23+
pub struct PipeStream {
24+
priv obj: RtioPipeObject
25+
}
26+
27+
// This should not be a newtype, but rt::uv::process::set_stdio needs to reach
28+
// into the internals of this :(
2429
pub struct UnboundPipeStream(~RtioUnboundPipeObject);
2530

2631
impl PipeStream {
@@ -41,13 +46,13 @@ impl PipeStream {
4146
}
4247

4348
pub fn bind(inner: RtioPipeObject) -> PipeStream {
44-
PipeStream(inner)
49+
PipeStream { obj: inner }
4550
}
4651
}
4752

4853
impl Reader for PipeStream {
4954
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
50-
match (**self).read(buf) {
55+
match self.obj.read(buf) {
5156
Ok(read) => Some(read),
5257
Err(ioerr) => {
5358
// EOF is indicated by returning None
@@ -64,7 +69,7 @@ impl Reader for PipeStream {
6469

6570
impl Writer for PipeStream {
6671
fn write(&mut self, buf: &[u8]) {
67-
match (**self).write(buf) {
72+
match self.obj.write(buf) {
6873
Ok(_) => (),
6974
Err(ioerr) => {
7075
io_error::cond.raise(ioerr);

src/libstd/rt/io/timer.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use rt::rtio::{IoFactory, IoFactoryObject,
1515
RtioTimer, RtioTimerObject};
1616
use rt::local::Local;
1717

18-
pub struct Timer(~RtioTimerObject);
18+
pub struct Timer {
19+
priv obj: ~RtioTimerObject
20+
}
1921

2022
/// Sleep the current task for `msecs` milliseconds.
2123
pub fn sleep(msecs: u64) {
@@ -34,7 +36,7 @@ impl Timer {
3436
(*io).timer_init()
3537
};
3638
match timer {
37-
Ok(t) => Some(Timer(t)),
39+
Ok(t) => Some(Timer { obj: t }),
3840
Err(ioerr) => {
3941
rtdebug!("Timer::init: failed to init: {:?}", ioerr);
4042
io_error::cond.raise(ioerr);
@@ -44,7 +46,7 @@ impl Timer {
4446
}
4547

4648
pub fn sleep(&mut self, msecs: u64) {
47-
(**self).sleep(msecs);
49+
self.obj.sleep(msecs);
4850
}
4951
}
5052

src/test/run-pass/rtio-processes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn read_all(input: &mut Reader) -> ~str {
8484
let mut buf = [0, ..1024];
8585
loop {
8686
match input.read(buf) {
87-
None | Some(0) => { break }
87+
None => { break }
8888
Some(n) => { ret = ret + str::from_utf8(buf.slice_to(n)); }
8989
}
9090
}

0 commit comments

Comments
 (0)