about summary refs log tree commit diff
path: root/src/libstd/io/stdio.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2013-12-12 17:30:41 -0800
committerAlex Crichton <alex@alexcrichton.com>2013-12-24 14:42:00 -0800
commit4538369566b8b51fc8371253aa90f9725547a193 (patch)
treedd3390d2083e2a6d5c494dec6760bdf4590a8b86 /src/libstd/io/stdio.rs
parenta55c57284d8341ee5b22c5372e77ac0af9479dc5 (diff)
downloadrust-4538369566b8b51fc8371253aa90f9725547a193.tar.gz
rust-4538369566b8b51fc8371253aa90f9725547a193.zip
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.
Diffstat (limited to 'src/libstd/io/stdio.rs')
-rw-r--r--src/libstd/io/stdio.rs26
1 files changed, 10 insertions, 16 deletions
diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs
index 41337075aa9..0adb83d2015 100644
--- a/src/libstd/io/stdio.rs
+++ b/src/libstd/io/stdio.rs
@@ -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.