Skip to content

Commit 34b1135

Browse files
author
Eric Reed
committed
Converted UdpSocket into a newtype struct and (dis)connecting uses move semantics rather than ~.
1 parent f604686 commit 34b1135

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

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

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

19-
pub struct UdpSocket {
20-
rtsocket: ~RtioUdpSocketObject
21-
}
19+
pub struct UdpSocket(~RtioUdpSocketObject);
2220

2321
impl UdpSocket {
24-
fn new(s: ~RtioUdpSocketObject) -> UdpSocket {
25-
UdpSocket { rtsocket: s }
26-
}
22+
fn new(s: ~RtioUdpSocketObject) -> UdpSocket { UdpSocket(s) }
2723

2824
pub fn bind(addr: IpAddr) -> Option<UdpSocket> {
29-
let socket = unsafe {
30-
let io = Local::unsafe_borrow::<IoFactoryObject>();
31-
(*io).udp_bind(addr)
32-
};
25+
let socket = unsafe { (*Local::unsafe_borrow::<IoFactoryObject>()).udp_bind(addr) };
3326
match socket {
34-
Ok(s) => { Some(UdpSocket { rtsocket: s }) }
27+
Ok(s) => Some(UdpSocket::new(s)),
3528
Err(ioerr) => {
3629
io_error::cond.raise(ioerr);
37-
return None;
30+
None
3831
}
3932
}
4033
}
4134

4235
pub fn recvfrom(&self, buf: &mut [u8]) -> Option<(uint, IpAddr)> {
43-
match (*self.rtsocket).recvfrom(buf) {
36+
match (**self).recvfrom(buf) {
4437
Ok((nread, src)) => Some((nread, src)),
4538
Err(ioerr) => {
4639
// EOF is indicated by returning None
@@ -53,34 +46,26 @@ impl UdpSocket {
5346
}
5447

5548
pub fn sendto(&self, buf: &[u8], dst: IpAddr) {
56-
match (*self.rtsocket).sendto(buf, dst) {
49+
match (**self).sendto(buf, dst) {
5750
Ok(_) => (),
58-
Err(ioerr) => {
59-
io_error::cond.raise(ioerr);
60-
}
51+
Err(ioerr) => io_error::cond.raise(ioerr),
6152
}
6253
}
6354

64-
// XXX convert ~self to self eventually
65-
pub fn connect(~self, other: IpAddr) -> UdpStream {
55+
pub fn connect(self, other: IpAddr) -> UdpStream {
6656
UdpStream { socket: self, connectedTo: other }
6757
}
6858
}
6959

7060
pub struct UdpStream {
71-
socket: ~UdpSocket,
61+
socket: UdpSocket,
7262
connectedTo: IpAddr
7363
}
7464

7565
impl UdpStream {
76-
pub fn as_socket<T>(&self, f: &fn(&UdpSocket) -> T) -> T {
77-
f(self.socket)
78-
}
66+
pub fn as_socket<T>(&self, f: &fn(&UdpSocket) -> T) -> T { f(&self.socket) }
7967

80-
pub fn disconnect(self) -> ~UdpSocket {
81-
let UdpStream { socket: s, _ } = self;
82-
s
83-
}
68+
pub fn disconnect(self) -> UdpSocket { self.socket }
8469
}
8570

8671
impl Reader for UdpStream {

0 commit comments

Comments
 (0)