rust-analyzer/crates/ra_hir/src/adt.rs

55 lines
1.5 KiB
Rust
Raw Normal View History

//! This module contains the implementation details of the HIR for ADTs, i.e.
//! structs and enums (and unions).
use std::sync::Arc;
2019-10-31 13:40:36 +00:00
use hir_def::adt::VariantData;
use crate::{
2019-10-31 13:40:36 +00:00
db::{DefDatabase, HirDatabase},
EnumVariant, Module, Name, Struct, StructField,
};
impl Struct {
2019-07-05 16:02:32 +00:00
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
2019-10-31 13:40:36 +00:00
db.struct_data(self.id).variant_data.clone()
}
}
2019-01-25 17:32:34 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum VariantDef {
Struct(Struct),
EnumVariant(EnumVariant),
}
impl_froms!(VariantDef: Struct, EnumVariant);
impl VariantDef {
2019-07-12 16:56:18 +00:00
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
match self {
VariantDef::Struct(it) => it.fields(db),
VariantDef::EnumVariant(it) => it.fields(db),
}
}
2019-10-09 11:59:47 +00:00
pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
2019-01-25 17:32:34 +00:00
match self {
VariantDef::Struct(it) => it.field(db, name),
VariantDef::EnumVariant(it) => it.field(db, name),
}
}
2019-10-09 11:59:47 +00:00
pub fn module(self, db: &impl HirDatabase) -> Module {
match self {
VariantDef::Struct(it) => it.module(db),
VariantDef::EnumVariant(it) => it.module(db),
}
}
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
2019-01-25 17:32:34 +00:00
match self {
VariantDef::Struct(it) => it.variant_data(db),
VariantDef::EnumVariant(it) => it.variant_data(db),
}
}
}