-
Notifications
You must be signed in to change notification settings - Fork 829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add additional documentation and examples to ArrayAccessor
#6141
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,8 +40,10 @@ | |
//! assert_eq!(array.values(), &[1, 0, 3]) | ||
//! ``` | ||
//! | ||
//! It is also possible to write generic code. For example, the following is generic over | ||
//! all primitively typed arrays | ||
//! It is also possible to write generic code for different concrete types. | ||
//! For example, since the following function is generic over all primitively | ||
//! typed arrays, when invoked the Rust compiler will generate specialized implementations | ||
//! with optimized code for each concrete type. | ||
//! | ||
//! ```rust | ||
//! # use std::iter::Sum; | ||
|
@@ -60,7 +62,10 @@ | |
//! assert_eq!(sum(&TimestampNanosecondArray::from(vec![1, 2, 3])), 6); | ||
//! ``` | ||
//! | ||
//! And the following is generic over all arrays with comparable values | ||
//! And the following uses [`ArrayAccessor`] to implement a generic function | ||
//! over all arrays with comparable values. | ||
//! | ||
//! [`ArrayAccessor`]: array::ArrayAccessor | ||
//! | ||
//! ```rust | ||
//! # use arrow::array::{ArrayAccessor, ArrayIter, Int32Array, StringArray}; | ||
|
@@ -81,10 +86,11 @@ | |
//! | ||
//! # Type Erasure / Trait Objects | ||
//! | ||
//! It is often the case that code wishes to handle any type of array, without necessarily knowing | ||
//! its concrete type. This use-case is catered for by a combination of [`Array`] | ||
//! and [`DataType`](datatypes::DataType), with the former providing a type-erased container for | ||
//! the array, and the latter identifying the concrete type of array. | ||
//! It is common to write code that handles any type of array, without necessarily | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to make this less verbose so it was easier to read. |
||
//! knowing its concrete type. This is done using the [`Array`] trait and using | ||
//! [`DataType`] to determine the appropriate `downcast_ref`. | ||
//! | ||
//! [`DataType`]: datatypes::DataType | ||
//! | ||
//! ```rust | ||
//! # use arrow::array::{Array, Float32Array}; | ||
|
@@ -96,14 +102,18 @@ | |
//! | ||
//! fn impl_dyn(array: &dyn Array) { | ||
//! match array.data_type() { | ||
//! // downcast `dyn Array` to concrete `StringArray` | ||
//! DataType::Utf8 => impl_string(array.as_any().downcast_ref().unwrap()), | ||
//! // downcast `dyn Array` to concrete `Float32Array` | ||
//! DataType::Float32 => impl_f32(array.as_any().downcast_ref().unwrap()), | ||
//! _ => unimplemented!() | ||
//! } | ||
//! } | ||
//! ``` | ||
//! | ||
//! To facilitate downcasting, the [`AsArray`](crate::array::AsArray) extension trait can be used | ||
//! You can use the [`AsArray`] extension trait to facilitate downcasting: | ||
//! | ||
//! [`AsArray`]: crate::array::AsArray | ||
//! | ||
//! ```rust | ||
//! # use arrow::array::{Array, Float32Array, AsArray}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example is adapted from apache/datafusion#11676