Skip to content

Methods should return &Arc references to inner trait objects for &self #11100

@linhr

Description

@linhr

Is your feature request related to a problem or challenge?

I'm trying to implement a helper function like this in my own project, but it cannot compile.

fn expr_to_func_impl<T: ScalarUDFImpl + 'static>(expr: &Expr) -> Option<&T> {
    match expr {
        Expr::ScalarFunction(ScalarFunction { func, .. }) => {
            func.inner().as_any().downcast_ref::<T>()
        }
        _ => None,
    }
}

The compiler gives the following error.

|             func.inner().as_any().downcast_ref::<T>()
|             ------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|             |
|             returns a value referencing data owned by the current function
|             temporary value created here

The issue is that inner() is defined to return a clone of Arc whose lifetime is not associated with &self. Therefore, it's inconvenient if someone has a reference to an outer struct and would like to cast the inner Arc<dyn MyTrait> object to &MyTraitImpl where MyTraitImpl implements MyTrait.

impl ScalarUDF {
    pub fn inner(&self) -> Arc<dyn ScalarUDFImpl> {
        self.inner.clone()
    }
}

Describe the solution you'd like

We should return &Arc to inner trait objects for &self methods. Many data structures in DataFusion already follow this pattern, and we could simply fix the few remaining places, such as the ScalarUDF::inner() method discussed above.

Describe alternatives you've considered

N/A

Additional context

N/A

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions