You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/10/24 15:26:07 UTC

[GitHub] [arrow-rs] crepererum commented on a diff in pull request #2902: add `Array::as_any_arc`

crepererum commented on code in PR #2902:
URL: https://github.com/apache/arrow-rs/pull/2902#discussion_r1003454609


##########
arrow-array/src/array/mod.rs:
##########
@@ -88,6 +88,31 @@ pub trait Array: std::fmt::Debug + Send + Sync {
     /// ```
     fn as_any(&self) -> &dyn Any;
 
+    /// Returns the array as [`Any`](std::any::Any) wrapped into an [`Arc`] so that it can be
+    /// downcasted to a specific implementation without the need for an unsized reference. This is helpful if you want
+    /// to return a concrete array type.
+    /// 
+    /// Note that this conversion may not be possible for all types due to the `'static` constraint.
+    ///
+    /// # Example:
+    ///
+    /// ```
+    /// # use std::sync::Arc;
+    /// # use arrow_array::{Int32Array, RecordBatch};
+    /// # use arrow_schema::{Schema, Field, DataType, ArrowError};
+    ///
+    /// let id = Int32Array::from(vec![1, 2, 3, 4, 5]);
+    /// let batch = RecordBatch::try_new(
+    ///     Arc::new(Schema::new(vec![Field::new("id", DataType::Int32, false)])),
+    ///     vec![Arc::new(id)]
+    /// ).unwrap();
+    ///
+    /// let int32array = Arc::downcast::<Int32Array>(
+    ///     Arc::clone(batch.column(0)).as_any_arc().expect("Failed to create static Arc")
+    /// ).expect("Failed to downcast");

Review Comment:
   Adding a helpers sounds great.
   
   Regarding privacy: do we consider `Array` to be a sealed trait (because this would be the implication)? Currently users can implement their own `Array` types if they want.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org