Skip to content

Commit 4b5139e

Browse files
committed
impl gen hash for structs
1 parent ec2535e commit 4b5139e

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

crates/ide/src/hover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,8 +2732,8 @@ fn foo() {
27322732
file_id: FileId(
27332733
1,
27342734
),
2735-
full_range: 252..434,
2736-
focus_range: 291..297,
2735+
full_range: 253..435,
2736+
focus_range: 292..298,
27372737
name: "Future",
27382738
kind: Trait,
27392739
description: "pub trait Future",

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,56 @@ impl Default for Foo {
370370
)
371371
}
372372

373+
#[test]
374+
fn add_custom_impl_hash_record_struct() {
375+
check_assist(
376+
replace_derive_with_manual_impl,
377+
r#"
378+
//- minicore: hash
379+
#[derive(Has$0h)]
380+
struct Foo {
381+
bin: usize,
382+
bar: usize,
383+
}
384+
"#,
385+
r#"
386+
struct Foo {
387+
bin: usize,
388+
bar: usize,
389+
}
390+
391+
impl core::hash::Hash for Foo {
392+
$0fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
393+
self.bin.hash(state);
394+
self.bar.hash(state);
395+
}
396+
}
397+
"#,
398+
)
399+
}
400+
401+
#[test]
402+
fn add_custom_impl_hash_tuple_struct() {
403+
check_assist(
404+
replace_derive_with_manual_impl,
405+
r#"
406+
//- minicore: hash
407+
#[derive(Has$0h)]
408+
struct Foo(usize, usize);
409+
"#,
410+
r#"
411+
struct Foo(usize, usize);
412+
413+
impl core::hash::Hash for Foo {
414+
$0fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
415+
self.0.hash(state);
416+
self.1.hash(state);
417+
}
418+
}
419+
"#,
420+
)
421+
}
422+
373423
#[test]
374424
fn add_custom_impl_hash_enum() {
375425
check_assist(

crates/ide_assists/src/utils/gen_trait_fn_body.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
155155

156156
/// Generate a `Hash` impl based on the fields and members of the target type.
157157
fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
158+
fn gen_hash_call(target: ast::Expr) -> ast::Stmt {
159+
let method = make::name_ref("hash");
160+
let arg = make::expr_path(make::ext::ident_path("state"));
161+
let expr = make::expr_method_call(target, method, make::arg_list(Some(arg)));
162+
let stmt = make::expr_stmt(expr);
163+
stmt.into()
164+
}
165+
158166
let body = match adt {
159167
// `Hash` cannot be derived for unions, so no default impl can be provided.
160168
ast::Adt::Union(_) => return None,
@@ -169,29 +177,35 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
169177

170178
let arg = make::expr_path(make::ext::ident_path("self"));
171179
let fn_call = make::expr_call(fn_name, make::arg_list(Some(arg)));
180+
let stmt = gen_hash_call(fn_call);
172181

173-
let method = make::name_ref("hash");
174-
let arg = make::expr_path(make::ext::ident_path("state"));
175-
let expr = make::expr_method_call(fn_call, method, make::arg_list(Some(arg)));
176-
let stmt = make::expr_stmt(expr);
177-
178-
make::block_expr(Some(stmt.into()), None).indent(ast::edit::IndentLevel(1))
182+
make::block_expr(Some(stmt), None).indent(ast::edit::IndentLevel(1))
179183
}
180184
ast::Adt::Struct(strukt) => match strukt.field_list() {
181185
// => self.<field>.hash(state);*
182186
Some(ast::FieldList::RecordFieldList(field_list)) => {
183-
// let mut stmts = vec![];
184-
for field in field_list.fields() {}
185-
todo!();
187+
let mut stmts = vec![];
188+
for field in field_list.fields() {
189+
let base = make::expr_path(make::ext::ident_path("self"));
190+
let target = make::expr_field(base, &field.name()?.to_string());
191+
stmts.push(gen_hash_call(target));
192+
}
193+
make::block_expr(stmts, None).indent(ast::edit::IndentLevel(1))
186194
}
187195

188196
// => self.<field_index>.hash(state);*
189197
Some(ast::FieldList::TupleFieldList(field_list)) => {
190-
todo!();
198+
let mut stmts = vec![];
199+
for (i, _) in field_list.fields().enumerate() {
200+
let base = make::expr_path(make::ext::ident_path("self"));
201+
let target = make::expr_field(base, &format!("{}", i));
202+
stmts.push(gen_hash_call(target));
203+
}
204+
make::block_expr(stmts, None).indent(ast::edit::IndentLevel(1))
191205
}
192206

193207
// No fields in the body means there's nothing to hash.
194-
None => make::ext::empty_block_expr(),
208+
None => return None,
195209
},
196210
};
197211

0 commit comments

Comments
 (0)