alamb commented on issue #1128:
URL: https://github.com/apache/arrow-rs/issues/1128#issuecomment-1004051178


   So what I am really trying to do is to the following
   
   ## Initial state:
   
   ```rust
   pub fn as_primitive_array<T>(arr: &ArrayRef) -> &PrimitiveArray<T>
   where
       T: ArrowPrimitiveType,
   {
     ...
   }
   ```
   
   There are many calls in the code base that look like this:
   
   ```rust
       let arr: ArrayRef = new_arr();
       let foo = as_primitive_array::<Int8Type>(&arr);
   ```
   
   I would like to re-use `as_primitive_array` in functions such as the compute 
kernels that take a `&dyn Array` rather than `&ArrayRef`:
   
   ```rust
       let arr_ref: &dyn Array = arr.as_ref();
       let bar = as_primitive_array::<Int8Type>(arr_ref);
   ```
   
   As written this results in an error:
   ```
   let bar = as_primitive_array::<Int8Type>(arr_ref);
                                            ^^^^^^^ expected struct `Arc`, 
found trait object `dyn Array`
   ```
   Playground here: 
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3a7907b6e7f08eb3b510aaaa6edb1500
 
   
   When I change the signature to my preferred one:
   ```rust
   pub fn as_primitive_array<T>(arr: &dyn Array) -> &PrimitiveArray<T>
   where
       T: ArrowPrimitiveType,
   {
   ...
   }
   ```
   
   Then the original calls error like this: 
   
   ```
      Compiling playground v0.0.1 (/playground)
   error[E0277]: the trait bound `Arc<(dyn Array + 'static)>: Array` is not 
satisfied
     --> src/main.rs:22:46
      |
   22 |     let foo = as_primitive_array::<Int8Type>(&arr);
      |               ------------------------------ ^^^^ the trait `Array` is 
not implemented for `Arc<(dyn Array + 'static)>`
      |               |
      |               required by a bound introduced by this call
      |
      = note: required for the cast to the object type `dyn Array`
   ```
   
   
   Link to playground showing errors when I tried this: 
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5ebd46406b1673e3bf80199b2cb5887e
   
   When I tried using `impl`:
   ```rust
   pub fn as_primitive_array<T>(arr: impl AsRef<dyn Array>) -> 
&PrimitiveArray<T>
   ...
   ```
   
   The compiler tells me it needs a named lifetime parameter: 
   
   ```rust
     Compiling playground v0.0.1 (/playground)
   error[E0106]: missing lifetime specifier
    --> src/main.rs:7:61
     |
   7 | pub fn as_primitive_array<T>(arr: impl AsRef<dyn Array>) -> 
&PrimitiveArray<T>
     |                                                             ^ expected 
named lifetime parameter
   ```
   
   But if I add a lifetime parameter, then all the existing calls of 
`as_primitive_array::<Int8Type>(&arr);` need to be changed which is what I am 
trying to avoid


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to