mbrobbel commented on issue #655: URL: https://github.com/apache/arrow-rs/issues/655#issuecomment-900092027
I've been working on [an Arrow implementation](https://github.com/mbrobbel/narrow) for applications with statically known data types. The way that I've managed to express this is by adding a boolean const generic to array types that indicate the nullability of an array (`true` indicates nullable). This makes nullable and non-nullable arrays different types allowing: ```rust impl FromIterator<Option<Foo>> for FooArray<true> { ... } impl FromIterator<Foo> for FooArray<false> { ... } ``` Type check fails when you attempt to collect the iterator in an array type with the wrong nullability generic value. ```rust #[derive(Array, Copy, Clone, Default)] struct Foo { a: bool, } // Fails to type check let array = [Foo::default(); 4] .iter() .copied() .collect::<StructArray<Foo, true>>(); // Compiles let array = [Foo::default(); 4] .iter() .copied() .collect::<StructArray<Foo, false>>(); ``` ```rust a value of type `StructArray<Foo, true>` cannot be built from an iterator over elements of type `Foo` the trait `FromIterator<Foo>` is not implemented for `StructArray<Foo, true>` ``` This would work for a nullable array: ```rust let array = [Some(Foo::default()); 4] .iter() .copied() .collect::<StructArray<Foo, true>>(); ``` Currently const arguments cannot be inferred by the compiler, but that might be possible in the future making this more ergonomic. ```rust let array = [Foo::default(); 4] .iter() .copied() .collect::<StructArray<Foo, _>>(); ``` -- 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]
