Skip to content

Commit a085f4a

Browse files
committed
Use const fn to abstract away the contents of UnsafeCell & friends.
1 parent f7d9b3e commit a085f4a

74 files changed

Lines changed: 409 additions & 519 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/libcollectionstest/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ fn test_map_in_place_zero_sized() {
399399

400400
#[test]
401401
fn test_map_in_place_zero_drop_count() {
402-
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
402+
use std::sync::atomic::{AtomicUsize, Ordering};
403403

404404
#[derive(Clone, PartialEq, Debug)]
405405
struct Nothing;
@@ -413,7 +413,7 @@ fn test_map_in_place_zero_drop_count() {
413413
}
414414
}
415415
const NUM_ELEMENTS: usize = 2;
416-
static DROP_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
416+
static DROP_COUNTER: AtomicUsize = AtomicUsize::new(0);
417417

418418
let v = repeat(Nothing).take(NUM_ELEMENTS).collect::<Vec<_>>();
419419

src/libcore/atomic.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ pub struct AtomicBool {
8787
}
8888

8989
impl Default for AtomicBool {
90-
fn default() -> AtomicBool {
91-
ATOMIC_BOOL_INIT
90+
fn default() -> Self {
91+
Self::new(Default::default())
9292
}
9393
}
9494

@@ -101,8 +101,8 @@ pub struct AtomicIsize {
101101
}
102102

103103
impl Default for AtomicIsize {
104-
fn default() -> AtomicIsize {
105-
ATOMIC_ISIZE_INIT
104+
fn default() -> Self {
105+
Self::new(Default::default())
106106
}
107107
}
108108

@@ -115,8 +115,8 @@ pub struct AtomicUsize {
115115
}
116116

117117
impl Default for AtomicUsize {
118-
fn default() -> AtomicUsize {
119-
ATOMIC_USIZE_INIT
118+
fn default() -> Self {
119+
Self::new(Default::default())
120120
}
121121
}
122122

@@ -125,8 +125,7 @@ unsafe impl Sync for AtomicUsize {}
125125
/// A raw pointer type which can be safely shared between threads.
126126
#[stable(feature = "rust1", since = "1.0.0")]
127127
pub struct AtomicPtr<T> {
128-
p: UnsafeCell<usize>,
129-
_marker: PhantomData<*mut T>,
128+
p: UnsafeCell<*mut T>,
130129
}
131130

132131
impl<T> Default for AtomicPtr<T> {
@@ -175,16 +174,13 @@ pub enum Ordering {
175174

176175
/// An `AtomicBool` initialized to `false`.
177176
#[stable(feature = "rust1", since = "1.0.0")]
178-
pub const ATOMIC_BOOL_INIT: AtomicBool =
179-
AtomicBool { v: UnsafeCell { value: 0 } };
177+
pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false);
180178
/// An `AtomicIsize` initialized to `0`.
181179
#[stable(feature = "rust1", since = "1.0.0")]
182-
pub const ATOMIC_ISIZE_INIT: AtomicIsize =
183-
AtomicIsize { v: UnsafeCell { value: 0 } };
180+
pub const ATOMIC_ISIZE_INIT: AtomicIsize = AtomicIsize::new(0);
184181
/// An `AtomicUsize` initialized to `0`.
185182
#[stable(feature = "rust1", since = "1.0.0")]
186-
pub const ATOMIC_USIZE_INIT: AtomicUsize =
187-
AtomicUsize { v: UnsafeCell { value: 0, } };
183+
pub const ATOMIC_USIZE_INIT: AtomicUsize = AtomicUsize::new(0);
188184

189185
// NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
190186
const UINT_TRUE: usize = !0;
@@ -202,9 +198,8 @@ impl AtomicBool {
202198
/// ```
203199
#[inline]
204200
#[stable(feature = "rust1", since = "1.0.0")]
205-
pub fn new(v: bool) -> AtomicBool {
206-
let val = if v { UINT_TRUE } else { 0 };
207-
AtomicBool { v: UnsafeCell::new(val) }
201+
pub const fn new(v: bool) -> AtomicBool {
202+
AtomicBool { v: UnsafeCell::new(-(v as isize) as usize) }
208203
}
209204

210205
/// Loads a value from the bool.
@@ -445,7 +440,7 @@ impl AtomicIsize {
445440
/// ```
446441
#[inline]
447442
#[stable(feature = "rust1", since = "1.0.0")]
448-
pub fn new(v: isize) -> AtomicIsize {
443+
pub const fn new(v: isize) -> AtomicIsize {
449444
AtomicIsize {v: UnsafeCell::new(v)}
450445
}
451446

@@ -633,7 +628,7 @@ impl AtomicUsize {
633628
/// ```
634629
#[inline]
635630
#[stable(feature = "rust1", since = "1.0.0")]
636-
pub fn new(v: usize) -> AtomicUsize {
631+
pub const fn new(v: usize) -> AtomicUsize {
637632
AtomicUsize { v: UnsafeCell::new(v) }
638633
}
639634

@@ -821,9 +816,8 @@ impl<T> AtomicPtr<T> {
821816
/// ```
822817
#[inline]
823818
#[stable(feature = "rust1", since = "1.0.0")]
824-
pub fn new(p: *mut T) -> AtomicPtr<T> {
825-
AtomicPtr { p: UnsafeCell::new(p as usize),
826-
_marker: PhantomData }
819+
pub const fn new(p: *mut T) -> AtomicPtr<T> {
820+
AtomicPtr { p: UnsafeCell::new(p) }
827821
}
828822

829823
/// Loads a value from the pointer.
@@ -848,7 +842,7 @@ impl<T> AtomicPtr<T> {
848842
#[stable(feature = "rust1", since = "1.0.0")]
849843
pub fn load(&self, order: Ordering) -> *mut T {
850844
unsafe {
851-
atomic_load(self.p.get(), order) as *mut T
845+
atomic_load(self.p.get() as *mut usize, order) as *mut T
852846
}
853847
}
854848

@@ -875,7 +869,7 @@ impl<T> AtomicPtr<T> {
875869
#[inline]
876870
#[stable(feature = "rust1", since = "1.0.0")]
877871
pub fn store(&self, ptr: *mut T, order: Ordering) {
878-
unsafe { atomic_store(self.p.get(), ptr as usize, order); }
872+
unsafe { atomic_store(self.p.get() as *mut usize, ptr as usize, order); }
879873
}
880874

881875
/// Stores a value into the pointer, returning the old value.
@@ -897,7 +891,7 @@ impl<T> AtomicPtr<T> {
897891
#[inline]
898892
#[stable(feature = "rust1", since = "1.0.0")]
899893
pub fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T {
900-
unsafe { atomic_swap(self.p.get(), ptr as usize, order) as *mut T }
894+
unsafe { atomic_swap(self.p.get() as *mut usize, ptr as usize, order) as *mut T }
901895
}
902896

903897
/// Stores a value into the pointer if the current value is the same as the expected value.
@@ -925,7 +919,7 @@ impl<T> AtomicPtr<T> {
925919
#[stable(feature = "rust1", since = "1.0.0")]
926920
pub fn compare_and_swap(&self, old: *mut T, new: *mut T, order: Ordering) -> *mut T {
927921
unsafe {
928-
atomic_compare_and_swap(self.p.get(), old as usize,
922+
atomic_compare_and_swap(self.p.get() as *mut usize, old as usize,
929923
new as usize, order) as *mut T
930924
}
931925
}

src/libcore/cell.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<T:Copy> Cell<T> {
170170
/// ```
171171
#[stable(feature = "rust1", since = "1.0.0")]
172172
#[inline]
173-
pub fn new(value: T) -> Cell<T> {
173+
pub const fn new(value: T) -> Cell<T> {
174174
Cell {
175175
value: UnsafeCell::new(value),
176176
}
@@ -302,7 +302,7 @@ impl<T> RefCell<T> {
302302
/// ```
303303
#[stable(feature = "rust1", since = "1.0.0")]
304304
#[inline]
305-
pub fn new(value: T) -> RefCell<T> {
305+
pub const fn new(value: T) -> RefCell<T> {
306306
RefCell {
307307
value: UnsafeCell::new(value),
308308
borrow: Cell::new(UNUSED),
@@ -663,7 +663,7 @@ impl<T> UnsafeCell<T> {
663663
/// ```
664664
#[stable(feature = "rust1", since = "1.0.0")]
665665
#[inline]
666-
pub fn new(value: T) -> UnsafeCell<T> {
666+
pub const fn new(value: T) -> UnsafeCell<T> {
667667
UnsafeCell { value: value }
668668
}
669669

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#![feature(concat_idents)]
7575
#![feature(reflect)]
7676
#![feature(custom_attribute)]
77+
#![feature(const_fn)]
7778

7879
#[macro_use]
7980
mod macros;

src/libcoretest/atomic.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ fn int_xor() {
7070
assert_eq!(x.load(SeqCst), 0xf731 ^ 0x137f);
7171
}
7272

73-
static S_BOOL : AtomicBool = ATOMIC_BOOL_INIT;
74-
static S_INT : AtomicIsize = ATOMIC_ISIZE_INIT;
75-
static S_UINT : AtomicUsize = ATOMIC_USIZE_INIT;
73+
static S_FALSE: AtomicBool = AtomicBool::new(false);
74+
static S_TRUE: AtomicBool = AtomicBool::new(true);
75+
static S_INT: AtomicIsize = AtomicIsize::new(0);
76+
static S_UINT: AtomicUsize = AtomicUsize::new(0);
7677

7778
#[test]
7879
fn static_init() {
79-
assert!(!S_BOOL.load(SeqCst));
80+
assert!(!S_FALSE.load(SeqCst));
81+
assert!(S_TRUE.load(SeqCst));
8082
assert!(S_INT.load(SeqCst) == 0);
8183
assert!(S_UINT.load(SeqCst) == 0);
8284
}

src/liblog/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ use std::mem;
184184
use std::env;
185185
use std::rt;
186186
use std::slice;
187-
use std::sync::{Once, ONCE_INIT, StaticMutex, MUTEX_INIT};
187+
use std::sync::{Once, StaticMutex};
188188

189189
use directive::LOG_LEVEL_NAMES;
190190

@@ -200,7 +200,7 @@ pub const MAX_LOG_LEVEL: u32 = 255;
200200
/// The default logging level of a crate if no other is specified.
201201
const DEFAULT_LOG_LEVEL: u32 = 1;
202202

203-
static LOCK: StaticMutex = MUTEX_INIT;
203+
static LOCK: StaticMutex = StaticMutex::new();
204204

205205
/// An unsafe constant that is the maximum logging level of any module
206206
/// specified. This is the first line of defense to determining whether a
@@ -367,7 +367,7 @@ pub struct LogLocation {
367367
/// module's log statement should be emitted or not.
368368
#[doc(hidden)]
369369
pub fn mod_enabled(level: u32, module: &str) -> bool {
370-
static INIT: Once = ONCE_INIT;
370+
static INIT: Once = Once::new();
371371
INIT.call_once(init);
372372

373373
// It's possible for many threads are in this function, only one of them

src/librustc/middle/infer/region_inference/graphviz.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::env;
3232
use std::fs::File;
3333
use std::io;
3434
use std::io::prelude::*;
35-
use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
35+
use std::sync::atomic::{AtomicBool, Ordering};
3636
use syntax::ast;
3737

3838
fn print_help_message() {
@@ -76,7 +76,7 @@ pub fn maybe_print_constraints_for<'a, 'tcx>(region_vars: &RegionVarBindings<'a,
7676
let output_path = {
7777
let output_template = match requested_output {
7878
Ok(ref s) if &**s == "help" => {
79-
static PRINTED_YET: AtomicBool = ATOMIC_BOOL_INIT;
79+
static PRINTED_YET: AtomicBool = AtomicBool::new(false);
8080
if !PRINTED_YET.load(Ordering::SeqCst) {
8181
print_help_message();
8282
PRINTED_YET.store(true, Ordering::SeqCst);

src/librustc_trans/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,8 +1005,8 @@ pub fn run_assembler(sess: &Session, outputs: &OutputFilenames) {
10051005
}
10061006

10071007
unsafe fn configure_llvm(sess: &Session) {
1008-
use std::sync::{Once, ONCE_INIT};
1009-
static INIT: Once = ONCE_INIT;
1008+
use std::sync::Once;
1009+
static INIT: Once = Once::new();
10101010

10111011
// Copy what clang does by turning on loop vectorization at O2 and
10121012
// slp vectorization at O3

src/librustc_trans/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#![feature(path_ext)]
4040
#![feature(fs)]
4141
#![feature(path_relative_from)]
42+
#![feature(std_misc)]
4243

4344
#![allow(trivial_casts)]
4445

src/librustc_trans/trans/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,8 +2653,8 @@ pub fn trans_crate<'tcx>(analysis: ty::CrateAnalysis<'tcx>)
26532653

26542654
// Before we touch LLVM, make sure that multithreading is enabled.
26552655
unsafe {
2656-
use std::sync::{Once, ONCE_INIT};
2657-
static INIT: Once = ONCE_INIT;
2656+
use std::sync::Once;
2657+
static INIT: Once = Once::new();
26582658
static mut POISONED: bool = false;
26592659
INIT.call_once(|| {
26602660
if llvm::LLVMStartMultithreaded() != 1 {

0 commit comments

Comments
 (0)