Skip to content

No way to downcast Arc<dyn Array> to Arc<T: Array> #8794

Description

@scovich

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

arrow-rs makes heavy use of ArrayRef (= Arc<dyn Array>), and it's impossible to impl Array for !Any types because Array::as_any is a required method. But for some reason we have Array: Send + Sync instead of Array: Any + Send + Sync, so there's no way to Arc::downcast an ArrayRef to e.g. Arc<StructArray>.

Describe the solution you'd like

trait Array: std::any::Any + ... { ... }

Which allows

let array: ArrayRef = ...;
let struct_array: Arc<StructArray> = Arc::downcast(array).map_err(...)?;

One big problem tho -- the blanket impl<T: Array> Array for &T means Array cannot require Any (because Any: 'static). I'm not sure how to work around that.

Describe alternatives you've considered

As far as I can tell, with today's code you have to downcast to &StructArray, clone it, and allocate a new Arc:

let array: ArrayRef = ...;
let struct_array = array.as_struct_opt().ok_or_else(...)?;
let struct_array = Arc::new(struct_array.clone());

Sure, cloning an array is probably fairly inexpensive, but there's no way it's as cheap as an arc clone.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementAny new improvement worthy of a entry in the changelog

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions