starlark/values/typing/type_compiled/
matcher.rs1use std::fmt::Debug;
19
20use allocative::Allocative;
21
22use crate::typing::custom::TyCustom;
23use crate::values::typing::type_compiled::alloc::TypeMatcherAlloc;
24use crate::values::typing::type_compiled::type_matcher_factory::TypeMatcherFactory;
25use crate::values::Value;
26
27pub trait TypeMatcher: Allocative + Debug + Clone + Sized + Send + Sync + 'static {
30 fn matches(&self, value: Value) -> bool;
32 fn is_wildcard(&self) -> bool {
34 false
35 }
36}
37
38pub(crate) trait TypeMatcherDyn: Debug + Allocative + Send + Sync + 'static {
39 fn matches_dyn(&self, value: Value) -> bool;
40 fn is_wildcard_dyn(&self) -> bool;
41
42 fn to_box(&self) -> TypeMatcherBox;
43}
44
45impl<T: TypeMatcher> TypeMatcherDyn for T {
46 fn matches_dyn(&self, value: Value) -> bool {
47 TypeMatcher::matches(self, value)
48 }
49
50 fn is_wildcard_dyn(&self) -> bool {
51 TypeMatcher::is_wildcard(self)
52 }
53
54 fn to_box(&self) -> TypeMatcherBox {
55 TypeMatcherBox::new(self.clone())
56 }
57}
58
59#[derive(Debug, Allocative)]
60pub(crate) struct TypeMatcherBox(pub(crate) Box<dyn TypeMatcherDyn>);
61
62impl TypeMatcherBox {
63 pub(crate) fn new<T: TypeMatcher>(matcher: T) -> TypeMatcherBox {
64 TypeMatcherBox(Box::new(matcher))
65 }
66}
67
68impl Clone for TypeMatcherBox {
69 fn clone(&self) -> Self {
70 self.0.to_box()
71 }
72}
73
74impl TypeMatcher for TypeMatcherBox {
75 fn matches(&self, value: Value) -> bool {
76 self.0.matches_dyn(value)
77 }
78
79 fn is_wildcard(&self) -> bool {
80 self.0.is_wildcard_dyn()
81 }
82}
83
84pub(crate) struct TypeMatcherBoxAlloc;
86
87impl TypeMatcherAlloc for TypeMatcherBoxAlloc {
88 type Result = TypeMatcherBox;
89
90 fn alloc<T: TypeMatcher>(self, matcher: T) -> Self::Result {
91 TypeMatcherBox::new(matcher)
92 }
93
94 fn custom(self, custom: &TyCustom) -> Self::Result {
95 custom.matcher_with_box()
96 }
97
98 fn from_type_matcher_factory(self, factory: &TypeMatcherFactory) -> Self::Result {
99 factory.factory.matcher_box()
100 }
101}