starlark/values/typing/type_compiled/
factory.rs1use crate::typing::custom::TyCustom;
19use crate::typing::Ty;
20use crate::values::layout::avalue::AValueBasic;
21use crate::values::layout::avalue::AValueImpl;
22use crate::values::layout::heap::repr::AValueRepr;
23use crate::values::typing::type_compiled::alloc::TypeMatcherAlloc;
24use crate::values::typing::type_compiled::compiled::TypeCompiled;
25use crate::values::typing::type_compiled::compiled::TypeCompiledImplAsStarlarkValue;
26use crate::values::typing::type_compiled::matcher::TypeMatcher;
27use crate::values::typing::type_compiled::matchers::IsAny;
28use crate::values::typing::type_compiled::matchers::IsBool;
29use crate::values::typing::type_compiled::matchers::IsInt;
30use crate::values::typing::type_compiled::matchers::IsNone;
31use crate::values::typing::type_compiled::matchers::IsStr;
32use crate::values::typing::type_compiled::type_matcher_factory::TypeMatcherFactory;
33use crate::values::FrozenValue;
34use crate::values::Heap;
35use crate::values::Value;
36
37pub struct TypeCompiledFactory<'a, 'v> {
39 heap: &'v Heap,
40 ty: &'a Ty,
41}
42
43impl<'a, 'v> TypeMatcherAlloc for TypeCompiledFactory<'a, 'v> {
44 type Result = TypeCompiled<Value<'v>>;
45
46 fn alloc<T: TypeMatcher>(self, matcher: T) -> Self::Result {
47 TypeCompiled::alloc(matcher, self.ty.clone(), self.heap)
48 }
49
50 fn custom(self, custom: &TyCustom) -> Self::Result {
51 custom.matcher_with_type_compiled_factory(self)
52 }
53
54 fn from_type_matcher_factory(self, factory: &TypeMatcherFactory) -> Self::Result {
55 factory.factory.type_compiled(self)
56 }
57
58 fn any(self) -> TypeCompiled<Value<'v>> {
59 if self.ty == &Ty::any() {
60 TypeCompiled::any().to_value()
61 } else {
62 self.alloc(IsAny)
63 }
64 }
65
66 fn none(self) -> TypeCompiled<Value<'v>> {
67 if self.ty == &Ty::none() {
68 static IS_NONE: AValueRepr<
69 AValueImpl<'static, AValueBasic<TypeCompiledImplAsStarlarkValue<IsNone>>>,
70 > = TypeCompiledImplAsStarlarkValue::alloc_static(IsNone, Ty::none());
71
72 TypeCompiled::unchecked_new(FrozenValue::new_repr(&IS_NONE).to_value())
73 } else {
74 self.alloc(IsNone)
75 }
76 }
77
78 fn bool(self) -> TypeCompiled<Value<'v>> {
79 if self.ty == &Ty::bool() {
80 static IS_BOOL: AValueRepr<
81 AValueImpl<'static, AValueBasic<TypeCompiledImplAsStarlarkValue<IsBool>>>,
82 > = TypeCompiledImplAsStarlarkValue::alloc_static(IsBool, Ty::bool());
83
84 TypeCompiled::unchecked_new(FrozenValue::new_repr(&IS_BOOL).to_value())
85 } else {
86 self.alloc(IsBool)
87 }
88 }
89
90 fn int(self) -> TypeCompiled<Value<'v>> {
91 if self.ty == &Ty::int() {
92 static IS_INT: AValueRepr<
93 AValueImpl<AValueBasic<TypeCompiledImplAsStarlarkValue<IsInt>>>,
94 > = TypeCompiledImplAsStarlarkValue::alloc_static(IsInt, Ty::int());
95
96 TypeCompiled::unchecked_new(FrozenValue::new_repr(&IS_INT).to_value())
97 } else {
98 self.alloc(IsInt)
99 }
100 }
101
102 fn str(self) -> TypeCompiled<Value<'v>> {
103 if self.ty == &Ty::string() {
104 static IS_STRING: AValueRepr<
105 AValueImpl<'static, AValueBasic<TypeCompiledImplAsStarlarkValue<IsStr>>>,
106 > = TypeCompiledImplAsStarlarkValue::alloc_static(IsStr, Ty::string());
107
108 TypeCompiled::unchecked_new(FrozenValue::new_repr(&IS_STRING).to_value())
109 } else {
110 self.alloc(IsStr)
111 }
112 }
113}
114
115impl<'a, 'v> TypeCompiledFactory<'a, 'v> {
116 pub(crate) fn alloc_ty(ty: &'a Ty, heap: &'v Heap) -> TypeCompiled<Value<'v>> {
117 TypeCompiledFactory { heap, ty }.ty(ty)
118 }
119}