Add Deref -> DerefMut for generate_mut_trait_impl

This commit is contained in:
A4-Tacks 2025-07-19 15:00:54 +08:00
parent 1520876545
commit dfd8434847
No known key found for this signature in database
GPG Key ID: 86AC1F526BA06668

View File

@ -134,6 +134,9 @@ fn get_trait_mut(apply_trait: &hir::Trait, famous: FamousDefs<'_, '_>) -> Option
if trait_ == famous.core_borrow_Borrow().as_ref() {
return Some("BorrowMut");
}
if trait_ == famous.core_ops_Deref().as_ref() {
return Some("DerefMut");
}
None
}
@ -142,6 +145,7 @@ fn process_method_name(name: ast::Name) -> Option<(ast::Name, &'static str)> {
"index" => "index_mut",
"as_ref" => "as_mut",
"borrow" => "borrow_mut",
"deref" => "deref_mut",
_ => return None,
};
Some((name, new_name))
@ -258,6 +262,39 @@ impl core::convert::AsRef<i32> for Foo {
&self.0
}
}
"#,
);
check_assist(
generate_mut_trait_impl,
r#"
//- minicore: deref
struct Foo(i32);
impl core::ops::Deref$0 for Foo {
type Target = i32;
fn deref(&self) -> &Self::Target {
&self.0
}
}
"#,
r#"
struct Foo(i32);
$0impl core::ops::DerefMut for Foo {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl core::ops::Deref for Foo {
type Target = i32;
fn deref(&self) -> &Self::Target {
&self.0
}
}
"#,
);
}