Skip to content

Commit b46f60a

Browse files
committed
Remove IoFactoryObject for ~IoFactory
This involved changing a fair amount of code, rooted in how we access the local IoFactory instance. I added a helper method to the rtio module to access the optional local IoFactory. This is different than before in which it was assumed that a local IoFactory was *always* present. Now, a separate io_error is raised when an IoFactory is not present, yet I/O is requested.
1 parent 9110a38 commit b46f60a

File tree

17 files changed

+303
-315
lines changed

17 files changed

+303
-315
lines changed

src/libstd/c_str.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ mod tests {
382382
use libc;
383383
use ptr;
384384
use option::{Some, None};
385+
use vec;
385386

386387
#[test]
387388
fn test_str_multistring_parsing() {
@@ -391,7 +392,8 @@ mod tests {
391392
let expected = ["zero", "one"];
392393
let mut it = expected.iter();
393394
let result = do from_c_multistring(ptr as *libc::c_char, None) |c| {
394-
assert_eq!(c.as_str(), expected.next().unwrap());
395+
let cbytes = c.as_bytes().slice_to(c.len());
396+
assert_eq!(cbytes, it.next().unwrap().as_bytes());
395397
};
396398
assert_eq!(result, 2);
397399
assert!(it.next().is_none());

src/libstd/rt/io/file.rs

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ use prelude::*;
3434
use c_str::ToCStr;
3535
use super::{Reader, Writer, Seek};
3636
use super::{SeekStyle, Read, Write};
37-
use rt::rtio::{RtioFileStream, IoFactory, IoFactoryObject};
37+
use rt::rtio::{RtioFileStream, IoFactory, with_local_io};
3838
use rt::io::{io_error, read_error, EndOfFile,
3939
FileMode, FileAccess, FileStat, IoError,
4040
PathAlreadyExists, PathDoesntExist,
4141
MismatchedFileTypeForOperation, ignore_io_error};
42-
use rt::local::Local;
4342
use option::{Some, None};
4443
use path::Path;
4544

@@ -90,19 +89,17 @@ use path::Path;
9089
pub fn open<P: ToCStr>(path: &P,
9190
mode: FileMode,
9291
access: FileAccess
93-
) -> Option<FileStream> {
94-
let open_result = unsafe {
95-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
96-
(*io).fs_open(&path.to_c_str(), mode, access)
97-
};
98-
match open_result {
99-
Ok(fd) => Some(FileStream {
100-
fd: fd,
101-
last_nread: -1
102-
}),
103-
Err(ioerr) => {
104-
io_error::cond.raise(ioerr);
105-
None
92+
) -> Option<FileStream> {
93+
do with_local_io |io| {
94+
match io.fs_open(&path.to_c_str(), mode, access) {
95+
Ok(fd) => Some(FileStream {
96+
fd: fd,
97+
last_nread: -1
98+
}),
99+
Err(ioerr) => {
100+
io_error::cond.raise(ioerr);
101+
None
102+
}
106103
}
107104
}
108105
}
@@ -129,16 +126,15 @@ pub fn open<P: ToCStr>(path: &P,
129126
/// This function will raise an `io_error` condition if the user lacks permissions to
130127
/// remove the file or if some other filesystem-level error occurs
131128
pub fn unlink<P: ToCStr>(path: &P) {
132-
let unlink_result = unsafe {
133-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
134-
(*io).fs_unlink(&path.to_c_str())
135-
};
136-
match unlink_result {
137-
Ok(_) => (),
138-
Err(ioerr) => {
139-
io_error::cond.raise(ioerr);
129+
do with_local_io |io| {
130+
match io.fs_unlink(&path.to_c_str()) {
131+
Ok(_) => Some(()),
132+
Err(ioerr) => {
133+
io_error::cond.raise(ioerr);
134+
None
135+
}
140136
}
141-
}
137+
};
142138
}
143139

144140
/// Create a new, empty directory at the provided path
@@ -158,16 +154,15 @@ pub fn unlink<P: ToCStr>(path: &P) {
158154
/// This call will raise an `io_error` condition if the user lacks permissions to make a
159155
/// new directory at the provided path, or if the directory already exists
160156
pub fn mkdir<P: ToCStr>(path: &P) {
161-
let mkdir_result = unsafe {
162-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
163-
(*io).fs_mkdir(&path.to_c_str())
164-
};
165-
match mkdir_result {
166-
Ok(_) => (),
167-
Err(ioerr) => {
168-
io_error::cond.raise(ioerr);
157+
do with_local_io |io| {
158+
match io.fs_mkdir(&path.to_c_str()) {
159+
Ok(_) => Some(()),
160+
Err(ioerr) => {
161+
io_error::cond.raise(ioerr);
162+
None
163+
}
169164
}
170-
}
165+
};
171166
}
172167

173168
/// Remove an existing, empty directory
@@ -187,16 +182,15 @@ pub fn mkdir<P: ToCStr>(path: &P) {
187182
/// This call will raise an `io_error` condition if the user lacks permissions to remove the
188183
/// directory at the provided path, or if the directory isn't empty
189184
pub fn rmdir<P: ToCStr>(path: &P) {
190-
let rmdir_result = unsafe {
191-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
192-
(*io).fs_rmdir(&path.to_c_str())
193-
};
194-
match rmdir_result {
195-
Ok(_) => (),
196-
Err(ioerr) => {
197-
io_error::cond.raise(ioerr);
185+
do with_local_io |io| {
186+
match io.fs_rmdir(&path.to_c_str()) {
187+
Ok(_) => Some(()),
188+
Err(ioerr) => {
189+
io_error::cond.raise(ioerr);
190+
None
191+
}
198192
}
199-
}
193+
};
200194
}
201195

202196
/// Get information on the file, directory, etc at the provided path
@@ -235,17 +229,13 @@ pub fn rmdir<P: ToCStr>(path: &P) {
235229
/// permissions to perform a `stat` call on the given path or if there is no
236230
/// entry in the filesystem at the provided path.
237231
pub fn stat<P: ToCStr>(path: &P) -> Option<FileStat> {
238-
let open_result = unsafe {
239-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
240-
(*io).fs_stat(&path.to_c_str())
241-
};
242-
match open_result {
243-
Ok(p) => {
244-
Some(p)
245-
},
246-
Err(ioerr) => {
247-
io_error::cond.raise(ioerr);
248-
None
232+
do with_local_io |io| {
233+
match io.fs_stat(&path.to_c_str()) {
234+
Ok(p) => Some(p),
235+
Err(ioerr) => {
236+
io_error::cond.raise(ioerr);
237+
None
238+
}
249239
}
250240
}
251241
}
@@ -275,17 +265,13 @@ pub fn stat<P: ToCStr>(path: &P) -> Option<FileStat> {
275265
/// the process lacks permissions to view the contents or if the `path` points
276266
/// at a non-directory file
277267
pub fn readdir<P: ToCStr>(path: &P) -> Option<~[Path]> {
278-
let readdir_result = unsafe {
279-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
280-
(*io).fs_readdir(&path.to_c_str(), 0)
281-
};
282-
match readdir_result {
283-
Ok(p) => {
284-
Some(p)
285-
},
286-
Err(ioerr) => {
287-
io_error::cond.raise(ioerr);
288-
None
268+
do with_local_io |io| {
269+
match io.fs_readdir(&path.to_c_str(), 0) {
270+
Ok(p) => Some(p),
271+
Err(ioerr) => {
272+
io_error::cond.raise(ioerr);
273+
None
274+
}
289275
}
290276
}
291277
}

src/libstd/rt/io/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ pub enum IoErrorKind {
369369
BrokenPipe,
370370
PathAlreadyExists,
371371
PathDoesntExist,
372-
MismatchedFileTypeForOperation
372+
MismatchedFileTypeForOperation,
373+
IoUnavailable,
373374
}
374375

375376
// FIXME: #8242 implementing manually because deriving doesn't work for some reason
@@ -389,7 +390,8 @@ impl ToStr for IoErrorKind {
389390
BrokenPipe => ~"BrokenPipe",
390391
PathAlreadyExists => ~"PathAlreadyExists",
391392
PathDoesntExist => ~"PathDoesntExist",
392-
MismatchedFileTypeForOperation => ~"MismatchedFileTypeForOperation"
393+
MismatchedFileTypeForOperation => ~"MismatchedFileTypeForOperation",
394+
IoUnavailable => ~"IoUnavailable",
393395
}
394396
}
395397
}

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ getaddrinfo()
1919

2020
use option::{Option, Some, None};
2121
use result::{Ok, Err};
22-
use rt::io::io_error;
22+
use rt::io::{io_error};
2323
use rt::io::net::ip::{SocketAddr, IpAddr};
24-
use rt::rtio::{IoFactory, IoFactoryObject};
25-
use rt::local::Local;
24+
use rt::rtio::{IoFactory, with_local_io};
2625

2726
/// Hints to the types of sockets that are desired when looking up hosts
2827
pub enum SocketType {
@@ -94,16 +93,13 @@ pub fn get_host_addresses(host: &str) -> Option<~[IpAddr]> {
9493
/// On failure, this will raise on the `io_error` condition.
9594
pub fn lookup(hostname: Option<&str>, servname: Option<&str>,
9695
hint: Option<Hint>) -> Option<~[Info]> {
97-
let ipaddrs = unsafe {
98-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
99-
(*io).get_host_addresses(hostname, servname, hint)
100-
};
101-
102-
match ipaddrs {
103-
Ok(i) => Some(i),
104-
Err(ioerr) => {
105-
io_error::cond.raise(ioerr);
106-
None
96+
do with_local_io |io| {
97+
match io.get_host_addresses(hostname, servname, hint) {
98+
Ok(i) => Some(i),
99+
Err(ioerr) => {
100+
io_error::cond.raise(ioerr);
101+
None
102+
}
107103
}
108104
}
109105
}

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use result::{Ok, Err};
1313
use rt::io::net::ip::SocketAddr;
1414
use rt::io::{Reader, Writer, Listener, Acceptor};
1515
use rt::io::{io_error, read_error, EndOfFile};
16-
use rt::rtio::{IoFactory, IoFactoryObject, RtioTcpListenerObject,
16+
use rt::rtio::{IoFactory, RtioTcpListenerObject, with_local_io,
1717
RtioSocket, RtioTcpListener, RtioTcpAcceptor, RtioTcpStream};
18-
use rt::local::Local;
1918

2019
pub struct TcpStream {
2120
priv obj: ~RtioTcpStream
@@ -27,19 +26,13 @@ impl TcpStream {
2726
}
2827

2928
pub fn connect(addr: SocketAddr) -> Option<TcpStream> {
30-
let stream = unsafe {
31-
rtdebug!("borrowing io to connect");
32-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
33-
rtdebug!("about to connect");
34-
(*io).tcp_connect(addr)
35-
};
36-
37-
match stream {
38-
Ok(s) => Some(TcpStream::new(s)),
39-
Err(ioerr) => {
40-
rtdebug!("failed to connect: {:?}", ioerr);
41-
io_error::cond.raise(ioerr);
42-
None
29+
do 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+
}
4336
}
4437
}
4538
}
@@ -101,15 +94,13 @@ pub struct TcpListener {
10194

10295
impl TcpListener {
10396
pub fn bind(addr: SocketAddr) -> Option<TcpListener> {
104-
let listener = unsafe {
105-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
106-
(*io).tcp_bind(addr)
107-
};
108-
match listener {
109-
Ok(l) => Some(TcpListener { obj: l }),
110-
Err(ioerr) => {
111-
io_error::cond.raise(ioerr);
112-
return None;
97+
do with_local_io |io| {
98+
match io.tcp_bind(addr) {
99+
Ok(l) => Some(TcpListener { obj: l }),
100+
Err(ioerr) => {
101+
io_error::cond.raise(ioerr);
102+
None
103+
}
113104
}
114105
}
115106
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,21 @@ use result::{Ok, Err};
1313
use rt::io::net::ip::SocketAddr;
1414
use rt::io::{Reader, Writer};
1515
use rt::io::{io_error, read_error, EndOfFile};
16-
use rt::rtio::{RtioSocket, RtioUdpSocket, IoFactory, IoFactoryObject};
17-
use rt::local::Local;
16+
use rt::rtio::{RtioSocket, RtioUdpSocket, IoFactory, with_local_io};
1817

1918
pub struct UdpSocket {
2019
priv obj: ~RtioUdpSocket
2120
}
2221

2322
impl UdpSocket {
2423
pub fn bind(addr: SocketAddr) -> Option<UdpSocket> {
25-
let socket = unsafe {
26-
let factory: *mut IoFactoryObject = Local::unsafe_borrow();
27-
(*factory).udp_bind(addr)
28-
};
29-
match socket {
30-
Ok(s) => Some(UdpSocket { obj: s }),
31-
Err(ioerr) => {
32-
io_error::cond.raise(ioerr);
33-
None
24+
do 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+
}
3431
}
3532
}
3633
}

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

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

2727
use c_str::ToCStr;
28-
use rt::rtio::{IoFactory, IoFactoryObject, RtioUnixListener};
28+
use rt::rtio::{IoFactory, RtioUnixListener, with_local_io};
2929
use rt::rtio::{RtioUnixAcceptor, RtioPipe, RtioUnixListenerObject};
3030
use rt::io::pipe::PipeStream;
3131
use rt::io::{io_error, Listener, Acceptor, Reader, Writer};
32-
use rt::local::Local;
3332

3433
/// A stream which communicates over a named pipe.
3534
pub struct UnixStream {
@@ -60,16 +59,13 @@ impl UnixStream {
6059
/// stream.write([1, 2, 3]);
6160
///
6261
pub fn connect<P: ToCStr>(path: &P) -> Option<UnixStream> {
63-
let pipe = unsafe {
64-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
65-
(*io).unix_connect(path)
66-
};
67-
68-
match pipe {
69-
Ok(s) => Some(UnixStream::new(s)),
70-
Err(ioerr) => {
71-
io_error::cond.raise(ioerr);
72-
None
62+
do 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+
}
7369
}
7470
}
7571
}
@@ -113,15 +109,13 @@ impl UnixListener {
113109
/// }
114110
///
115111
pub fn bind<P: ToCStr>(path: &P) -> Option<UnixListener> {
116-
let listener = unsafe {
117-
let io: *mut IoFactoryObject = Local::unsafe_borrow();
118-
(*io).unix_bind(path)
119-
};
120-
match listener {
121-
Ok(s) => Some(UnixListener{ obj: s }),
122-
Err(ioerr) => {
123-
io_error::cond.raise(ioerr);
124-
None
112+
do with_local_io |io| {
113+
match io.unix_bind(&path.to_c_str()) {
114+
Ok(s) => Some(UnixListener{ obj: s }),
115+
Err(ioerr) => {
116+
io_error::cond.raise(ioerr);
117+
None
118+
}
125119
}
126120
}
127121
}

0 commit comments

Comments
 (0)