-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add AsRef and Borrow for generate_mut_trait_impl #19917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
target, | ||
|edit| { | ||
edit.insert(target.start(), format!("$0{impl_def}\n\n{indent}")); | ||
}, | ||
) | ||
} | ||
|
||
fn get_famous(apply_trait: &str, famous: FamousDefs<'_, '_>) -> Option<hir::Trait> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be receive hir::Trait
though it's currently using &str
before this PR.
Like here:
rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs
Lines 35 to 44 in 5b852da
let impl_ = ctx.find_node_at_offset::<ast::Impl>()?; | |
let src_type = impl_.self_ty()?; | |
let ast_trait = impl_.trait_()?; | |
let module = ctx.sema.scope(impl_.syntax())?.module(); | |
let trait_ = resolve_target_trait(&ctx.sema, &impl_)?; | |
if trait_ != FamousDefs(&ctx.sema, module.krate()).core_convert_Into()? { | |
return None; | |
} |
- AsRef -> AsMut - Borrow -> BorrowMut Example ==================== ```rust //- minicore: as_ref struct Foo(i32); impl<T> core::convert::AsRef$0<i32> for Foo { fn as_ref(&self) -> &i32 { &self.0 } } ``` -> ```rust struct Foo(i32); $0impl<T> core::convert::AsMut<i32> for Foo { fn as_mut(&mut self) -> &mut i32 { &self.0 } } impl<T> core::convert::AsRef<i32> for Foo { fn as_ref(&self) -> &i32 { &self.0 } } ```
875c1a3
to
fc5dd8b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's still room for improvement here like:
- We want to replace
ted::*
API withSyntaxEditor
ones - When making implementations for
BorrowMut
, it might emit nameres error because we don't import it automatically. In fact, this is same for pre-existingIndexMut
😅
But anyway, I think this is an improvement over the current assist. Thanks!
Example
->