tustvold commented on code in PR #3912:
URL: https://github.com/apache/arrow-rs/pull/3912#discussion_r1146128716
##########
arrow-array/src/cast.rs:
##########
@@ -709,6 +709,165 @@ where
T::from(array.to_data())
}
+mod private {
+ pub trait Sealed {}
+}
+
+/// An extension trait for `dyn Array` that provides ergonomic downcasting
+///
+/// ```
+/// # use std::sync::Arc;
+/// # use arrow_array::{ArrayRef, Int32Array};
+/// # use arrow_array::cast::AsArray;
+/// # use arrow_array::types::Int32Type;
+/// let col = Arc::new(Int32Array::from(vec![1, 2, 3])) as ArrayRef;
+/// assert_eq!(col.as_primitive::<Int32Type>().values(), &[1, 2, 3]);
+/// ```
+pub trait AsArray: private::Sealed {
+ /// Downcast this to a [`BooleanArray`] returning `None` if not possible
+ fn as_boolean_opt(&self) -> Option<&BooleanArray>;
+
+ /// Downcast this to a [`BooleanArray`] panicking if not possible
+ fn as_boolean(&self) -> &BooleanArray {
+ self.as_boolean_opt().expect("boolean array")
+ }
+
+ /// Downcast this to a [`PrimitiveArray`] returning `None` if not possible
+ fn as_primitive_opt<T: ArrowPrimitiveType>(&self) ->
Option<&PrimitiveArray<T>>;
+
+ /// Downcast this to a [`PrimitiveArray`] panicking if not possible
+ fn as_primitive<T: ArrowPrimitiveType>(&self) -> &PrimitiveArray<T> {
+ self.as_primitive_opt().expect("primitive array")
+ }
+
+ /// Downcast this to a [`GenericByteArray`] returning `None` if not
possible
+ fn as_bytes_opt<T: ByteArrayType>(&self) -> Option<&GenericByteArray<T>>;
Review Comment:
In hindsight I probably should have called this `GenericBytesArray` but hey
ho
--
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]