Skip to content

Commit 28ac05d

Browse files
authored
Merge pull request #20256 from A4-Tacks/gen-mut-trait-deref
Add Deref -> DerefMut for generate_mut_trait_impl
2 parents ba425cc + dfd8434 commit 28ac05d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

crates/ide-assists/src/handlers/generate_mut_trait_impl.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ fn get_trait_mut(apply_trait: &hir::Trait, famous: FamousDefs<'_, '_>) -> Option
134134
if trait_ == famous.core_borrow_Borrow().as_ref() {
135135
return Some("BorrowMut");
136136
}
137+
if trait_ == famous.core_ops_Deref().as_ref() {
138+
return Some("DerefMut");
139+
}
137140
None
138141
}
139142

@@ -142,6 +145,7 @@ fn process_method_name(name: ast::Name) -> Option<(ast::Name, &'static str)> {
142145
"index" => "index_mut",
143146
"as_ref" => "as_mut",
144147
"borrow" => "borrow_mut",
148+
"deref" => "deref_mut",
145149
_ => return None,
146150
};
147151
Some((name, new_name))
@@ -258,6 +262,39 @@ impl core::convert::AsRef<i32> for Foo {
258262
&self.0
259263
}
260264
}
265+
"#,
266+
);
267+
268+
check_assist(
269+
generate_mut_trait_impl,
270+
r#"
271+
//- minicore: deref
272+
struct Foo(i32);
273+
274+
impl core::ops::Deref$0 for Foo {
275+
type Target = i32;
276+
277+
fn deref(&self) -> &Self::Target {
278+
&self.0
279+
}
280+
}
281+
"#,
282+
r#"
283+
struct Foo(i32);
284+
285+
$0impl core::ops::DerefMut for Foo {
286+
fn deref_mut(&mut self) -> &mut Self::Target {
287+
&mut self.0
288+
}
289+
}
290+
291+
impl core::ops::Deref for Foo {
292+
type Target = i32;
293+
294+
fn deref(&self) -> &Self::Target {
295+
&self.0
296+
}
297+
}
261298
"#,
262299
);
263300
}

0 commit comments

Comments
 (0)