1
Fork 0

std: Expose that LocalIo may not always be available

It is not the case that all programs will always be able to acquire an instance
of the LocalIo borrow, so this commit exposes this limitation by returning
Option<LocalIo> from LocalIo::borrow().

At the same time, a helper method LocalIo::maybe_raise() has been added in order
to encapsulate the functionality of raising on io_error if there is on local I/O
available.
This commit is contained in:
Alex Crichton 2013-12-12 17:30:41 -08:00
parent a55c57284d
commit 4538369566
11 changed files with 118 additions and 176 deletions

View file

@ -27,13 +27,14 @@ out.write(bytes!("Hello, world!"));
*/
use fmt;
use io::buffered::LineBufferedWriter;
use io::{Reader, Writer, io_error, IoError, OtherIoError,
standard_error, EndOfFile};
use libc;
use option::{Option, Some, None};
use result::{Ok, Err};
use io::buffered::LineBufferedWriter;
use rt::rtio::{DontClose, IoFactory, LocalIo, RtioFileStream, RtioTTY};
use super::{Reader, Writer, io_error, IoError, OtherIoError,
standard_error, EndOfFile};
use vec;
// And so begins the tale of acquiring a uv handle to a stdio stream on all
// platforms in all situations. Our story begins by splitting the world into two
@ -69,19 +70,12 @@ enum StdSource {
}
fn src<T>(fd: libc::c_int, readable: bool, f: |StdSource| -> T) -> T {
let mut io = LocalIo::borrow();
match io.get().tty_open(fd, readable) {
Ok(tty) => f(TTY(tty)),
Err(_) => {
// It's not really that desirable if these handles are closed
// synchronously, and because they're squirreled away in a task
// structure the destructors will be run when the task is
// attempted to get destroyed. This means that if we run a
// synchronous destructor we'll attempt to do some scheduling
// operations which will just result in sadness.
f(File(io.get().fs_from_raw_fd(fd, DontClose)))
}
}
LocalIo::maybe_raise(|io| {
Ok(match io.tty_open(fd, readable) {
Ok(tty) => f(TTY(tty)),
Err(_) => f(File(io.fs_from_raw_fd(fd, DontClose))),
})
}).unwrap()
}
/// Creates a new non-blocking handle to the stdin of the current process.