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