From 377b0900aede976b2d37a499bbd7b62c2e39b358 Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Wed, 27 May 2015 11:18:36 +0300 Subject: Use `const fn` to abstract away the contents of UnsafeCell & friends. --- src/libcore/atomic.rs | 47 ++++++++++++++++++++--------------------------- src/libcore/cell.rs | 6 +++--- src/libcore/lib.rs | 1 + 3 files changed, 24 insertions(+), 30 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/atomic.rs b/src/libcore/atomic.rs index ec693f36691..56b459f5f17 100644 --- a/src/libcore/atomic.rs +++ b/src/libcore/atomic.rs @@ -76,7 +76,6 @@ use marker::Sync; use intrinsics; use cell::UnsafeCell; -use marker::PhantomData; use default::Default; @@ -87,8 +86,8 @@ pub struct AtomicBool { } impl Default for AtomicBool { - fn default() -> AtomicBool { - ATOMIC_BOOL_INIT + fn default() -> Self { + Self::new(Default::default()) } } @@ -101,8 +100,8 @@ pub struct AtomicIsize { } impl Default for AtomicIsize { - fn default() -> AtomicIsize { - ATOMIC_ISIZE_INIT + fn default() -> Self { + Self::new(Default::default()) } } @@ -115,8 +114,8 @@ pub struct AtomicUsize { } impl Default for AtomicUsize { - fn default() -> AtomicUsize { - ATOMIC_USIZE_INIT + fn default() -> Self { + Self::new(Default::default()) } } @@ -125,8 +124,7 @@ unsafe impl Sync for AtomicUsize {} /// A raw pointer type which can be safely shared between threads. #[stable(feature = "rust1", since = "1.0.0")] pub struct AtomicPtr { - p: UnsafeCell, - _marker: PhantomData<*mut T>, + p: UnsafeCell<*mut T>, } impl Default for AtomicPtr { @@ -175,16 +173,13 @@ pub enum Ordering { /// An `AtomicBool` initialized to `false`. #[stable(feature = "rust1", since = "1.0.0")] -pub const ATOMIC_BOOL_INIT: AtomicBool = - AtomicBool { v: UnsafeCell { value: 0 } }; +pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false); /// An `AtomicIsize` initialized to `0`. #[stable(feature = "rust1", since = "1.0.0")] -pub const ATOMIC_ISIZE_INIT: AtomicIsize = - AtomicIsize { v: UnsafeCell { value: 0 } }; +pub const ATOMIC_ISIZE_INIT: AtomicIsize = AtomicIsize::new(0); /// An `AtomicUsize` initialized to `0`. #[stable(feature = "rust1", since = "1.0.0")] -pub const ATOMIC_USIZE_INIT: AtomicUsize = - AtomicUsize { v: UnsafeCell { value: 0, } }; +pub const ATOMIC_USIZE_INIT: AtomicUsize = AtomicUsize::new(0); // NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly const UINT_TRUE: usize = !0; @@ -202,9 +197,8 @@ impl AtomicBool { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(v: bool) -> AtomicBool { - let val = if v { UINT_TRUE } else { 0 }; - AtomicBool { v: UnsafeCell::new(val) } + pub const fn new(v: bool) -> AtomicBool { + AtomicBool { v: UnsafeCell::new(-(v as isize) as usize) } } /// Loads a value from the bool. @@ -445,7 +439,7 @@ impl AtomicIsize { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(v: isize) -> AtomicIsize { + pub const fn new(v: isize) -> AtomicIsize { AtomicIsize {v: UnsafeCell::new(v)} } @@ -633,7 +627,7 @@ impl AtomicUsize { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(v: usize) -> AtomicUsize { + pub const fn new(v: usize) -> AtomicUsize { AtomicUsize { v: UnsafeCell::new(v) } } @@ -821,9 +815,8 @@ impl AtomicPtr { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(p: *mut T) -> AtomicPtr { - AtomicPtr { p: UnsafeCell::new(p as usize), - _marker: PhantomData } + pub const fn new(p: *mut T) -> AtomicPtr { + AtomicPtr { p: UnsafeCell::new(p) } } /// Loads a value from the pointer. @@ -848,7 +841,7 @@ impl AtomicPtr { #[stable(feature = "rust1", since = "1.0.0")] pub fn load(&self, order: Ordering) -> *mut T { unsafe { - atomic_load(self.p.get(), order) as *mut T + atomic_load(self.p.get() as *mut usize, order) as *mut T } } @@ -875,7 +868,7 @@ impl AtomicPtr { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn store(&self, ptr: *mut T, order: Ordering) { - unsafe { atomic_store(self.p.get(), ptr as usize, order); } + unsafe { atomic_store(self.p.get() as *mut usize, ptr as usize, order); } } /// Stores a value into the pointer, returning the old value. @@ -897,7 +890,7 @@ impl AtomicPtr { #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T { - unsafe { atomic_swap(self.p.get(), ptr as usize, order) as *mut T } + unsafe { atomic_swap(self.p.get() as *mut usize, ptr as usize, order) as *mut T } } /// Stores a value into the pointer if the current value is the same as the expected value. @@ -925,7 +918,7 @@ impl AtomicPtr { #[stable(feature = "rust1", since = "1.0.0")] pub fn compare_and_swap(&self, old: *mut T, new: *mut T, order: Ordering) -> *mut T { unsafe { - atomic_compare_and_swap(self.p.get(), old as usize, + atomic_compare_and_swap(self.p.get() as *mut usize, old as usize, new as usize, order) as *mut T } } diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 45a80122104..c83421d3067 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -170,7 +170,7 @@ impl Cell { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn new(value: T) -> Cell { + pub const fn new(value: T) -> Cell { Cell { value: UnsafeCell::new(value), } @@ -302,7 +302,7 @@ impl RefCell { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn new(value: T) -> RefCell { + pub const fn new(value: T) -> RefCell { RefCell { value: UnsafeCell::new(value), borrow: Cell::new(UNUSED), @@ -663,7 +663,7 @@ impl UnsafeCell { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn new(value: T) -> UnsafeCell { + pub const fn new(value: T) -> UnsafeCell { UnsafeCell { value: value } } diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 0794fb0c45d..9dfaec0095a 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -74,6 +74,7 @@ #![feature(concat_idents)] #![feature(reflect)] #![feature(custom_attribute)] +#![feature(const_fn)] #[macro_use] mod macros; -- cgit 1.4.1-3-g733a5