Skip to content

Commit

Permalink
Use #[derive(Default)] instead of manually implementing it
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank authored and gitbot committed Feb 20, 2025
1 parent f571bfe commit c04bda7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 33 deletions.
15 changes: 3 additions & 12 deletions core/tests/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ use std::hash::{BuildHasher, Hash, Hasher};
use std::ptr;
use std::rc::Rc;

#[derive(Default)]
struct MyHasher {
hash: u64,
}

impl Default for MyHasher {
fn default() -> MyHasher {
MyHasher { hash: 0 }
}
}

impl Hasher for MyHasher {
fn write(&mut self, buf: &[u8]) {
for byte in buf {
Expand Down Expand Up @@ -107,6 +102,8 @@ fn test_writer_hasher() {
struct Custom {
hash: u64,
}

#[derive(Default)]
struct CustomHasher {
output: u64,
}
Expand All @@ -123,12 +120,6 @@ impl Hasher for CustomHasher {
}
}

impl Default for CustomHasher {
fn default() -> CustomHasher {
CustomHasher { output: 0 }
}
}

impl Hash for Custom {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u64(self.hash);
Expand Down
8 changes: 1 addition & 7 deletions proc_macro/src/bridge/fxhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
/// out-performs an FNV-based hash within rustc itself -- the collision rate is
/// similar or slightly worse than FNV, but the speed of the hash function
/// itself is much higher because it works on up to 8 bytes at a time.
#[derive(Default)]
pub struct FxHasher {
hash: usize,
}
Expand All @@ -31,13 +32,6 @@ const K: usize = 0x9e3779b9;
#[cfg(target_pointer_width = "64")]
const K: usize = 0x517cc1b727220a95;

impl Default for FxHasher {
#[inline]
fn default() -> FxHasher {
FxHasher { hash: 0 }
}
}

impl FxHasher {
#[inline]
fn add_to_hash(&mut self, i: usize) {
Expand Down
9 changes: 2 additions & 7 deletions std/src/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ extern "C" fn __rust_foreign_exception() -> ! {
rtabort!("Rust cannot catch foreign exceptions");
}

#[derive(Default)]
enum Hook {
#[default]
Default,
Custom(Box<dyn Fn(&PanicHookInfo<'_>) + 'static + Sync + Send>),
}
Expand All @@ -96,13 +98,6 @@ impl Hook {
}
}

impl Default for Hook {
#[inline]
fn default() -> Hook {
Hook::Default
}
}

static HOOK: RwLock<Hook> = RwLock::new(Hook::Default);

/// Registers a custom panic hook, replacing the previously registered hook.
Expand Down
8 changes: 1 addition & 7 deletions std/src/sys_common/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ use crate::sys::process::{EnvKey, ExitStatus, Process, StdioPipes};
use crate::{env, fmt, io};

// Stores a set of changes to an environment
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct CommandEnv {
clear: bool,
saw_path: bool,
vars: BTreeMap<EnvKey, Option<OsString>>,
}

impl Default for CommandEnv {
fn default() -> Self {
CommandEnv { clear: false, saw_path: false, vars: Default::default() }
}
}

impl fmt::Debug for CommandEnv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug_command_env = f.debug_struct("CommandEnv");
Expand Down

0 comments on commit c04bda7

Please sign in to comment.