starlark/values/typing/
iter.rs

1/*
2 * Copyright 2018 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::marker::PhantomData;
19
20use allocative::Allocative;
21use starlark_derive::starlark_value;
22use starlark_derive::NoSerialize;
23use starlark_derive::ProvidesStaticType;
24
25use crate as starlark;
26use crate::typing::Ty;
27use crate::values::layout::avalue::alloc_static;
28use crate::values::layout::avalue::AValueBasic;
29use crate::values::layout::avalue::AValueImpl;
30use crate::values::layout::heap::repr::AValueRepr;
31use crate::values::type_repr::StarlarkTypeRepr;
32use crate::values::AllocFrozenValue;
33use crate::values::FrozenHeap;
34use crate::values::FrozenValue;
35use crate::values::StarlarkValue;
36
37enum NonInstantiable {}
38
39/// `StarlarkTypeRepr` for iterable types.
40pub struct StarlarkIter<T: StarlarkTypeRepr>(PhantomData<T>, NonInstantiable);
41
42impl<T: StarlarkTypeRepr> StarlarkTypeRepr for StarlarkIter<T> {
43    type Canonical = StarlarkIter<T::Canonical>;
44
45    fn starlark_type_repr() -> Ty {
46        Ty::iter(T::starlark_type_repr())
47    }
48}
49
50#[derive(
51    Debug,
52    derive_more::Display,
53    Allocative,
54    ProvidesStaticType,
55    NoSerialize
56)]
57#[display("{}", Self::TYPE)]
58pub(crate) struct TypingIterable;
59
60#[starlark_value(type = "typing.Iterable")]
61impl<'v> StarlarkValue<'v> for TypingIterable {
62    fn eval_type(&self) -> Option<Ty> {
63        Some(Ty::iter(Ty::any()))
64    }
65
66    // TODO(nga): support `[]`.
67}
68
69impl AllocFrozenValue for TypingIterable {
70    fn alloc_frozen_value(self, _heap: &FrozenHeap) -> FrozenValue {
71        static ANY: AValueRepr<AValueImpl<'static, AValueBasic<TypingIterable>>> =
72            alloc_static(TypingIterable);
73
74        FrozenValue::new_repr(&ANY)
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use crate::assert;
81
82    #[test]
83    fn test_iterable_runtime() {
84        assert::is_true("isinstance([1, 2, 3], typing.Iterable)");
85        assert::is_true("isinstance((1, 2, 3), typing.Iterable)");
86        assert::is_true("isinstance(range(10), typing.Iterable)");
87        assert::is_false("isinstance('', typing.Iterable)");
88        assert::is_false("isinstance(1, typing.Iterable)");
89    }
90
91    #[test]
92    fn test_iterable_compile_time_pass() {
93        assert::pass(
94            r#"
95def foo(x: typing.Iterable):
96    pass
97
98def bar():
99    foo([1, 2, 3])
100"#,
101        );
102    }
103
104    #[test]
105    fn test_iterable_compile_time_fail() {
106        assert::fail(
107            r#"
108def foo(x: typing.Iterable):
109    pass
110
111def bar():
112    foo(1)
113"#,
114            "Expected type `typing.Iterable`",
115        );
116    }
117}