Skip to content

Commit 3c9258e

Browse files
committed
Prefer Default::default over FxHash*::default in struct constructors
1 parent ee81739 commit 3c9258e

File tree

82 files changed

+237
-362
lines changed

Some content is hidden

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

82 files changed

+237
-362
lines changed

src/bootstrap/cache.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,13 @@ impl Ord for Interned<String> {
169169
}
170170
}
171171

172-
struct TyIntern<T> {
172+
#[derive(Default)]
173+
struct TyIntern<T: Hash + Clone + Eq> {
173174
items: Vec<T>,
174175
set: HashMap<T, Interned<T>>,
175176
}
176177

177178
impl<T: Hash + Clone + Eq> TyIntern<T> {
178-
fn new() -> TyIntern<T> {
179-
TyIntern {
180-
items: Vec::new(),
181-
set: HashMap::new(),
182-
}
183-
}
184-
185179
fn intern_borrow<B>(&mut self, item: &B) -> Interned<T>
186180
where
187181
B: Eq + Hash + ToOwned<Owned=T> + ?Sized,
@@ -212,19 +206,13 @@ impl<T: Hash + Clone + Eq> TyIntern<T> {
212206
}
213207
}
214208

209+
#[derive(Default)]
215210
pub struct Interner {
216211
strs: Mutex<TyIntern<String>>,
217212
paths: Mutex<TyIntern<PathBuf>>,
218213
}
219214

220215
impl Interner {
221-
fn new() -> Interner {
222-
Interner {
223-
strs: Mutex::new(TyIntern::new()),
224-
paths: Mutex::new(TyIntern::new()),
225-
}
226-
}
227-
228216
pub fn intern_str(&self, s: &str) -> Interned<String> {
229217
self.strs.lock().unwrap().intern_borrow(s)
230218
}
@@ -238,7 +226,7 @@ impl Interner {
238226
}
239227

240228
lazy_static! {
241-
pub static ref INTERNER: Interner = Interner::new();
229+
pub static ref INTERNER: Interner = Interner::default();
242230
}
243231

244232
/// This is essentially a HashMap which allows storing any type in its input and

src/libarena/lib.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ impl<T> TypedArenaChunk<T> {
114114

115115
const PAGE: usize = 4096;
116116

117-
impl<T> TypedArena<T> {
117+
impl<T> Default for TypedArena<T> {
118118
/// Creates a new `TypedArena`.
119-
#[inline]
120-
pub fn new() -> TypedArena<T> {
119+
fn default() -> TypedArena<T> {
121120
TypedArena {
122121
// We set both `ptr` and `end` to 0 so that the first call to
123122
// alloc() will trigger a grow().
@@ -127,7 +126,9 @@ impl<T> TypedArena<T> {
127126
_own: PhantomData,
128127
}
129128
}
129+
}
130130

131+
impl<T> TypedArena<T> {
131132
/// Allocates an object in the `TypedArena`, returning a reference to it.
132133
#[inline]
133134
pub fn alloc(&self, object: T) -> &mut T {
@@ -296,15 +297,17 @@ pub struct DroplessArena {
296297

297298
unsafe impl Send for DroplessArena {}
298299

299-
impl DroplessArena {
300-
pub fn new() -> DroplessArena {
300+
impl Default for DroplessArena {
301+
fn default() -> DroplessArena {
301302
DroplessArena {
302303
ptr: Cell::new(0 as *mut u8),
303304
end: Cell::new(0 as *mut u8),
304-
chunks: RefCell::new(vec![]),
305+
chunks: Default::default(),
305306
}
306307
}
308+
}
307309

310+
impl DroplessArena {
308311
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
309312
let ptr = ptr as *const u8 as *mut u8;
310313
for chunk in &*self.chunks.borrow() {
@@ -419,18 +422,13 @@ impl DroplessArena {
419422
}
420423
}
421424

425+
#[derive(Default)]
426+
// FIXME(@Zoxc): this type is entirely unused in rustc
422427
pub struct SyncTypedArena<T> {
423428
lock: MTLock<TypedArena<T>>,
424429
}
425430

426431
impl<T> SyncTypedArena<T> {
427-
#[inline(always)]
428-
pub fn new() -> SyncTypedArena<T> {
429-
SyncTypedArena {
430-
lock: MTLock::new(TypedArena::new())
431-
}
432-
}
433-
434432
#[inline(always)]
435433
pub fn alloc(&self, object: T) -> &mut T {
436434
// Extend the lifetime of the result since it's limited to the lock guard
@@ -452,18 +450,12 @@ impl<T> SyncTypedArena<T> {
452450
}
453451
}
454452

453+
#[derive(Default)]
455454
pub struct SyncDroplessArena {
456455
lock: MTLock<DroplessArena>,
457456
}
458457

459458
impl SyncDroplessArena {
460-
#[inline(always)]
461-
pub fn new() -> SyncDroplessArena {
462-
SyncDroplessArena {
463-
lock: MTLock::new(DroplessArena::new())
464-
}
465-
}
466-
467459
#[inline(always)]
468460
pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
469461
self.lock.lock().in_arena(ptr)

src/librustc/dep_graph/cgu_reuse_tracker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ pub struct CguReuseTracker {
5151
impl CguReuseTracker {
5252
pub fn new() -> CguReuseTracker {
5353
let data = TrackerData {
54-
actual_reuse: FxHashMap::default(),
55-
expected_reuse: FxHashMap::default(),
54+
actual_reuse: Default::default(),
55+
expected_reuse: Default::default(),
5656
};
5757

5858
CguReuseTracker {

src/librustc/dep_graph/dep_tracking_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<M: DepTrackingMapConfig> DepTrackingMap<M> {
3636
DepTrackingMap {
3737
phantom: PhantomData,
3838
graph,
39-
map: FxHashMap::default(),
39+
map: Default::default(),
4040
}
4141
}
4242
}

src/librustc/dep_graph/graph.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ impl DepGraph {
101101
DepGraph {
102102
data: Some(Lrc::new(DepGraphData {
103103
previous_work_products: prev_work_products,
104-
dep_node_debug: Lock::new(FxHashMap::default()),
104+
dep_node_debug: Lock::new(Default::default()),
105105
current: Lock::new(CurrentDepGraph::new()),
106106
previous: prev_graph,
107107
colors: Lock::new(DepNodeColorMap::new(prev_graph_node_count)),
108-
loaded_from_cache: Lock::new(FxHashMap::default()),
108+
loaded_from_cache: Lock::new(Default::default()),
109109
})),
110110
fingerprints: Lrc::new(Lock::new(fingerprints)),
111111
}
@@ -209,7 +209,7 @@ impl DepGraph {
209209
|key| OpenTask::Regular(Lock::new(RegularOpenTask {
210210
node: key,
211211
reads: SmallVec::new(),
212-
read_set: FxHashSet::default(),
212+
read_set: Default::default(),
213213
})),
214214
|data, key, task| data.borrow_mut().complete_task(key, task))
215215
}
@@ -353,7 +353,7 @@ impl DepGraph {
353353
let (result, open_task) = ty::tls::with_context(|icx| {
354354
let task = OpenTask::Anon(Lock::new(AnonOpenTask {
355355
reads: SmallVec::new(),
356-
read_set: FxHashSet::default(),
356+
read_set: Default::default(),
357357
}));
358358

359359
let r = {
@@ -937,7 +937,7 @@ impl CurrentDepGraph {
937937
CurrentDepGraph {
938938
nodes: IndexVec::new(),
939939
edges: IndexVec::new(),
940-
node_to_node_index: FxHashMap::default(),
940+
node_to_node_index: Default::default(),
941941
anon_id_seed: stable_hasher.finish(),
942942
forbidden_edge,
943943
total_read_count: 0,

src/librustc/dep_graph/prev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap;
1313
use super::dep_node::DepNode;
1414
use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
1515

16-
#[derive(Debug, RustcEncodable, RustcDecodable)]
16+
#[derive(Debug, RustcEncodable, RustcDecodable, Default)]
1717
pub struct PreviousDepGraph {
1818
data: SerializedDepGraph,
1919
index: FxHashMap<DepNode, SerializedDepNodeIndex>,

src/librustc/dep_graph/serialized.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ newtype_index! {
1919
}
2020

2121
/// Data for use when recompiling the **current crate**.
22-
#[derive(Debug, RustcEncodable, RustcDecodable)]
22+
#[derive(Debug, RustcEncodable, RustcDecodable, Default)]
2323
pub struct SerializedDepGraph {
2424
/// The set of all DepNodes in the graph
2525
pub nodes: IndexVec<SerializedDepNodeIndex, DepNode>,
@@ -36,16 +36,6 @@ pub struct SerializedDepGraph {
3636
}
3737

3838
impl SerializedDepGraph {
39-
40-
pub fn new() -> SerializedDepGraph {
41-
SerializedDepGraph {
42-
nodes: IndexVec::new(),
43-
fingerprints: IndexVec::new(),
44-
edge_list_indices: IndexVec::new(),
45-
edge_list_data: Vec::new(),
46-
}
47-
}
48-
4939
#[inline]
5040
pub fn edge_targets_from(&self,
5141
source: SerializedDepNodeIndex)

src/librustc/hir/map/definitions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@ impl Definitions {
421421
node_to_def_index: NodeMap(),
422422
def_index_to_node: [vec![], vec![]],
423423
node_to_hir_id: IndexVec::new(),
424-
parent_modules_of_macro_defs: FxHashMap::default(),
425-
expansions_that_defined: FxHashMap::default(),
426-
next_disambiguator: FxHashMap::default(),
427-
def_index_to_span: FxHashMap::default(),
424+
parent_modules_of_macro_defs: Default::default(),
425+
expansions_that_defined: Default::default(),
426+
next_disambiguator: Default::default(),
427+
def_index_to_span: Default::default(),
428428
}
429429
}
430430

src/librustc/hir/map/hir_id_validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a, 'hir: 'a> OuterVisitor<'a, 'hir> {
5151
HirIdValidator {
5252
hir_map,
5353
owner_def_index: None,
54-
hir_ids_seen: FxHashMap::default(),
54+
hir_ids_seen: Default::default(),
5555
errors: Vec::new(),
5656
}
5757
}

src/librustc/ich/hcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for Span {
370370
// recursing every time.
371371
thread_local! {
372372
static CACHE: RefCell<FxHashMap<hygiene::Mark, u64>> =
373-
RefCell::new(FxHashMap::default());
373+
RefCell::new(Default::default());
374374
}
375375

376376
let sub_hash: u64 = CACHE.with(|cache| {

0 commit comments

Comments
 (0)