diff options
| author | Eduard Burtescu <edy.burt@gmail.com> | 2015-05-27 11:18:36 +0300 |
|---|---|---|
| committer | Eduard Burtescu <edy.burt@gmail.com> | 2015-05-27 11:19:03 +0300 |
| commit | 377b0900aede976b2d37a499bbd7b62c2e39b358 (patch) | |
| tree | b4a5a4431d36ed1a4e0a39c7d2ef2563ecac9bf4 /src/libcore | |
| parent | 6e8e4f847c2ea02fec021ea15dfb2de6beac797a (diff) | |
| download | rust-377b0900aede976b2d37a499bbd7b62c2e39b358.tar.gz rust-377b0900aede976b2d37a499bbd7b62c2e39b358.zip | |
Use `const fn` to abstract away the contents of UnsafeCell & friends.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/atomic.rs | 47 | ||||
| -rw-r--r-- | src/libcore/cell.rs | 6 | ||||
| -rw-r--r-- | src/libcore/lib.rs | 1 |
3 files changed, 24 insertions, 30 deletions
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<T> { - p: UnsafeCell<usize>, - _marker: PhantomData<*mut T>, + p: UnsafeCell<*mut T>, } impl<T> Default for AtomicPtr<T> { @@ -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<T> AtomicPtr<T> { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] - pub fn new(p: *mut T) -> AtomicPtr<T> { - AtomicPtr { p: UnsafeCell::new(p as usize), - _marker: PhantomData } + pub const fn new(p: *mut T) -> AtomicPtr<T> { + AtomicPtr { p: UnsafeCell::new(p) } } /// Loads a value from the pointer. @@ -848,7 +841,7 @@ impl<T> AtomicPtr<T> { #[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<T> AtomicPtr<T> { #[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<T> AtomicPtr<T> { #[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<T> AtomicPtr<T> { #[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<T:Copy> Cell<T> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn new(value: T) -> Cell<T> { + pub const fn new(value: T) -> Cell<T> { Cell { value: UnsafeCell::new(value), } @@ -302,7 +302,7 @@ impl<T> RefCell<T> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn new(value: T) -> RefCell<T> { + pub const fn new(value: T) -> RefCell<T> { RefCell { value: UnsafeCell::new(value), borrow: Cell::new(UNUSED), @@ -663,7 +663,7 @@ impl<T> UnsafeCell<T> { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn new(value: T) -> UnsafeCell<T> { + pub const fn new(value: T) -> UnsafeCell<T> { 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; |
