mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-13 15:33:53 +00:00
macro-generate froms
This commit is contained in:
parent
aea1f95a66
commit
a7c0336a75
@ -70,30 +70,7 @@ pub enum ModuleDef {
|
||||
Enum(Enum),
|
||||
Def(DefId),
|
||||
}
|
||||
//FIXME: change to from
|
||||
impl From<Module> for ModuleDef {
|
||||
fn from(it: Module) -> ModuleDef {
|
||||
ModuleDef::Module(it)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Function> for ModuleDef {
|
||||
fn from(it: Function) -> ModuleDef {
|
||||
ModuleDef::Function(it)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Struct> for ModuleDef {
|
||||
fn from(it: Struct) -> ModuleDef {
|
||||
ModuleDef::Struct(it)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Enum> for ModuleDef {
|
||||
fn from(it: Enum) -> ModuleDef {
|
||||
ModuleDef::Enum(it)
|
||||
}
|
||||
}
|
||||
impl_froms!(ModuleDef: Module, Function, Struct, Enum);
|
||||
|
||||
impl From<DefId> for ModuleDef {
|
||||
fn from(it: DefId) -> ModuleDef {
|
||||
|
@ -29,24 +29,7 @@ pub enum GenericDef {
|
||||
Enum(Enum),
|
||||
Def(DefId),
|
||||
}
|
||||
|
||||
impl From<Function> for GenericDef {
|
||||
fn from(func: Function) -> GenericDef {
|
||||
GenericDef::Function(func)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Struct> for GenericDef {
|
||||
fn from(s: Struct) -> GenericDef {
|
||||
GenericDef::Struct(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Enum> for GenericDef {
|
||||
fn from(e: Enum) -> GenericDef {
|
||||
GenericDef::Enum(e)
|
||||
}
|
||||
}
|
||||
impl_froms!(GenericDef: Function, Struct, Enum);
|
||||
|
||||
impl From<DefId> for GenericDef {
|
||||
fn from(def_id: DefId) -> GenericDef {
|
||||
|
@ -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;
|
||||
|
@ -168,18 +168,7 @@ pub enum AdtDef {
|
||||
Struct(Struct),
|
||||
Enum(Enum),
|
||||
}
|
||||
|
||||
impl From<Struct> for AdtDef {
|
||||
fn from(s: Struct) -> AdtDef {
|
||||
AdtDef::Struct(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Enum> for AdtDef {
|
||||
fn from(e: Enum) -> AdtDef {
|
||||
AdtDef::Enum(e)
|
||||
}
|
||||
}
|
||||
impl_froms!(AdtDef: Struct, Enum);
|
||||
|
||||
impl AdtDef {
|
||||
fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
|
||||
@ -701,24 +690,7 @@ pub enum TypableDef {
|
||||
Enum(Enum),
|
||||
Def(DefId),
|
||||
}
|
||||
|
||||
impl From<Function> for TypableDef {
|
||||
fn from(func: Function) -> TypableDef {
|
||||
TypableDef::Function(func)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Struct> for TypableDef {
|
||||
fn from(s: Struct) -> TypableDef {
|
||||
TypableDef::Struct(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Enum> for TypableDef {
|
||||
fn from(e: Enum) -> TypableDef {
|
||||
TypableDef::Enum(e)
|
||||
}
|
||||
}
|
||||
impl_froms!(TypableDef: Function, Struct, Enum);
|
||||
|
||||
impl From<DefId> for TypableDef {
|
||||
fn from(func: DefId) -> TypableDef {
|
||||
@ -763,12 +735,7 @@ pub enum VariantDef {
|
||||
Struct(Struct),
|
||||
Def(DefId), // EnumVariant
|
||||
}
|
||||
|
||||
impl From<Struct> for VariantDef {
|
||||
fn from(struct_: Struct) -> VariantDef {
|
||||
VariantDef::Struct(struct_)
|
||||
}
|
||||
}
|
||||
impl_froms!(VariantDef: Struct);
|
||||
|
||||
impl From<DefId> for VariantDef {
|
||||
fn from(def_id: DefId) -> VariantDef {
|
||||
|
Loading…
Reference in New Issue
Block a user