vortex_array/expr/
view.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Deref;
5
6use vortex_error::VortexExpect;
7
8use crate::expr::Expression;
9use crate::expr::VTable;
10
11/// A view over an [`Expression`] with an associated vtable, allowing typed access to the
12/// expression's instance data.
13pub struct ExpressionView<'a, V: VTable> {
14    expression: &'a Expression,
15    vtable: &'a V,
16    data: &'a V::Instance,
17}
18
19impl<'a, V: VTable> ExpressionView<'a, V> {
20    /// Wrap up the given expression as an [`ExpressionView`] of the specified vtable type.
21    ///
22    /// # Panics
23    ///
24    /// Panics if the expression cannot be downcast to the specified vtable type.
25    #[inline]
26    pub fn new(expression: &'a Expression) -> Self {
27        Self::maybe_new(expression).vortex_expect("Failed to downcast expression")
28    }
29
30    /// Attempts to wrap up the given expression as an [`ExpressionView`] of the specified vtable type.
31    #[inline]
32    pub fn maybe_new(expression: &'a Expression) -> Option<Self> {
33        let vtable = expression.vtable().as_opt::<V>()?;
34        let data = expression.data().downcast_ref::<V::Instance>()?;
35        Some(Self {
36            expression,
37            vtable,
38            data,
39        })
40    }
41}
42
43impl<'a, V: VTable> ExpressionView<'a, V> {
44    /// Returns the vtable for this expression.
45    #[inline(always)]
46    pub fn vtable(&self) -> &'a V {
47        self.vtable
48    }
49
50    /// Returns the instance data for this expression.
51    #[inline(always)]
52    pub fn data(&self) -> &'a V::Instance {
53        self.data
54    }
55}
56
57impl<'a, V: VTable> Deref for ExpressionView<'a, V> {
58    type Target = Expression;
59
60    fn deref(&self) -> &Self::Target {
61        self.expression
62    }
63}