vortex_expr/scope.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::dtype::DType;
5
6/// The context an [`Expression`](crate::Expression) is bound against.
7///
8/// Today a scope is just the dtype that [`root`](crate::root) resolves to. It is an opaque
9/// struct rather than a bare [`DType`] so that lexical bindings can be added later without changing
10/// [`Expression::bind_scope`](crate::Expression::bind_scope)'s signature.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct Scope {
13 root: DType,
14}
15
16impl Scope {
17 /// Create a scope in which `root` resolves to the given dtype.
18 pub fn new(root: DType) -> Self {
19 Self { root }
20 }
21
22 /// The dtype that `root` resolves to.
23 pub fn root(&self) -> &DType {
24 &self.root
25 }
26}
27
28impl From<DType> for Scope {
29 fn from(root: DType) -> Self {
30 Self::new(root)
31 }
32}
33
34#[cfg(test)]
35mod tests {
36 use vortex_array::dtype::Nullability;
37
38 use super::*;
39
40 #[test]
41 fn root_round_trips() {
42 let dtype = DType::Bool(Nullability::Nullable);
43 assert_eq!(Scope::new(dtype.clone()).root(), &dtype);
44 assert_eq!(Scope::from(dtype.clone()).root(), &dtype);
45 }
46}