|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::any::type_name; |
| 5 | +use std::fmt::Debug; |
| 6 | +use std::fmt::Display; |
| 7 | +use std::fmt::Formatter; |
| 8 | +use std::hash::Hash; |
| 9 | +use std::hash::Hasher; |
| 10 | +use std::sync::Arc; |
| 11 | + |
| 12 | +use vortex_error::VortexExpect; |
| 13 | +use vortex_error::VortexResult; |
| 14 | +use vortex_error::vortex_err; |
| 15 | +use vortex_utils::debug_with::DebugWith; |
| 16 | + |
| 17 | +use crate::higher_order_fn::HigherOrderFnId; |
| 18 | +use crate::higher_order_fn::HigherOrderFnOptions; |
| 19 | +use crate::higher_order_fn::HigherOrderFnVTable; |
| 20 | +use crate::higher_order_fn::TypedHigherOrderFnInstance; |
| 21 | +use crate::higher_order_fn::typed::DynHigherOrderFn; |
| 22 | +use crate::scalar_fn::Arity; |
| 23 | +use crate::scalar_fn::ChildName; |
| 24 | + |
| 25 | +/// A type-erased higher-order function, pairing a vtable with per-call options. |
| 26 | +#[derive(Clone)] |
| 27 | +pub struct HigherOrderFnRef(pub(super) Arc<dyn DynHigherOrderFn>); |
| 28 | + |
| 29 | +impl HigherOrderFnRef { |
| 30 | + /// Bind `vtable` with its per-call `options`. |
| 31 | + pub fn new<V: HigherOrderFnVTable>(vtable: V, options: V::Options) -> Self { |
| 32 | + TypedHigherOrderFnInstance::new(vtable, options).erased() |
| 33 | + } |
| 34 | + |
| 35 | + /// The function's global identifier. |
| 36 | + pub fn id(&self) -> HigherOrderFnId { |
| 37 | + self.0.id() |
| 38 | + } |
| 39 | + |
| 40 | + /// Whether this function uses vtable `V`. |
| 41 | + pub fn is<V: HigherOrderFnVTable>(&self) -> bool { |
| 42 | + self.0.as_any().is::<TypedHigherOrderFnInstance<V>>() |
| 43 | + } |
| 44 | + |
| 45 | + /// Return typed options when this function uses vtable `V`. |
| 46 | + pub fn as_opt<V: HigherOrderFnVTable>(&self) -> Option<&V::Options> { |
| 47 | + self.0 |
| 48 | + .as_any() |
| 49 | + .downcast_ref::<TypedHigherOrderFnInstance<V>>() |
| 50 | + .map(TypedHigherOrderFnInstance::options) |
| 51 | + } |
| 52 | + |
| 53 | + /// Return typed options for vtable `V`. |
| 54 | + /// |
| 55 | + /// # Panics |
| 56 | + /// |
| 57 | + /// Panics if this function does not use vtable `V`. |
| 58 | + pub fn as_<V: HigherOrderFnVTable>(&self) -> &V::Options { |
| 59 | + self.as_opt::<V>() |
| 60 | + .vortex_expect("higher-order function options type mismatch") |
| 61 | + } |
| 62 | + |
| 63 | + /// Return these options behind an opaque type-erased handle. |
| 64 | + pub fn options(&self) -> HigherOrderFnOptions<'_> { |
| 65 | + HigherOrderFnOptions { inner: &*self.0 } |
| 66 | + } |
| 67 | + |
| 68 | + /// Return the arity of the ordinary arguments. |
| 69 | + pub fn arity(&self) -> Arity { |
| 70 | + self.0.arity() |
| 71 | + } |
| 72 | + |
| 73 | + /// Return the number of lambda arguments. |
| 74 | + pub fn lambda_arity(&self) -> usize { |
| 75 | + self.0.lambda_arity() |
| 76 | + } |
| 77 | + |
| 78 | + /// Return the name of an ordinary argument. |
| 79 | + pub fn child_name(&self, child_idx: usize) -> ChildName { |
| 80 | + self.0.child_name(child_idx) |
| 81 | + } |
| 82 | + |
| 83 | + /// Serialize the per-call options. |
| 84 | + pub fn serialize(&self) -> VortexResult<Option<Vec<u8>>> { |
| 85 | + self.0.options_serialize() |
| 86 | + } |
| 87 | + |
| 88 | + /// Downcast this function to its typed representation. |
| 89 | + pub fn try_downcast<V: HigherOrderFnVTable>( |
| 90 | + self, |
| 91 | + ) -> Result<Arc<TypedHigherOrderFnInstance<V>>, Self> { |
| 92 | + if self.is::<V>() { |
| 93 | + let ptr = Arc::into_raw(self.0) as *const TypedHigherOrderFnInstance<V>; |
| 94 | + Ok(unsafe { Arc::from_raw(ptr) }) |
| 95 | + } else { |
| 96 | + Err(self) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /// Downcast this function to its typed representation. |
| 101 | + /// |
| 102 | + /// # Panics |
| 103 | + /// |
| 104 | + /// Panics if this function does not use vtable `V`. |
| 105 | + pub fn downcast<V: HigherOrderFnVTable>(self) -> Arc<TypedHigherOrderFnInstance<V>> { |
| 106 | + self.try_downcast::<V>() |
| 107 | + .map_err(|function| { |
| 108 | + vortex_err!( |
| 109 | + "failed to downcast higher-order function {} to {}", |
| 110 | + function.id(), |
| 111 | + type_name::<V>(), |
| 112 | + ) |
| 113 | + }) |
| 114 | + .vortex_expect("failed to downcast higher-order function") |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl Debug for HigherOrderFnRef { |
| 119 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 120 | + f.debug_struct("HigherOrderFnRef") |
| 121 | + .field("vtable", &self.id()) |
| 122 | + .field("options", &DebugWith(|fmt| self.0.options_debug(fmt))) |
| 123 | + .finish() |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl Display for HigherOrderFnRef { |
| 128 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 129 | + write!(f, "{}(", self.id())?; |
| 130 | + self.0.options_display(f)?; |
| 131 | + write!(f, ")") |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl PartialEq for HigherOrderFnRef { |
| 136 | + fn eq(&self, other: &Self) -> bool { |
| 137 | + self.id() == other.id() && self.0.options_eq(other.0.options_any()) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl Eq for HigherOrderFnRef {} |
| 142 | + |
| 143 | +impl Hash for HigherOrderFnRef { |
| 144 | + fn hash<H: Hasher>(&self, state: &mut H) { |
| 145 | + self.id().hash(state); |
| 146 | + self.0.options_hash(state); |
| 147 | + } |
| 148 | +} |
0 commit comments