vortex_array/expr/scope.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use crate::dtype::DType;
5
6/// The context an [`Expression`](crate::expr::Expression) is bound against.
7///
8/// Today a scope is just the dtype that [`root`](crate::expr::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::expr::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 super::*;
37 use crate::dtype::Nullability;
38
39 #[test]
40 fn root_round_trips() {
41 let dtype = DType::Bool(Nullability::Nullable);
42 assert_eq!(Scope::new(dtype.clone()).root(), &dtype);
43 assert_eq!(Scope::from(dtype.clone()).root(), &dtype);
44 }
45}