Skip to content

Commit 377b090

Browse files
committed
Use const fn to abstract away the contents of UnsafeCell & friends.
1 parent 6e8e4f8 commit 377b090

Some content is hidden

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

76 files changed

+417
-525
lines changed

src/libcollectionstest/vec.rs

+2-2
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

+20-27
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ use marker::Sync;
7676

7777
use intrinsics;
7878
use cell::UnsafeCell;
79-
use marker::PhantomData;
8079

8180
use default::Default;
8281

@@ -87,8 +86,8 @@ pub struct AtomicBool {
8786
}
8887

8988
impl Default for AtomicBool {
90-
fn default() -> AtomicBool {
91-
ATOMIC_BOOL_INIT
89+
fn default() -> Self {
90+
Self::new(Default::default())
9291
}
9392
}
9493

@@ -101,8 +100,8 @@ pub struct AtomicIsize {
101100
}
102101

103102
impl Default for AtomicIsize {
104-
fn default() -> AtomicIsize {
105-
ATOMIC_ISIZE_INIT
103+
fn default() -> Self {
104+
Self::new(Default::default())
106105
}
107106
}
108107

@@ -115,8 +114,8 @@ pub struct AtomicUsize {
115114
}
116115

117116
impl Default for AtomicUsize {
118-
fn default() -> AtomicUsize {
119-
ATOMIC_USIZE_INIT
117+
fn default() -> Self {
118+
Self::new(Default::default())
120119
}
121120
}
122121

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

132130
impl<T> Default for AtomicPtr<T> {
@@ -175,16 +173,13 @@ pub enum Ordering {
175173

176174
/// An `AtomicBool` initialized to `false`.
177175
#[stable(feature = "rust1", since = "1.0.0")]
178-
pub const ATOMIC_BOOL_INIT: AtomicBool =
179-
AtomicBool { v: UnsafeCell { value: 0 } };
176+
pub const ATOMIC_BOOL_INIT: AtomicBool = AtomicBool::new(false);
180177
/// An `AtomicIsize` initialized to `0`.
181178
#[stable(feature = "rust1", since = "1.0.0")]
182-
pub const ATOMIC_ISIZE_INIT: AtomicIsize =
183-
AtomicIsize { v: UnsafeCell { value: 0 } };
179+
pub const ATOMIC_ISIZE_INIT: AtomicIsize = AtomicIsize::new(0);
184180
/// An `AtomicUsize` initialized to `0`.
185181
#[stable(feature = "rust1", since = "1.0.0")]
186-
pub const ATOMIC_USIZE_INIT: AtomicUsize =
187-
AtomicUsize { v: UnsafeCell { value: 0, } };
182+
pub const ATOMIC_USIZE_INIT: AtomicUsize = AtomicUsize::new(0);
188183

189184
// NB: Needs to be -1 (0b11111111...) to make fetch_nand work correctly
190185
const UINT_TRUE: usize = !0;
@@ -202,9 +197,8 @@ impl AtomicBool {
202197
/// ```
203198
#[inline]
204199
#[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) }
200+
pub const fn new(v: bool) -> AtomicBool {
201+
AtomicBool { v: UnsafeCell::new(-(v as isize) as usize) }
208202
}
209203

210204
/// Loads a value from the bool.
@@ -445,7 +439,7 @@ impl AtomicIsize {
445439
/// ```
446440
#[inline]
447441
#[stable(feature = "rust1", since = "1.0.0")]
448-
pub fn new(v: isize) -> AtomicIsize {
442+
pub const fn new(v: isize) -> AtomicIsize {
449443
AtomicIsize {v: UnsafeCell::new(v)}
450444
}
451445

@@ -633,7 +627,7 @@ impl AtomicUsize {
633627
/// ```
634628
#[inline]
635629
#[stable(feature = "rust1", since = "1.0.0")]
636-
pub fn new(v: usize) -> AtomicUsize {
630+
pub const fn new(v: usize) -> AtomicUsize {
637631
AtomicUsize { v: UnsafeCell::new(v) }
638632
}
639633

@@ -821,9 +815,8 @@ impl<T> AtomicPtr<T> {
821815
/// ```
822816
#[inline]
823817
#[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 }
818+
pub const fn new(p: *mut T) -> AtomicPtr<T> {
819+
AtomicPtr { p: UnsafeCell::new(p) }
827820
}
828821

829822
/// Loads a value from the pointer.
@@ -848,7 +841,7 @@ impl<T> AtomicPtr<T> {
848841
#[stable(feature = "rust1", since = "1.0.0")]
849842
pub fn load(&self, order: Ordering) -> *mut T {
850843
unsafe {
851-
atomic_load(self.p.get(), order) as *mut T
844+
atomic_load(self.p.get() as *mut usize, order) as *mut T
852845
}
853846
}
854847

@@ -875,7 +868,7 @@ impl<T> AtomicPtr<T> {
875868
#[inline]
876869
#[stable(feature = "rust1", since = "1.0.0")]
877870
pub fn store(&self, ptr: *mut T, order: Ordering) {
878-
unsafe { atomic_store(self.p.get(), ptr as usize, order); }
871+
unsafe { atomic_store(self.p.get() as *mut usize, ptr as usize, order); }
879872
}
880873

881874
/// Stores a value into the pointer, returning the old value.
@@ -897,7 +890,7 @@ impl<T> AtomicPtr<T> {
897890
#[inline]
898891
#[stable(feature = "rust1", since = "1.0.0")]
899892
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 }
893+
unsafe { atomic_swap(self.p.get() as *mut usize, ptr as usize, order) as *mut T }
901894
}
902895

903896
/// 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> {
925918
#[stable(feature = "rust1", since = "1.0.0")]
926919
pub fn compare_and_swap(&self, old: *mut T, new: *mut T, order: Ordering) -> *mut T {
927920
unsafe {
928-
atomic_compare_and_swap(self.p.get(), old as usize,
921+
atomic_compare_and_swap(self.p.get() as *mut usize, old as usize,
929922
new as usize, order) as *mut T
930923
}
931924
}

src/libcore/cell.rs

+3-3
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

+1
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

+6-4
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

+3-3
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

+2-2
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

+2-2
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

+1
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

+2-2
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 {

src/libstd/dynamic_lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ mod dl {
211211
pub fn check_for_errors_in<T, F>(f: F) -> Result<T, String> where
212212
F: FnOnce() -> T,
213213
{
214-
use sync::{StaticMutex, MUTEX_INIT};
215-
static LOCK: StaticMutex = MUTEX_INIT;
214+
use sync::StaticMutex;
215+
static LOCK: StaticMutex = StaticMutex::new();
216216
unsafe {
217217
// dlerror isn't thread safe, so we need to lock around this entire
218218
// sequence

src/libstd/env.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use ffi::{OsStr, OsString};
2323
use fmt;
2424
use io;
2525
use path::{Path, PathBuf};
26-
use sync::atomic::{AtomicIsize, ATOMIC_ISIZE_INIT, Ordering};
27-
use sync::{StaticMutex, MUTEX_INIT};
26+
use sync::atomic::{AtomicIsize, Ordering};
27+
use sync::StaticMutex;
2828
use sys::os as os_imp;
2929

3030
/// Returns the current working directory as a `PathBuf`.
@@ -70,7 +70,7 @@ pub fn set_current_dir<P: AsRef<Path>>(p: P) -> io::Result<()> {
7070
os_imp::chdir(p.as_ref())
7171
}
7272

73-
static ENV_LOCK: StaticMutex = MUTEX_INIT;
73+
static ENV_LOCK: StaticMutex = StaticMutex::new();
7474

7575
/// An iterator over a snapshot of the environment variables of this process.
7676
///
@@ -475,7 +475,7 @@ pub fn current_exe() -> io::Result<PathBuf> {
475475
os_imp::current_exe()
476476
}
477477

478-
static EXIT_STATUS: AtomicIsize = ATOMIC_ISIZE_INIT;
478+
static EXIT_STATUS: AtomicIsize = AtomicIsize::new(0);
479479

480480
/// Sets the process exit code
481481
///

src/libstd/io/lazy.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
use prelude::v1::*;
1212

1313
use boxed;
14-
use cell::UnsafeCell;
14+
use cell::Cell;
1515
use rt;
1616
use sync::{StaticMutex, Arc};
1717

1818
pub struct Lazy<T> {
19-
pub lock: StaticMutex,
20-
pub ptr: UnsafeCell<*mut Arc<T>>,
21-
pub init: fn() -> Arc<T>,
19+
lock: StaticMutex,
20+
ptr: Cell<*mut Arc<T>>,
21+
init: fn() -> Arc<T>,
2222
}
2323

2424
unsafe impl<T> Sync for Lazy<T> {}
2525

26-
macro_rules! lazy_init {
27-
($init:expr) => (::io::lazy::Lazy {
28-
lock: ::sync::MUTEX_INIT,
29-
ptr: ::cell::UnsafeCell { value: 0 as *mut _ },
30-
init: $init,
31-
})
32-
}
33-
3426
impl<T: Send + Sync + 'static> Lazy<T> {
27+
pub const fn new(init: fn() -> Arc<T>) -> Lazy<T> {
28+
Lazy {
29+
lock: StaticMutex::new(),
30+
ptr: Cell::new(0 as *mut _),
31+
init: init
32+
}
33+
}
34+
3535
pub fn get(&'static self) -> Option<Arc<T>> {
3636
let _g = self.lock.lock();
37+
let ptr = self.ptr.get();
3738
unsafe {
38-
let ptr = *self.ptr.get();
3939
if ptr.is_null() {
4040
Some(self.init())
4141
} else if ptr as usize == 1 {
@@ -53,14 +53,14 @@ impl<T: Send + Sync + 'static> Lazy<T> {
5353
// `Arc`.
5454
let registered = rt::at_exit(move || {
5555
let g = self.lock.lock();
56-
let ptr = *self.ptr.get();
57-
*self.ptr.get() = 1 as *mut _;
56+
let ptr = self.ptr.get();
57+
self.ptr.set(1 as *mut _);
5858
drop(g);
5959
drop(Box::from_raw(ptr))
6060
});
6161
let ret = (self.init)();
6262
if registered.is_ok() {
63-
*self.ptr.get() = boxed::into_raw(Box::new(ret.clone()));
63+
self.ptr.set(boxed::into_raw(Box::new(ret.clone())));
6464
}
6565
return ret
6666
}

0 commit comments

Comments
 (0)