From bcf30d389c3744ab517372431eb7e1e1dfcc50df Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 13 Sep 2019 00:31:04 +0300 Subject: generalize impl_froms to nested enums --- crates/ra_hir/src/code_model.rs | 20 +------------------- crates/ra_hir/src/generics.rs | 27 ++++++++------------------- crates/ra_hir/src/lib.rs | 9 ++++++++- crates/ra_hir/src/ty/lower.rs | 28 +++++++++------------------- 4 files changed, 26 insertions(+), 58 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 026753c9f..a10e795ce 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -139,7 +139,7 @@ pub enum ModuleDef { impl_froms!( ModuleDef: Module, Function, - AdtDef, + AdtDef(Struct, Enum, Union), EnumVariant, Const, Static, @@ -148,24 +148,6 @@ impl_froms!( BuiltinType ); -impl From for ModuleDef { - fn from(it: Struct) -> ModuleDef { - ModuleDef::AdtDef(AdtDef::Struct(it)) - } -} - -impl From for ModuleDef { - fn from(it: Enum) -> ModuleDef { - ModuleDef::AdtDef(AdtDef::Enum(it)) - } -} - -impl From for ModuleDef { - fn from(it: Union) -> ModuleDef { - ModuleDef::AdtDef(AdtDef::Union(it)) - } -} - pub enum ModuleSource { SourceFile(ast::SourceFile), Module(ast::Module), diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index 0e89941f3..74435c20a 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -55,25 +55,14 @@ pub enum GenericDef { // can, and this makes some code easier to write EnumVariant(EnumVariant), } -impl_froms!(GenericDef: Function, AdtDef, Trait, TypeAlias, ImplBlock, EnumVariant); - -impl From for GenericDef { - fn from(it: Struct) -> GenericDef { - GenericDef::AdtDef(AdtDef::Struct(it)) - } -} - -impl From for GenericDef { - fn from(it: Enum) -> GenericDef { - GenericDef::AdtDef(AdtDef::Enum(it)) - } -} - -impl From for GenericDef { - fn from(it: Union) -> GenericDef { - GenericDef::AdtDef(AdtDef::Union(it)) - } -} +impl_froms!( + GenericDef: Function, + AdtDef(Struct, Enum, Union), + Trait, + TypeAlias, + ImplBlock, + EnumVariant +); impl GenericParams { pub(crate) fn generic_params_query( diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 2fe1a658e..639e97c3d 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -8,13 +8,20 @@ //! applied. So, the relation between syntax and HIR is many-to-one. macro_rules! impl_froms { - ($e:ident: $($v:ident),*) => { + ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { $( impl From<$v> for $e { fn from(it: $v) -> $e { $e::$v(it) } } + $($( + impl From<$sv> for $e { + fn from(it: $sv) -> $e { + $e::$v($v::$sv(it)) + } + } + )*)? )* } } diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 587dc12ee..105c893f0 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -611,25 +611,15 @@ pub enum TypableDef { Static(Static), BuiltinType(BuiltinType), } -impl_froms!(TypableDef: Function, AdtDef, EnumVariant, TypeAlias, Const, Static, BuiltinType); - -impl From for TypableDef { - fn from(it: Struct) -> TypableDef { - TypableDef::AdtDef(AdtDef::Struct(it)) - } -} - -impl From for TypableDef { - fn from(it: Enum) -> TypableDef { - TypableDef::AdtDef(AdtDef::Enum(it)) - } -} - -impl From for TypableDef { - fn from(it: Union) -> TypableDef { - TypableDef::AdtDef(AdtDef::Union(it)) - } -} +impl_froms!( + TypableDef: Function, + AdtDef(Struct, Enum, Union), + EnumVariant, + TypeAlias, + Const, + Static, + BuiltinType +); impl From for Option { fn from(def: ModuleDef) -> Option { -- cgit v1.2.3