summary refs log tree commit diff
path: root/src/libstd/io/stdio.rs
diff options
context:
space:
mode:
authorFlavio Percoco <flaper87@gmail.com>2014-12-24 17:40:40 +0100
committerFlavio Percoco <flaper87@gmail.com>2014-12-26 17:26:33 +0100
commitbb315f25f84c93b69d99d41b2c68185639c30e83 (patch)
tree7d5ad491eeadcbb8a707126cebdd285a57ec7dc9 /src/libstd/io/stdio.rs
parent52072dec0f6838ae251259a8f0574776c2028a1b (diff)
downloadrust-bb315f25f84c93b69d99d41b2c68185639c30e83.tar.gz
rust-bb315f25f84c93b69d99d41b2c68185639c30e83.zip
Implement RaceBox for StdinReader
Diffstat (limited to 'src/libstd/io/stdio.rs')
-rw-r--r--src/libstd/io/stdio.rs41
1 files changed, 26 insertions, 15 deletions
diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs
index 1c5ceaf2450..b7da57fed27 100644
--- a/src/libstd/io/stdio.rs
+++ b/src/libstd/io/stdio.rs
@@ -34,7 +34,7 @@ use failure::LOCAL_STDERR;
 use fmt;
 use io::{Reader, Writer, IoResult, IoError, OtherIoError, Buffer,
          standard_error, EndOfFile, LineBufferedWriter, BufferedReader};
-use kinds::Send;
+use kinds::{Sync, Send};
 use libc;
 use mem;
 use option::Option;
@@ -98,26 +98,34 @@ thread_local! {
     }
 }
 
+struct RaceBox(BufferedReader<StdReader>);
+
+unsafe impl Send for RaceBox {}
+unsafe impl Sync for RaceBox {}
+
 /// A synchronized wrapper around a buffered reader from stdin
 #[deriving(Clone)]
 pub struct StdinReader {
-    inner: Arc<Mutex<BufferedReader<StdReader>>>,
+    inner: Arc<Mutex<RaceBox>>,
 }
 
+unsafe impl Send for StdinReader {}
+unsafe impl Sync for StdinReader {}
+
 /// A guard for exclusive access to `StdinReader`'s internal `BufferedReader`.
 pub struct StdinReaderGuard<'a> {
-    inner: MutexGuard<'a, BufferedReader<StdReader>>,
+    inner: MutexGuard<'a, RaceBox>,
 }
 
 impl<'a> Deref<BufferedReader<StdReader>> for StdinReaderGuard<'a> {
     fn deref(&self) -> &BufferedReader<StdReader> {
-        &*self.inner
+        &self.inner.0
     }
 }
 
 impl<'a> DerefMut<BufferedReader<StdReader>> for StdinReaderGuard<'a> {
     fn deref_mut(&mut self) -> &mut BufferedReader<StdReader> {
-        &mut *self.inner
+        &mut self.inner.0
     }
 }
 
@@ -147,7 +155,7 @@ impl StdinReader {
     /// The read is performed atomically - concurrent read calls in other
     /// threads will not interleave with this one.
     pub fn read_line(&mut self) -> IoResult<String> {
-        self.inner.lock().read_line()
+        self.inner.lock().0.read_line()
     }
 
     /// Like `Buffer::read_until`.
@@ -155,7 +163,7 @@ impl StdinReader {
     /// The read is performed atomically - concurrent read calls in other
     /// threads will not interleave with this one.
     pub fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>> {
-        self.inner.lock().read_until(byte)
+        self.inner.lock().0.read_until(byte)
     }
 
     /// Like `Buffer::read_char`.
@@ -163,13 +171,13 @@ impl StdinReader {
     /// The read is performed atomically - concurrent read calls in other
     /// threads will not interleave with this one.
     pub fn read_char(&mut self) -> IoResult<char> {
-        self.inner.lock().read_char()
+        self.inner.lock().0.read_char()
     }
 }
 
 impl Reader for StdinReader {
     fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
-        self.inner.lock().read(buf)
+        self.inner.lock().0.read(buf)
     }
 
     // We have to manually delegate all of these because the default impls call
@@ -177,23 +185,23 @@ impl Reader for StdinReader {
     // incur the costs of repeated locking).
 
     fn read_at_least(&mut self, min: uint, buf: &mut [u8]) -> IoResult<uint> {
-        self.inner.lock().read_at_least(min, buf)
+        self.inner.lock().0.read_at_least(min, buf)
     }
 
     fn push_at_least(&mut self, min: uint, len: uint, buf: &mut Vec<u8>) -> IoResult<uint> {
-        self.inner.lock().push_at_least(min, len, buf)
+        self.inner.lock().0.push_at_least(min, len, buf)
     }
 
     fn read_to_end(&mut self) -> IoResult<Vec<u8>> {
-        self.inner.lock().read_to_end()
+        self.inner.lock().0.read_to_end()
     }
 
     fn read_le_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
-        self.inner.lock().read_le_uint_n(nbytes)
+        self.inner.lock().0.read_le_uint_n(nbytes)
     }
 
     fn read_be_uint_n(&mut self, nbytes: uint) -> IoResult<u64> {
-        self.inner.lock().read_be_uint_n(nbytes)
+        self.inner.lock().0.read_be_uint_n(nbytes)
     }
 }
 
@@ -221,7 +229,7 @@ pub fn stdin() -> StdinReader {
                 BufferedReader::new(stdin_raw())
             };
             let stdin = StdinReader {
-                inner: Arc::new(Mutex::new(stdin))
+                inner: Arc::new(Mutex::new(RaceBox(stdin)))
             };
             STDIN = mem::transmute(box stdin);
 
@@ -426,6 +434,9 @@ pub struct StdWriter {
     inner: StdSource
 }
 
+unsafe impl Send for StdWriter {}
+unsafe impl Sync for StdWriter {}
+
 impl StdWriter {
     /// Gets the size of this output window, if possible. This is typically used
     /// when the writer is attached to something like a terminal, this is used