This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 721f373fba Seal Array trait (#9092)
721f373fba is described below
commit 721f373fba7493c873a64421e32f1f67114ac130
Author: Raphael Taylor-Davies <[email protected]>
AuthorDate: Wed Jan 7 16:48:58 2026 +0000
Seal Array trait (#9092)
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
-->
- Closes https://github.com/apache/arrow-rs/issues/9106
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
This trait is not meant to be overridden, and doing so will break many
kernels in sometimes subtle ways.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
Seals the Array trait to prevent implementation outside of arrow-array.
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
---------
Co-authored-by: Andrew Lamb <[email protected]>
---
arrow-array/src/array/boolean_array.rs | 2 ++
arrow-array/src/array/byte_array.rs | 2 ++
arrow-array/src/array/byte_view_array.rs | 2 ++
arrow-array/src/array/dictionary_array.rs | 4 ++++
arrow-array/src/array/fixed_size_binary_array.rs | 2 ++
arrow-array/src/array/fixed_size_list_array.rs | 2 ++
arrow-array/src/array/list_array.rs | 2 ++
arrow-array/src/array/list_view_array.rs | 2 ++
arrow-array/src/array/map_array.rs | 2 ++
arrow-array/src/array/mod.rs | 14 +++++++++++++-
arrow-array/src/array/null_array.rs | 2 ++
arrow-array/src/array/primitive_array.rs | 2 ++
arrow-array/src/array/run_array.rs | 4 ++++
arrow-array/src/array/struct_array.rs | 2 ++
arrow-array/src/array/union_array.rs | 2 ++
15 files changed, 45 insertions(+), 1 deletion(-)
diff --git a/arrow-array/src/array/boolean_array.rs
b/arrow-array/src/array/boolean_array.rs
index 530121ea78..acea680ae3 100644
--- a/arrow-array/src/array/boolean_array.rs
+++ b/arrow-array/src/array/boolean_array.rs
@@ -286,6 +286,8 @@ impl BooleanArray {
}
}
+impl super::private::Sealed for BooleanArray {}
+
impl Array for BooleanArray {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/byte_array.rs
b/arrow-array/src/array/byte_array.rs
index fbd8458846..bd85bffcfe 100644
--- a/arrow-array/src/array/byte_array.rs
+++ b/arrow-array/src/array/byte_array.rs
@@ -462,6 +462,8 @@ impl<T: ByteArrayType> std::fmt::Debug for
GenericByteArray<T> {
}
}
+impl<T: ByteArrayType> super::private::Sealed for GenericByteArray<T> {}
+
impl<T: ByteArrayType> Array for GenericByteArray<T> {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/byte_view_array.rs
b/arrow-array/src/array/byte_view_array.rs
index f677c4ae67..b31c76ab5a 100644
--- a/arrow-array/src/array/byte_view_array.rs
+++ b/arrow-array/src/array/byte_view_array.rs
@@ -854,6 +854,8 @@ impl<T: ByteViewType + ?Sized> Debug for
GenericByteViewArray<T> {
}
}
+impl<T: ByteViewType + ?Sized> super::private::Sealed for
GenericByteViewArray<T> {}
+
impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/dictionary_array.rs
b/arrow-array/src/array/dictionary_array.rs
index 5243218392..be7703b13c 100644
--- a/arrow-array/src/array/dictionary_array.rs
+++ b/arrow-array/src/array/dictionary_array.rs
@@ -697,6 +697,8 @@ impl<'a, T: ArrowDictionaryKeyType> FromIterator<&'a str>
for DictionaryArray<T>
}
}
+impl<T: ArrowDictionaryKeyType> super::private::Sealed for DictionaryArray<T>
{}
+
impl<T: ArrowDictionaryKeyType> Array for DictionaryArray<T> {
fn as_any(&self) -> &dyn Any {
self
@@ -856,6 +858,8 @@ impl<'a, K: ArrowDictionaryKeyType, V>
TypedDictionaryArray<'a, K, V> {
}
}
+impl<K: ArrowDictionaryKeyType, V: Sync> super::private::Sealed for
TypedDictionaryArray<'_, K, V> {}
+
impl<K: ArrowDictionaryKeyType, V: Sync> Array for TypedDictionaryArray<'_, K,
V> {
fn as_any(&self) -> &dyn Any {
self.dictionary
diff --git a/arrow-array/src/array/fixed_size_binary_array.rs
b/arrow-array/src/array/fixed_size_binary_array.rs
index d13cecb180..b94e168cfe 100644
--- a/arrow-array/src/array/fixed_size_binary_array.rs
+++ b/arrow-array/src/array/fixed_size_binary_array.rs
@@ -602,6 +602,8 @@ impl std::fmt::Debug for FixedSizeBinaryArray {
}
}
+impl super::private::Sealed for FixedSizeBinaryArray {}
+
impl Array for FixedSizeBinaryArray {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/fixed_size_list_array.rs
b/arrow-array/src/array/fixed_size_list_array.rs
index fca92a6481..3d5e8a0787 100644
--- a/arrow-array/src/array/fixed_size_list_array.rs
+++ b/arrow-array/src/array/fixed_size_list_array.rs
@@ -462,6 +462,8 @@ impl From<FixedSizeListArray> for ArrayData {
}
}
+impl super::private::Sealed for FixedSizeListArray {}
+
impl Array for FixedSizeListArray {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/list_array.rs
b/arrow-array/src/array/list_array.rs
index 32add1abf5..225be14ae3 100644
--- a/arrow-array/src/array/list_array.rs
+++ b/arrow-array/src/array/list_array.rs
@@ -525,6 +525,8 @@ impl<OffsetSize: OffsetSizeTrait>
GenericListArray<OffsetSize> {
}
}
+impl<OffsetSize: OffsetSizeTrait> super::private::Sealed for
GenericListArray<OffsetSize> {}
+
impl<OffsetSize: OffsetSizeTrait> Array for GenericListArray<OffsetSize> {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/list_view_array.rs
b/arrow-array/src/array/list_view_array.rs
index 867dcf955b..52c88d581d 100644
--- a/arrow-array/src/array/list_view_array.rs
+++ b/arrow-array/src/array/list_view_array.rs
@@ -415,6 +415,8 @@ impl<OffsetSize: OffsetSizeTrait> ArrayAccessor for
&GenericListViewArray<Offset
}
}
+impl<OffsetSize: OffsetSizeTrait> super::private::Sealed for
GenericListViewArray<OffsetSize> {}
+
impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/map_array.rs
b/arrow-array/src/array/map_array.rs
index b5e611a92b..86608d586f 100644
--- a/arrow-array/src/array/map_array.rs
+++ b/arrow-array/src/array/map_array.rs
@@ -361,6 +361,8 @@ impl MapArray {
}
}
+impl super::private::Sealed for MapArray {}
+
impl Array for MapArray {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/mod.rs b/arrow-array/src/array/mod.rs
index bb114be950..75e32d57e8 100644
--- a/arrow-array/src/array/mod.rs
+++ b/arrow-array/src/array/mod.rs
@@ -78,8 +78,18 @@ pub use list_view_array::*;
use crate::iterator::ArrayIter;
+mod private {
+ /// Private marker trait to ensure [`super::Array`] can not be implemented
outside this crate
+ pub trait Sealed {}
+
+ impl<T: Sealed> Sealed for &T {}
+}
+
/// An array in the [arrow columnar
format](https://arrow.apache.org/docs/format/Columnar.html)
-pub trait Array: std::fmt::Debug + Send + Sync {
+///
+/// This trait is sealed as it is not intended for custom array types, rather
only
+/// those defined in this crate.
+pub trait Array: std::fmt::Debug + Send + Sync + private::Sealed {
/// Returns the array as [`Any`] so that it can be
/// downcasted to a specific implementation.
///
@@ -341,6 +351,8 @@ pub trait Array: std::fmt::Debug + Send + Sync {
/// A reference-counted reference to a generic `Array`
pub type ArrayRef = Arc<dyn Array>;
+impl private::Sealed for ArrayRef {}
+
/// Ergonomics: Allow use of an ArrayRef as an `&dyn Array`
impl Array for ArrayRef {
fn as_any(&self) -> &dyn Any {
diff --git a/arrow-array/src/array/null_array.rs
b/arrow-array/src/array/null_array.rs
index 72556a92a3..b682466b67 100644
--- a/arrow-array/src/array/null_array.rs
+++ b/arrow-array/src/array/null_array.rs
@@ -76,6 +76,8 @@ impl NullArray {
}
}
+impl super::private::Sealed for NullArray {}
+
impl Array for NullArray {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/primitive_array.rs
b/arrow-array/src/array/primitive_array.rs
index e71f4d4719..457c242814 100644
--- a/arrow-array/src/array/primitive_array.rs
+++ b/arrow-array/src/array/primitive_array.rs
@@ -1190,6 +1190,8 @@ impl<T: ArrowPrimitiveType> From<PrimitiveArray<T>> for
ArrayData {
}
}
+impl<T: ArrowPrimitiveType> super::private::Sealed for PrimitiveArray<T> {}
+
impl<T: ArrowPrimitiveType> Array for PrimitiveArray<T> {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/run_array.rs
b/arrow-array/src/array/run_array.rs
index ddc99f8e17..5254a0ed3c 100644
--- a/arrow-array/src/array/run_array.rs
+++ b/arrow-array/src/array/run_array.rs
@@ -260,6 +260,8 @@ impl<R: RunEndIndexType> From<RunArray<R>> for ArrayData {
}
}
+impl<T: RunEndIndexType> super::private::Sealed for RunArray<T> {}
+
impl<T: RunEndIndexType> Array for RunArray<T> {
fn as_any(&self) -> &dyn Any {
self
@@ -519,6 +521,8 @@ impl<'a, R: RunEndIndexType, V> TypedRunArray<'a, R, V> {
}
}
+impl<R: RunEndIndexType, V: Sync> super::private::Sealed for TypedRunArray<'_,
R, V> {}
+
impl<R: RunEndIndexType, V: Sync> Array for TypedRunArray<'_, R, V> {
fn as_any(&self) -> &dyn Any {
self.run_array
diff --git a/arrow-array/src/array/struct_array.rs
b/arrow-array/src/array/struct_array.rs
index 5b18bd35d0..6ad1ead0d2 100644
--- a/arrow-array/src/array/struct_array.rs
+++ b/arrow-array/src/array/struct_array.rs
@@ -401,6 +401,8 @@ impl TryFrom<Vec<(&str, ArrayRef)>> for StructArray {
}
}
+impl super::private::Sealed for StructArray {}
+
impl Array for StructArray {
fn as_any(&self) -> &dyn Any {
self
diff --git a/arrow-array/src/array/union_array.rs
b/arrow-array/src/array/union_array.rs
index 934107d075..e08542bc86 100644
--- a/arrow-array/src/array/union_array.rs
+++ b/arrow-array/src/array/union_array.rs
@@ -738,6 +738,8 @@ impl From<UnionArray> for ArrayData {
}
}
+impl super::private::Sealed for UnionArray {}
+
impl Array for UnionArray {
fn as_any(&self) -> &dyn Any {
self