diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index a094268caed..4b79358e4b8 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -70,30 +70,7 @@ pub enum ModuleDef { Enum(Enum), Def(DefId), } -//FIXME: change to from -impl From for ModuleDef { - fn from(it: Module) -> ModuleDef { - ModuleDef::Module(it) - } -} - -impl From for ModuleDef { - fn from(it: Function) -> ModuleDef { - ModuleDef::Function(it) - } -} - -impl From for ModuleDef { - fn from(it: Struct) -> ModuleDef { - ModuleDef::Struct(it) - } -} - -impl From for ModuleDef { - fn from(it: Enum) -> ModuleDef { - ModuleDef::Enum(it) - } -} +impl_froms!(ModuleDef: Module, Function, Struct, Enum); impl From for ModuleDef { fn from(it: DefId) -> ModuleDef { diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index df23871c76a..a8cacbb4ad7 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -29,24 +29,7 @@ pub enum GenericDef { Enum(Enum), Def(DefId), } - -impl From for GenericDef { - fn from(func: Function) -> GenericDef { - GenericDef::Function(func) - } -} - -impl From for GenericDef { - fn from(s: Struct) -> GenericDef { - GenericDef::Struct(s) - } -} - -impl From for GenericDef { - fn from(e: Enum) -> GenericDef { - GenericDef::Enum(e) - } -} +impl_froms!(GenericDef: Function, Struct, Enum); impl From for GenericDef { fn from(def_id: DefId) -> GenericDef { diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 5d63718924e..33438509c72 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -5,6 +5,18 @@ //! to a particular crate instance. That is, it has cfg flags and features //! applied. So, the relation between syntax and HIR is many-to-one. +macro_rules! impl_froms { + ($e:ident: $($v:ident), *) => { + $( + impl From<$v> for $e { + fn from(it: $v) -> $e { + $e::$v(it) + } + } + )* + } +} + pub mod db; #[cfg(test)] mod mock; diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index b8189966148..fcf8a1dbb09 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -168,18 +168,7 @@ pub enum AdtDef { Struct(Struct), Enum(Enum), } - -impl From for AdtDef { - fn from(s: Struct) -> AdtDef { - AdtDef::Struct(s) - } -} - -impl From for AdtDef { - fn from(e: Enum) -> AdtDef { - AdtDef::Enum(e) - } -} +impl_froms!(AdtDef: Struct, Enum); impl AdtDef { fn krate(self, db: &impl HirDatabase) -> Option { @@ -701,24 +690,7 @@ pub enum TypableDef { Enum(Enum), Def(DefId), } - -impl From for TypableDef { - fn from(func: Function) -> TypableDef { - TypableDef::Function(func) - } -} - -impl From for TypableDef { - fn from(s: Struct) -> TypableDef { - TypableDef::Struct(s) - } -} - -impl From for TypableDef { - fn from(e: Enum) -> TypableDef { - TypableDef::Enum(e) - } -} +impl_froms!(TypableDef: Function, Struct, Enum); impl From for TypableDef { fn from(func: DefId) -> TypableDef { @@ -763,12 +735,7 @@ pub enum VariantDef { Struct(Struct), Def(DefId), // EnumVariant } - -impl From for VariantDef { - fn from(struct_: Struct) -> VariantDef { - VariantDef::Struct(struct_) - } -} +impl_froms!(VariantDef: Struct); impl From for VariantDef { fn from(def_id: DefId) -> VariantDef {