generalize impl_froms to nested enums

This commit is contained in:
Aleksey Kladov 2019-09-13 00:31:04 +03:00
parent 45117c6388
commit bcf30d389c
4 changed files with 26 additions and 58 deletions

View file

@ -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<Struct> for ModuleDef {
fn from(it: Struct) -> ModuleDef {
ModuleDef::AdtDef(AdtDef::Struct(it))
}
}
impl From<Enum> for ModuleDef {
fn from(it: Enum) -> ModuleDef {
ModuleDef::AdtDef(AdtDef::Enum(it))
}
}
impl From<Union> for ModuleDef {
fn from(it: Union) -> ModuleDef {
ModuleDef::AdtDef(AdtDef::Union(it))
}
}
pub enum ModuleSource {
SourceFile(ast::SourceFile),
Module(ast::Module),

View file

@ -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<Struct> for GenericDef {
fn from(it: Struct) -> GenericDef {
GenericDef::AdtDef(AdtDef::Struct(it))
}
}
impl From<Enum> for GenericDef {
fn from(it: Enum) -> GenericDef {
GenericDef::AdtDef(AdtDef::Enum(it))
}
}
impl From<Union> 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(

View file

@ -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))
}
}
)*)?
)*
}
}

View file

@ -611,25 +611,15 @@ pub enum TypableDef {
Static(Static),
BuiltinType(BuiltinType),
}
impl_froms!(TypableDef: Function, AdtDef, EnumVariant, TypeAlias, Const, Static, BuiltinType);
impl From<Struct> for TypableDef {
fn from(it: Struct) -> TypableDef {
TypableDef::AdtDef(AdtDef::Struct(it))
}
}
impl From<Enum> for TypableDef {
fn from(it: Enum) -> TypableDef {
TypableDef::AdtDef(AdtDef::Enum(it))
}
}
impl From<Union> 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<ModuleDef> for Option<TypableDef> {
fn from(def: ModuleDef) -> Option<TypableDef> {