mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
impl gen hash for structs
This commit is contained in:
parent
ec2535e9ce
commit
4b5139e8a5
3 changed files with 77 additions and 13 deletions
|
@ -2732,8 +2732,8 @@ fn foo() {
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
full_range: 252..434,
|
full_range: 253..435,
|
||||||
focus_range: 291..297,
|
focus_range: 292..298,
|
||||||
name: "Future",
|
name: "Future",
|
||||||
kind: Trait,
|
kind: Trait,
|
||||||
description: "pub trait Future",
|
description: "pub trait Future",
|
||||||
|
|
|
@ -370,6 +370,56 @@ impl Default for Foo {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_custom_impl_hash_record_struct() {
|
||||||
|
check_assist(
|
||||||
|
replace_derive_with_manual_impl,
|
||||||
|
r#"
|
||||||
|
//- minicore: hash
|
||||||
|
#[derive(Has$0h)]
|
||||||
|
struct Foo {
|
||||||
|
bin: usize,
|
||||||
|
bar: usize,
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
struct Foo {
|
||||||
|
bin: usize,
|
||||||
|
bar: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl core::hash::Hash for Foo {
|
||||||
|
$0fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
|
||||||
|
self.bin.hash(state);
|
||||||
|
self.bar.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_custom_impl_hash_tuple_struct() {
|
||||||
|
check_assist(
|
||||||
|
replace_derive_with_manual_impl,
|
||||||
|
r#"
|
||||||
|
//- minicore: hash
|
||||||
|
#[derive(Has$0h)]
|
||||||
|
struct Foo(usize, usize);
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
struct Foo(usize, usize);
|
||||||
|
|
||||||
|
impl core::hash::Hash for Foo {
|
||||||
|
$0fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
|
||||||
|
self.0.hash(state);
|
||||||
|
self.1.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_custom_impl_hash_enum() {
|
fn add_custom_impl_hash_enum() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
|
|
@ -155,6 +155,14 @@ fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
|
||||||
|
|
||||||
/// Generate a `Hash` impl based on the fields and members of the target type.
|
/// Generate a `Hash` impl based on the fields and members of the target type.
|
||||||
fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
|
fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
|
||||||
|
fn gen_hash_call(target: ast::Expr) -> ast::Stmt {
|
||||||
|
let method = make::name_ref("hash");
|
||||||
|
let arg = make::expr_path(make::ext::ident_path("state"));
|
||||||
|
let expr = make::expr_method_call(target, method, make::arg_list(Some(arg)));
|
||||||
|
let stmt = make::expr_stmt(expr);
|
||||||
|
stmt.into()
|
||||||
|
}
|
||||||
|
|
||||||
let body = match adt {
|
let body = match adt {
|
||||||
// `Hash` cannot be derived for unions, so no default impl can be provided.
|
// `Hash` cannot be derived for unions, so no default impl can be provided.
|
||||||
ast::Adt::Union(_) => return None,
|
ast::Adt::Union(_) => return None,
|
||||||
|
@ -169,29 +177,35 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> {
|
||||||
|
|
||||||
let arg = make::expr_path(make::ext::ident_path("self"));
|
let arg = make::expr_path(make::ext::ident_path("self"));
|
||||||
let fn_call = make::expr_call(fn_name, make::arg_list(Some(arg)));
|
let fn_call = make::expr_call(fn_name, make::arg_list(Some(arg)));
|
||||||
|
let stmt = gen_hash_call(fn_call);
|
||||||
|
|
||||||
let method = make::name_ref("hash");
|
make::block_expr(Some(stmt), None).indent(ast::edit::IndentLevel(1))
|
||||||
let arg = make::expr_path(make::ext::ident_path("state"));
|
|
||||||
let expr = make::expr_method_call(fn_call, method, make::arg_list(Some(arg)));
|
|
||||||
let stmt = make::expr_stmt(expr);
|
|
||||||
|
|
||||||
make::block_expr(Some(stmt.into()), None).indent(ast::edit::IndentLevel(1))
|
|
||||||
}
|
}
|
||||||
ast::Adt::Struct(strukt) => match strukt.field_list() {
|
ast::Adt::Struct(strukt) => match strukt.field_list() {
|
||||||
// => self.<field>.hash(state);*
|
// => self.<field>.hash(state);*
|
||||||
Some(ast::FieldList::RecordFieldList(field_list)) => {
|
Some(ast::FieldList::RecordFieldList(field_list)) => {
|
||||||
// let mut stmts = vec![];
|
let mut stmts = vec![];
|
||||||
for field in field_list.fields() {}
|
for field in field_list.fields() {
|
||||||
todo!();
|
let base = make::expr_path(make::ext::ident_path("self"));
|
||||||
|
let target = make::expr_field(base, &field.name()?.to_string());
|
||||||
|
stmts.push(gen_hash_call(target));
|
||||||
|
}
|
||||||
|
make::block_expr(stmts, None).indent(ast::edit::IndentLevel(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// => self.<field_index>.hash(state);*
|
// => self.<field_index>.hash(state);*
|
||||||
Some(ast::FieldList::TupleFieldList(field_list)) => {
|
Some(ast::FieldList::TupleFieldList(field_list)) => {
|
||||||
todo!();
|
let mut stmts = vec![];
|
||||||
|
for (i, _) in field_list.fields().enumerate() {
|
||||||
|
let base = make::expr_path(make::ext::ident_path("self"));
|
||||||
|
let target = make::expr_field(base, &format!("{}", i));
|
||||||
|
stmts.push(gen_hash_call(target));
|
||||||
|
}
|
||||||
|
make::block_expr(stmts, None).indent(ast::edit::IndentLevel(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// No fields in the body means there's nothing to hash.
|
// No fields in the body means there's nothing to hash.
|
||||||
None => make::ext::empty_block_expr(),
|
None => return None,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue