starlark/values/typing/type_compiled/
matcher.rs

1/*
2 * Copyright 2019 The Starlark in Rust Authors.
3 * Copyright (c) Facebook, Inc. and its affiliates.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use 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
27/// Runtime type matcher. E.g. when `isinstance(1, int)` is called,
28/// implementation of `TypeMatcher` for `int` is used.
29pub trait TypeMatcher: Allocative + Debug + Clone + Sized + Send + Sync + 'static {
30    /// Check if the value matches the type.
31    fn matches(&self, value: Value) -> bool;
32    /// True if this matcher matches any value.
33    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
84/// Type allocator which allocates `TypeMatcher` into `TypeMatcherBox`.
85pub(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}