tustvold commented on code in PR #2297:
URL: https://github.com/apache/arrow-rs/pull/2297#discussion_r936536117


##########
arrow/src/array/array_dictionary.rs:
##########
@@ -385,6 +404,86 @@ impl<T: ArrowPrimitiveType> fmt::Debug for 
DictionaryArray<T> {
     }
 }
 
+/// A strongly-typed wrapper around a [`DictionaryArray`] that implements 
[`ArrayAccessor`]
+/// allowing fast access to its elements
+///
+/// ```
+/// use arrow::array::{ArrayIter, DictionaryArray, StringArray};
+/// use arrow::datatypes::Int32Type;
+///
+/// let orig = ["a", "b", "a", "b"];
+/// let dictionary = DictionaryArray::<Int32Type>::from_iter(orig);
+/// let typed = dictionary.downcast_dict::<StringArray>().unwrap();
+///
+/// for (maybe_val, orig) in typed.into_iter().zip(orig) {
+///     assert_eq!(maybe_val.unwrap(), orig)
+/// }
+/// ```
+#[derive(Copy, Clone)]
+pub struct TypedDictionaryArray<'a, K: ArrowPrimitiveType, V> {
+    /// The dictionary array
+    pub dictionary: &'a DictionaryArray<K>,
+    /// The values of the dictionary
+    pub values: &'a V,
+}
+
+impl<'a, K: ArrowPrimitiveType, V> fmt::Debug for TypedDictionaryArray<'a, K, 
V> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        writeln!(f, "TypedDictionaryArray({:?})", self.dictionary)
+    }
+}
+
+impl<'a, K: ArrowPrimitiveType, V: Sync> Array for TypedDictionaryArray<'a, K, 
V> {
+    fn as_any(&self) -> &dyn Any {
+        self.dictionary
+    }
+
+    fn data(&self) -> &ArrayData {
+        &self.dictionary.data
+    }
+
+    fn into_data(self) -> ArrayData {
+        self.dictionary.into_data()
+    }
+}
+
+impl<'a, K, V> IntoIterator for TypedDictionaryArray<'a, K, V>
+where
+    K: ArrowPrimitiveType,
+    V: Sync + Send,
+    &'a V: ArrayAccessor,
+{
+    type Item = Option<<Self as ArrayAccessor>::Item>;
+    type IntoIter = ArrayIter<Self>;
+
+    fn into_iter(self) -> Self::IntoIter {
+        ArrayIter::new(self)

Review Comment:
   I think this is a nice example of how the composability of traits can be 
better than macros



-- 
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