mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
macro-generate froms
This commit is contained in:
parent
aea1f95a66
commit
a7c0336a75
4 changed files with 17 additions and 78 deletions
|
@ -70,30 +70,7 @@ pub enum ModuleDef {
|
||||||
Enum(Enum),
|
Enum(Enum),
|
||||||
Def(DefId),
|
Def(DefId),
|
||||||
}
|
}
|
||||||
//FIXME: change to from
|
impl_froms!(ModuleDef: Module, Function, Struct, Enum);
|
||||||
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 From<DefId> for ModuleDef {
|
impl From<DefId> for ModuleDef {
|
||||||
fn from(it: DefId) -> ModuleDef {
|
fn from(it: DefId) -> ModuleDef {
|
||||||
|
|
|
@ -29,24 +29,7 @@ pub enum GenericDef {
|
||||||
Enum(Enum),
|
Enum(Enum),
|
||||||
Def(DefId),
|
Def(DefId),
|
||||||
}
|
}
|
||||||
|
impl_froms!(GenericDef: Function, Struct, Enum);
|
||||||
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 From<DefId> for GenericDef {
|
impl From<DefId> for GenericDef {
|
||||||
fn from(def_id: DefId) -> GenericDef {
|
fn from(def_id: DefId) -> GenericDef {
|
||||||
|
|
|
@ -5,6 +5,18 @@
|
||||||
//! to a particular crate instance. That is, it has cfg flags and features
|
//! 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.
|
//! 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;
|
pub mod db;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod mock;
|
mod mock;
|
||||||
|
|
|
@ -168,18 +168,7 @@ pub enum AdtDef {
|
||||||
Struct(Struct),
|
Struct(Struct),
|
||||||
Enum(Enum),
|
Enum(Enum),
|
||||||
}
|
}
|
||||||
|
impl_froms!(AdtDef: Struct, 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 AdtDef {
|
impl AdtDef {
|
||||||
fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
|
fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
|
||||||
|
@ -701,24 +690,7 @@ pub enum TypableDef {
|
||||||
Enum(Enum),
|
Enum(Enum),
|
||||||
Def(DefId),
|
Def(DefId),
|
||||||
}
|
}
|
||||||
|
impl_froms!(TypableDef: Function, Struct, Enum);
|
||||||
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 From<DefId> for TypableDef {
|
impl From<DefId> for TypableDef {
|
||||||
fn from(func: DefId) -> TypableDef {
|
fn from(func: DefId) -> TypableDef {
|
||||||
|
@ -763,12 +735,7 @@ pub enum VariantDef {
|
||||||
Struct(Struct),
|
Struct(Struct),
|
||||||
Def(DefId), // EnumVariant
|
Def(DefId), // EnumVariant
|
||||||
}
|
}
|
||||||
|
impl_froms!(VariantDef: Struct);
|
||||||
impl From<Struct> for VariantDef {
|
|
||||||
fn from(struct_: Struct) -> VariantDef {
|
|
||||||
VariantDef::Struct(struct_)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<DefId> for VariantDef {
|
impl From<DefId> for VariantDef {
|
||||||
fn from(def_id: DefId) -> VariantDef {
|
fn from(def_id: DefId) -> VariantDef {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue