11// SPDX-License-Identifier: Apache-2.0
22// SPDX-FileCopyrightText: Copyright the Vortex contributors
33
4- use std:: any:: type_name;
54use std:: ops:: Deref ;
65
7- use vortex_error:: { VortexExpect , VortexResult , vortex_err } ;
6+ use vortex_error:: VortexExpect ;
87
98use crate :: { Expression , VTable } ;
109
1110/// A view over an [`Expression`] with an associated vtable, allowing typed access to the
1211/// expression's instance data.
1312pub struct ExpressionView < ' a , V : VTable > {
1413 expression : & ' a Expression ,
14+ vtable : & ' a V ,
1515 data : & ' a V :: Instance ,
1616}
1717
@@ -23,35 +23,29 @@ impl<'a, V: VTable> ExpressionView<'a, V> {
2323 /// Panics if the expression cannot be downcast to the specified vtable type.
2424 #[ inline]
2525 pub fn new ( expression : & ' a Expression ) -> Self {
26- Self :: try_new ( expression) . vortex_expect ( "Failed to downcast expression" )
26+ Self :: maybe_new ( expression) . vortex_expect ( "Failed to downcast expression" )
2727 }
2828
2929 /// Attempts to wrap up the given expression as an [`ExpressionView`] of the specified vtable type.
3030 #[ inline]
31- pub fn try_new ( expression : & ' a Expression ) -> VortexResult < Self > {
32- expression. vtable ( ) . as_opt :: < V > ( ) . ok_or_else ( || {
33- vortex_err ! (
34- "Failed to downcast {} to {}" ,
35- expression. id( ) ,
36- type_name:: <V >( )
37- )
38- } ) ?;
39-
40- let data = expression
41- . data ( )
42- . downcast_ref :: < V :: Instance > ( )
43- . ok_or_else ( || {
44- vortex_err ! (
45- "Failed to downcast expression instance data to expected type {}" ,
46- type_name:: <V :: Instance >( )
47- )
48- } ) ?;
49-
50- Ok ( Self { expression, data } )
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+ } )
5139 }
5240}
5341
5442impl < ' 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+
5549 /// Returns the instance data for this expression.
5650 #[ inline( always) ]
5751 pub fn data ( & self ) -> & ' a V :: Instance {
0 commit comments