Skip to content

Commit 6bd80f7

Browse files
committed
librustuv: Change with_local_io to use RAII.
1 parent 8c2ebe1 commit 6bd80f7

File tree

14 files changed

+187
-158
lines changed

14 files changed

+187
-158
lines changed

src/librustuv/uvio.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use std::c_str::CString;
12+
use std::cast;
1213
use std::comm::SharedChan;
1314
use std::libc::c_int;
1415
use std::libc;
@@ -161,8 +162,11 @@ impl EventLoop for UvEventLoop {
161162
~AsyncWatcher::new(self.uvio.uv_loop(), f) as ~RemoteCallback
162163
}
163164

164-
fn io<'a>(&'a mut self, f: |&'a mut IoFactory|) {
165-
f(&mut self.uvio as &mut IoFactory)
165+
fn io(&mut self) -> &'static mut IoFactory:'static {
166+
unsafe {
167+
let factory = &mut self.uvio as &mut IoFactory;
168+
cast::transmute(factory)
169+
}
166170
}
167171
}
168172

src/libstd/io/fs.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use iter::Iterator;
5151
use super::{Reader, Writer, Seek};
5252
use super::{SeekStyle, Read, Write, Open, IoError, Truncate,
5353
FileMode, FileAccess, FileStat, io_error, FilePermission};
54-
use rt::rtio::{RtioFileStream, IoFactory, with_local_io};
54+
use rt::rtio::{RtioFileStream, IoFactory, LocalIo};
5555
use io;
5656
use option::{Some, None, Option};
5757
use result::{Ok, Err, Result};
@@ -76,15 +76,14 @@ pub struct File {
7676
}
7777

7878
fn io_raise<T>(f: |io: &mut IoFactory| -> Result<T, IoError>) -> Option<T> {
79-
with_local_io(|io| {
80-
match f(io) {
81-
Ok(t) => Some(t),
82-
Err(ioerr) => {
83-
io_error::cond.raise(ioerr);
84-
None
85-
}
79+
let mut io = LocalIo::borrow();
80+
match f(io.get()) {
81+
Ok(t) => Some(t),
82+
Err(ioerr) => {
83+
io_error::cond.raise(ioerr);
84+
None
8685
}
87-
})
86+
}
8887
}
8988

9089
impl File {
@@ -132,19 +131,18 @@ impl File {
132131
pub fn open_mode(path: &Path,
133132
mode: FileMode,
134133
access: FileAccess) -> Option<File> {
135-
with_local_io(|io| {
136-
match io.fs_open(&path.to_c_str(), mode, access) {
137-
Ok(fd) => Some(File {
138-
path: path.clone(),
139-
fd: fd,
140-
last_nread: -1
141-
}),
142-
Err(ioerr) => {
143-
io_error::cond.raise(ioerr);
144-
None
145-
}
134+
let mut io = LocalIo::borrow();
135+
match io.get().fs_open(&path.to_c_str(), mode, access) {
136+
Ok(fd) => Some(File {
137+
path: path.clone(),
138+
fd: fd,
139+
last_nread: -1
140+
}),
141+
Err(ioerr) => {
142+
io_error::cond.raise(ioerr);
143+
None
146144
}
147-
})
145+
}
148146
}
149147

150148
/// Attempts to open a file in read-only mode. This function is equivalent to

src/libstd/io/native/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,6 @@ impl rtio::IoFactory for IoFactory {
223223
Err(unimpl())
224224
}
225225
}
226+
227+
pub static mut NATIVE_IO_FACTORY: IoFactory = IoFactory;
228+

src/libstd/io/net/addrinfo.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use option::{Option, Some, None};
2121
use result::{Ok, Err};
2222
use io::{io_error};
2323
use io::net::ip::{SocketAddr, IpAddr};
24-
use rt::rtio::{IoFactory, with_local_io};
24+
use rt::rtio::{IoFactory, LocalIo};
2525
use vec::ImmutableVector;
2626

2727
/// Hints to the types of sockets that are desired when looking up hosts
@@ -95,17 +95,16 @@ pub fn get_host_addresses(host: &str) -> Option<~[IpAddr]> {
9595
///
9696
/// XXX: this is not public because the `Hint` structure is not ready for public
9797
/// consumption just yet.
98-
fn lookup(hostname: Option<&str>, servname: Option<&str>,
99-
hint: Option<Hint>) -> Option<~[Info]> {
100-
with_local_io(|io| {
101-
match io.get_host_addresses(hostname, servname, hint) {
102-
Ok(i) => Some(i),
103-
Err(ioerr) => {
104-
io_error::cond.raise(ioerr);
105-
None
106-
}
98+
fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
99+
-> Option<~[Info]> {
100+
let mut io = LocalIo::borrow();
101+
match io.get().get_host_addresses(hostname, servname, hint) {
102+
Ok(i) => Some(i),
103+
Err(ioerr) => {
104+
io_error::cond.raise(ioerr);
105+
None
107106
}
108-
})
107+
}
109108
}
110109

111110
#[cfg(test)]

src/libstd/io/net/tcp.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use result::{Ok, Err};
1313
use io::net::ip::SocketAddr;
1414
use io::{Reader, Writer, Listener, Acceptor};
1515
use io::{io_error, EndOfFile};
16-
use rt::rtio::{IoFactory, with_local_io,
17-
RtioSocket, RtioTcpListener, RtioTcpAcceptor, RtioTcpStream};
16+
use rt::rtio::{IoFactory, LocalIo, RtioSocket, RtioTcpListener};
17+
use rt::rtio::{RtioTcpAcceptor, RtioTcpStream};
1818

1919
pub struct TcpStream {
2020
priv obj: ~RtioTcpStream
@@ -26,15 +26,17 @@ impl TcpStream {
2626
}
2727

2828
pub fn connect(addr: SocketAddr) -> Option<TcpStream> {
29-
with_local_io(|io| {
30-
match io.tcp_connect(addr) {
31-
Ok(s) => Some(TcpStream::new(s)),
32-
Err(ioerr) => {
33-
io_error::cond.raise(ioerr);
34-
None
35-
}
29+
let result = {
30+
let mut io = LocalIo::borrow();
31+
io.get().tcp_connect(addr)
32+
};
33+
match result {
34+
Ok(s) => Some(TcpStream::new(s)),
35+
Err(ioerr) => {
36+
io_error::cond.raise(ioerr);
37+
None
3638
}
37-
})
39+
}
3840
}
3941

4042
pub fn peer_name(&mut self) -> Option<SocketAddr> {
@@ -92,15 +94,14 @@ pub struct TcpListener {
9294

9395
impl TcpListener {
9496
pub fn bind(addr: SocketAddr) -> Option<TcpListener> {
95-
with_local_io(|io| {
96-
match io.tcp_bind(addr) {
97-
Ok(l) => Some(TcpListener { obj: l }),
98-
Err(ioerr) => {
99-
io_error::cond.raise(ioerr);
100-
None
101-
}
97+
let mut io = LocalIo::borrow();
98+
match io.get().tcp_bind(addr) {
99+
Ok(l) => Some(TcpListener { obj: l }),
100+
Err(ioerr) => {
101+
io_error::cond.raise(ioerr);
102+
None
102103
}
103-
})
104+
}
104105
}
105106

106107
pub fn socket_name(&mut self) -> Option<SocketAddr> {

src/libstd/io/net/udp.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,22 @@ use result::{Ok, Err};
1313
use io::net::ip::SocketAddr;
1414
use io::{Reader, Writer};
1515
use io::{io_error, EndOfFile};
16-
use rt::rtio::{RtioSocket, RtioUdpSocket, IoFactory, with_local_io};
16+
use rt::rtio::{RtioSocket, RtioUdpSocket, IoFactory, LocalIo};
1717

1818
pub struct UdpSocket {
1919
priv obj: ~RtioUdpSocket
2020
}
2121

2222
impl UdpSocket {
2323
pub fn bind(addr: SocketAddr) -> Option<UdpSocket> {
24-
with_local_io(|io| {
25-
match io.udp_bind(addr) {
26-
Ok(s) => Some(UdpSocket { obj: s }),
27-
Err(ioerr) => {
28-
io_error::cond.raise(ioerr);
29-
None
30-
}
24+
let mut io = LocalIo::borrow();
25+
match io.get().udp_bind(addr) {
26+
Ok(s) => Some(UdpSocket { obj: s }),
27+
Err(ioerr) => {
28+
io_error::cond.raise(ioerr);
29+
None
3130
}
32-
})
31+
}
3332
}
3433

3534
pub fn recvfrom(&mut self, buf: &mut [u8]) -> Option<(uint, SocketAddr)> {

src/libstd/io/net/unix.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ instances as clients.
2525
use prelude::*;
2626

2727
use c_str::ToCStr;
28-
use rt::rtio::{IoFactory, RtioUnixListener, with_local_io};
28+
use rt::rtio::{IoFactory, LocalIo, RtioUnixListener};
2929
use rt::rtio::{RtioUnixAcceptor, RtioPipe};
3030
use io::pipe::PipeStream;
3131
use io::{io_error, Listener, Acceptor, Reader, Writer};
@@ -59,15 +59,14 @@ impl UnixStream {
5959
/// stream.write([1, 2, 3]);
6060
///
6161
pub fn connect<P: ToCStr>(path: &P) -> Option<UnixStream> {
62-
with_local_io(|io| {
63-
match io.unix_connect(&path.to_c_str()) {
64-
Ok(s) => Some(UnixStream::new(s)),
65-
Err(ioerr) => {
66-
io_error::cond.raise(ioerr);
67-
None
68-
}
62+
let mut io = LocalIo::borrow();
63+
match io.get().unix_connect(&path.to_c_str()) {
64+
Ok(s) => Some(UnixStream::new(s)),
65+
Err(ioerr) => {
66+
io_error::cond.raise(ioerr);
67+
None
6968
}
70-
})
69+
}
7170
}
7271
}
7372

@@ -108,15 +107,14 @@ impl UnixListener {
108107
/// }
109108
///
110109
pub fn bind<P: ToCStr>(path: &P) -> Option<UnixListener> {
111-
with_local_io(|io| {
112-
match io.unix_bind(&path.to_c_str()) {
113-
Ok(s) => Some(UnixListener{ obj: s }),
114-
Err(ioerr) => {
115-
io_error::cond.raise(ioerr);
116-
None
117-
}
110+
let mut io = LocalIo::borrow();
111+
match io.get().unix_bind(&path.to_c_str()) {
112+
Ok(s) => Some(UnixListener{ obj: s }),
113+
Err(ioerr) => {
114+
io_error::cond.raise(ioerr);
115+
None
118116
}
119-
})
117+
}
120118
}
121119
}
122120

src/libstd/io/pipe.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use prelude::*;
1717
use super::{Reader, Writer};
1818
use io::{io_error, EndOfFile};
1919
use io::native::file;
20-
use rt::rtio::{RtioPipe, with_local_io};
20+
use rt::rtio::{LocalIo, RtioPipe};
2121

2222
pub struct PipeStream {
2323
priv obj: ~RtioPipe,
@@ -44,15 +44,14 @@ impl PipeStream {
4444
/// If the pipe cannot be created, an error will be raised on the
4545
/// `io_error` condition.
4646
pub fn open(fd: file::fd_t) -> Option<PipeStream> {
47-
with_local_io(|io| {
48-
match io.pipe_open(fd) {
49-
Ok(obj) => Some(PipeStream { obj: obj }),
50-
Err(e) => {
51-
io_error::cond.raise(e);
52-
None
53-
}
47+
let mut io = LocalIo::borrow();
48+
match io.get().pipe_open(fd) {
49+
Ok(obj) => Some(PipeStream { obj: obj }),
50+
Err(e) => {
51+
io_error::cond.raise(e);
52+
None
5453
}
55-
})
54+
}
5655
}
5756

5857
pub fn new(inner: ~RtioPipe) -> PipeStream {

src/libstd/io/process.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
//! Bindings for executing child processes
1212
1313
use prelude::*;
14-
use cell::Cell;
1514

1615
use libc;
1716
use io;
1817
use io::io_error;
19-
use rt::rtio::{RtioProcess, IoFactory, with_local_io};
18+
use rt::rtio::{RtioProcess, IoFactory, LocalIo};
2019

2120
use fmt;
2221

@@ -120,21 +119,19 @@ impl Process {
120119
/// Creates a new pipe initialized, but not bound to any particular
121120
/// source/destination
122121
pub fn new(config: ProcessConfig) -> Option<Process> {
123-
let config = Cell::new(config);
124-
with_local_io(|io| {
125-
match io.spawn(config.take()) {
126-
Ok((p, io)) => Some(Process{
127-
handle: p,
128-
io: io.move_iter().map(|p|
129-
p.map(|p| io::PipeStream::new(p))
130-
).collect()
131-
}),
132-
Err(ioerr) => {
133-
io_error::cond.raise(ioerr);
134-
None
135-
}
122+
let mut io = LocalIo::borrow();
123+
match io.get().spawn(config) {
124+
Ok((p, io)) => Some(Process{
125+
handle: p,
126+
io: io.move_iter().map(|p|
127+
p.map(|p| io::PipeStream::new(p))
128+
).collect()
129+
}),
130+
Err(ioerr) => {
131+
io_error::cond.raise(ioerr);
132+
None
136133
}
137-
})
134+
}
138135
}
139136

140137
/// Returns the process id of this child process

src/libstd/io/signal.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ use comm::{Port, SharedChan, stream};
2424
use container::{Map, MutableMap};
2525
use hashmap;
2626
use io::io_error;
27-
use option::{Some, None};
2827
use result::{Err, Ok};
29-
use rt::rtio::{IoFactory, RtioSignal, with_local_io};
28+
use rt::rtio::{IoFactory, LocalIo, RtioSignal};
3029

3130
#[repr(int)]
3231
#[deriving(Eq, IterBytes)]
@@ -123,18 +122,17 @@ impl Listener {
123122
if self.handles.contains_key(&signum) {
124123
return true; // self is already listening to signum, so succeed
125124
}
126-
with_local_io(|io| {
127-
match io.signal(signum, self.chan.clone()) {
128-
Ok(w) => {
129-
self.handles.insert(signum, w);
130-
Some(())
131-
},
132-
Err(ioerr) => {
133-
io_error::cond.raise(ioerr);
134-
None
135-
}
125+
let mut io = LocalIo::borrow();
126+
match io.get().signal(signum, self.chan.clone()) {
127+
Ok(w) => {
128+
self.handles.insert(signum, w);
129+
true
130+
},
131+
Err(ioerr) => {
132+
io_error::cond.raise(ioerr);
133+
false
136134
}
137-
}).is_some()
135+
}
138136
}
139137

140138
/// Unregisters a signal. If this listener currently had a handler

0 commit comments

Comments
 (0)