Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3c9258e

Browse files
committedOct 19, 2018
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

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

+12-20
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)

0 commit comments

Comments
 (0)
Please sign in to comment.