about summary refs log tree commit diff
path: root/src/libstd/io
diff options
context:
space:
mode:
authorEduard Burtescu <edy.burt@gmail.com>2015-05-27 11:18:36 +0300
committerEduard Burtescu <edy.burt@gmail.com>2015-05-27 11:19:03 +0300
commit377b0900aede976b2d37a499bbd7b62c2e39b358 (patch)
treeb4a5a4431d36ed1a4e0a39c7d2ef2563ecac9bf4 /src/libstd/io
parent6e8e4f847c2ea02fec021ea15dfb2de6beac797a (diff)
downloadrust-377b0900aede976b2d37a499bbd7b62c2e39b358.tar.gz
rust-377b0900aede976b2d37a499bbd7b62c2e39b358.zip
Use `const fn` to abstract away the contents of UnsafeCell & friends.
Diffstat (limited to 'src/libstd/io')
-rw-r--r--src/libstd/io/lazy.rs32
-rw-r--r--src/libstd/io/mod.rs3
-rw-r--r--src/libstd/io/stdio.rs6
3 files changed, 20 insertions, 21 deletions
diff --git a/src/libstd/io/lazy.rs b/src/libstd/io/lazy.rs
index df280dab37d..d398cb88af4 100644
--- a/src/libstd/io/lazy.rs
+++ b/src/libstd/io/lazy.rs
@@ -11,31 +11,31 @@
 use prelude::v1::*;
 
 use boxed;
-use cell::UnsafeCell;
+use cell::Cell;
 use rt;
 use sync::{StaticMutex, Arc};
 
 pub struct Lazy<T> {
-    pub lock: StaticMutex,
-    pub ptr: UnsafeCell<*mut Arc<T>>,
-    pub init: fn() -> Arc<T>,
+    lock: StaticMutex,
+    ptr: Cell<*mut Arc<T>>,
+    init: fn() -> Arc<T>,
 }
 
 unsafe impl<T> Sync for Lazy<T> {}
 
-macro_rules! lazy_init {
-    ($init:expr) => (::io::lazy::Lazy {
-        lock: ::sync::MUTEX_INIT,
-        ptr: ::cell::UnsafeCell { value: 0 as *mut _ },
-        init: $init,
-    })
-}
-
 impl<T: Send + Sync + 'static> Lazy<T> {
+    pub const fn new(init: fn() -> Arc<T>) -> Lazy<T> {
+        Lazy {
+            lock: StaticMutex::new(),
+            ptr: Cell::new(0 as *mut _),
+            init: init
+        }
+    }
+
     pub fn get(&'static self) -> Option<Arc<T>> {
         let _g = self.lock.lock();
+        let ptr = self.ptr.get();
         unsafe {
-            let ptr = *self.ptr.get();
             if ptr.is_null() {
                 Some(self.init())
             } else if ptr as usize == 1 {
@@ -53,14 +53,14 @@ impl<T: Send + Sync + 'static> Lazy<T> {
         // `Arc`.
         let registered = rt::at_exit(move || {
             let g = self.lock.lock();
-            let ptr = *self.ptr.get();
-            *self.ptr.get() = 1 as *mut _;
+            let ptr = self.ptr.get();
+            self.ptr.set(1 as *mut _);
             drop(g);
             drop(Box::from_raw(ptr))
         });
         let ret = (self.init)();
         if registered.is_ok() {
-            *self.ptr.get() = boxed::into_raw(Box::new(ret.clone()));
+            self.ptr.set(boxed::into_raw(Box::new(ret.clone())));
         }
         return ret
     }
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs
index c9da279810b..c664def304e 100644
--- a/src/libstd/io/mod.rs
+++ b/src/libstd/io/mod.rs
@@ -36,13 +36,12 @@ pub use self::stdio::{StdoutLock, StderrLock, StdinLock};
 #[doc(no_inline, hidden)]
 pub use self::stdio::{set_panic, set_print};
 
-#[macro_use] mod lazy;
-
 pub mod prelude;
 mod buffered;
 mod cursor;
 mod error;
 mod impls;
+mod lazy;
 mod util;
 mod stdio;
 
diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs
index a14c472333c..9885ccfaae0 100644
--- a/src/libstd/io/stdio.rs
+++ b/src/libstd/io/stdio.rs
@@ -122,7 +122,7 @@ pub struct StdinLock<'a> {
 /// locked version, `StdinLock`, implements both `Read` and `BufRead`, however.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub fn stdin() -> Stdin {
-    static INSTANCE: Lazy<Mutex<BufReader<StdinRaw>>> = lazy_init!(stdin_init);
+    static INSTANCE: Lazy<Mutex<BufReader<StdinRaw>>> = Lazy::new(stdin_init);
     return Stdin {
         inner: INSTANCE.get().expect("cannot access stdin during shutdown"),
     };
@@ -236,7 +236,7 @@ pub struct StdoutLock<'a> {
 /// The returned handle implements the `Write` trait.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub fn stdout() -> Stdout {
-    static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = lazy_init!(stdout_init);
+    static INSTANCE: Lazy<ReentrantMutex<RefCell<LineWriter<StdoutRaw>>>> = Lazy::new(stdout_init);
     return Stdout {
         inner: INSTANCE.get().expect("cannot access stdout during shutdown"),
     };
@@ -308,7 +308,7 @@ pub struct StderrLock<'a> {
 /// The returned handle implements the `Write` trait.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub fn stderr() -> Stderr {
-    static INSTANCE: Lazy<ReentrantMutex<RefCell<StderrRaw>>> = lazy_init!(stderr_init);
+    static INSTANCE: Lazy<ReentrantMutex<RefCell<StderrRaw>>> = Lazy::new(stderr_init);
     return Stderr {
         inner: INSTANCE.get().expect("cannot access stderr during shutdown"),
     };