Merge #8467
8467: Adds impl Deref assist r=jhgg a=jhgg This PR adds a new `generate_deref` assist that automatically generates a deref impl for a given struct field. Check out this gif:  -- I have a few Q's: - [x] Should I write more tests, if so, what precisely should I test for? - [x] I have an inline question on line 65, can someone provide guidance? :) - [x] I can implement this for `ast::TupleField` too. But should it be a separate assist fn, or should I try and jam both into the `generate_deref`? - [x] I want to follow this up with an assist on `impl $0Deref for T {` which would automatically generate a `DerefMut` impl that mirrors the Deref as well, however, I could probably use some pointers on how to do that, since I'll have to reach into the ast of `fn deref` to grab the field that it's referencing for the `DerefMut` impl. Co-authored-by: jake <jh@discordapp.com>
This commit is contained in:
commit
3f432730df
6 changed files with 269 additions and 0 deletions
|
|
@ -113,6 +113,10 @@ impl FamousDefs<'_, '_> {
|
|||
self.find_module("core:iter")
|
||||
}
|
||||
|
||||
pub fn core_ops_Deref(&self) -> Option<Trait> {
|
||||
self.find_trait("core:ops:Deref")
|
||||
}
|
||||
|
||||
fn find_trait(&self, path: &str) -> Option<Trait> {
|
||||
match self.find_def(path)? {
|
||||
hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
|
||||
|
|
|
|||
|
|
@ -112,6 +112,12 @@ pub mod ops {
|
|||
type Output;
|
||||
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
|
||||
}
|
||||
|
||||
#[lang = "deref"]
|
||||
pub trait Deref {
|
||||
type Target: ?Sized;
|
||||
fn deref(&self) -> &Self::Target;
|
||||
}
|
||||
}
|
||||
|
||||
pub mod option {
|
||||
|
|
@ -141,3 +147,5 @@ mod return_keyword {}
|
|||
|
||||
/// Docs for prim_str
|
||||
mod prim_str {}
|
||||
|
||||
pub use core::ops;
|
||||
Loading…
Add table
Add a link
Reference in a new issue