Skip to content

Commit 3c400cd

Browse files
JakobDegenfacebook-github-bot
authored andcommitted
enum: T y p e s
Reviewed By: IanChilds Differential Revision: D73726009 fbshipit-source-id: 4b4aabc46fe1a089e4346ad1223f08e9d7f54b91
1 parent 2d82679 commit 3c400cd

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

starlark/src/values/types/enumeration/enum_type.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use crate::values::StarlarkValue;
5757
use crate::values::StringValue;
5858
use crate::values::Value;
5959
use crate::values::ValueLike;
60+
use crate::values::ValueTyped;
6061
use crate::values::enumeration::EnumValue;
6162
use crate::values::enumeration::matcher::EnumTypeMatcher;
6263
use crate::values::enumeration::ty_enum_type::TyEnumData;
@@ -169,11 +170,14 @@ pub type EnumType<'v> = EnumTypeGen<Value<'v>>;
169170
pub type FrozenEnumType = EnumTypeGen<FrozenValue>;
170171

171172
impl<'v> EnumType<'v> {
172-
pub(crate) fn new(elements: Vec<StringValue<'v>>, heap: &'v Heap) -> crate::Result<Value<'v>> {
173+
pub(crate) fn new(
174+
elements: Vec<StringValue<'v>>,
175+
heap: &'v Heap,
176+
) -> crate::Result<ValueTyped<'v, EnumType<'v>>> {
173177
// We are constructing the enum and all elements in one go.
174178
// They both point at each other, which adds to the complexity.
175179
let id = TypeInstanceId::r#gen();
176-
let typ = heap.alloc(EnumType {
180+
let typ = heap.alloc_typed(EnumType {
177181
id,
178182
ty_enum_data: OnceCell::new(),
179183
elements: UnsafeCell::new(SmallMap::new()),
@@ -183,7 +187,7 @@ impl<'v> EnumType<'v> {
183187
for (i, x) in elements.iter().enumerate() {
184188
let v = heap.alloc(EnumValue {
185189
id,
186-
typ,
190+
typ: typ.to_value(),
187191
index: i as i32,
188192
value: x.to_value(),
189193
});
@@ -195,10 +199,9 @@ impl<'v> EnumType<'v> {
195199
}
196200

197201
// Here we tie the cycle
198-
let t = typ.downcast_ref::<EnumType>().unwrap();
199202
unsafe {
200203
// SAFETY: we own unique reference to `t`.
201-
*t.elements.get() = res;
204+
*typ.elements.get() = res;
202205
}
203206
Ok(typ)
204207
}

starlark/src/values/types/enumeration/globals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn register_enum(builder: &mut GlobalsBuilder) {
5252
heap: &'v Heap,
5353
) -> starlark::Result<Value<'v>> {
5454
// Every Value must either be a field or a value (the type)
55-
EnumType::new(args.items, heap)
55+
Ok(EnumType::new(args.items, heap)?.to_value())
5656
}
5757
}
5858

0 commit comments

Comments
 (0)