aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/adt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/adt.rs')
-rw-r--r--crates/ra_hir/src/adt.rs55
1 files changed, 52 insertions, 3 deletions
diff --git a/crates/ra_hir/src/adt.rs b/crates/ra_hir/src/adt.rs
index 602e7db74..d30390f25 100644
--- a/crates/ra_hir/src/adt.rs
+++ b/crates/ra_hir/src/adt.rs
@@ -4,7 +4,7 @@ use ra_db::Cancelable;
4use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode}; 4use ra_syntax::ast::{self, NameOwner, StructFlavor, AstNode};
5 5
6use crate::{ 6use crate::{
7 DefId, Name, AsName, Struct, Enum, VariantData, StructField, HirDatabase, DefKind, 7 DefId, Name, AsName, Struct, Enum, HirDatabase, DefKind,
8 type_ref::TypeRef, 8 type_ref::TypeRef,
9}; 9};
10 10
@@ -12,6 +12,10 @@ impl Struct {
12 pub(crate) fn new(def_id: DefId) -> Self { 12 pub(crate) fn new(def_id: DefId) -> Self {
13 Struct { def_id } 13 Struct { def_id }
14 } 14 }
15
16 pub(crate) fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> {
17 Ok(db.struct_data(self.def_id)?.variant_data.clone())
18 }
15} 19}
16 20
17#[derive(Debug, Clone, PartialEq, Eq)] 21#[derive(Debug, Clone, PartialEq, Eq)]
@@ -83,6 +87,51 @@ impl EnumData {
83 } 87 }
84} 88}
85 89
90/// A single field of an enum variant or struct
91#[derive(Debug, Clone, PartialEq, Eq)]
92pub struct StructField {
93 pub(crate) name: Name,
94 pub(crate) type_ref: TypeRef,
95}
96
97/// Fields of an enum variant or struct
98#[derive(Debug, Clone, PartialEq, Eq)]
99pub enum VariantData {
100 Struct(Vec<StructField>),
101 Tuple(Vec<StructField>),
102 Unit,
103}
104
105impl VariantData {
106 pub fn fields(&self) -> &[StructField] {
107 match self {
108 VariantData::Struct(fields) | VariantData::Tuple(fields) => fields,
109 _ => &[],
110 }
111 }
112
113 pub fn is_struct(&self) -> bool {
114 match self {
115 VariantData::Struct(..) => true,
116 _ => false,
117 }
118 }
119
120 pub fn is_tuple(&self) -> bool {
121 match self {
122 VariantData::Tuple(..) => true,
123 _ => false,
124 }
125 }
126
127 pub fn is_unit(&self) -> bool {
128 match self {
129 VariantData::Unit => true,
130 _ => false,
131 }
132 }
133}
134
86impl VariantData { 135impl VariantData {
87 fn new(flavor: StructFlavor) -> Self { 136 fn new(flavor: StructFlavor) -> Self {
88 match flavor { 137 match flavor {
@@ -114,7 +163,7 @@ impl VariantData {
114 pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> { 163 pub(crate) fn get_field_type_ref(&self, field_name: &Name) -> Option<&TypeRef> {
115 self.fields() 164 self.fields()
116 .iter() 165 .iter()
117 .find(|f| f.name() == field_name) 166 .find(|f| f.name == *field_name)
118 .map(|f| f.type_ref()) 167 .map(|f| &f.type_ref)
119 } 168 }
120} 169}