Skip to content

Commit 721f373

Browse files
tustvoldalamb
andauthored
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 #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 <andrew@nerdnetworks.org>
1 parent 2b179b8 commit 721f373

15 files changed

Lines changed: 45 additions & 1 deletion

arrow-array/src/array/boolean_array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ impl BooleanArray {
286286
}
287287
}
288288

289+
impl super::private::Sealed for BooleanArray {}
290+
289291
impl Array for BooleanArray {
290292
fn as_any(&self) -> &dyn Any {
291293
self

arrow-array/src/array/byte_array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ impl<T: ByteArrayType> std::fmt::Debug for GenericByteArray<T> {
462462
}
463463
}
464464

465+
impl<T: ByteArrayType> super::private::Sealed for GenericByteArray<T> {}
466+
465467
impl<T: ByteArrayType> Array for GenericByteArray<T> {
466468
fn as_any(&self) -> &dyn Any {
467469
self

arrow-array/src/array/byte_view_array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ impl<T: ByteViewType + ?Sized> Debug for GenericByteViewArray<T> {
854854
}
855855
}
856856

857+
impl<T: ByteViewType + ?Sized> super::private::Sealed for GenericByteViewArray<T> {}
858+
857859
impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> {
858860
fn as_any(&self) -> &dyn Any {
859861
self

arrow-array/src/array/dictionary_array.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,8 @@ impl<'a, T: ArrowDictionaryKeyType> FromIterator<&'a str> for DictionaryArray<T>
697697
}
698698
}
699699

700+
impl<T: ArrowDictionaryKeyType> super::private::Sealed for DictionaryArray<T> {}
701+
700702
impl<T: ArrowDictionaryKeyType> Array for DictionaryArray<T> {
701703
fn as_any(&self) -> &dyn Any {
702704
self
@@ -856,6 +858,8 @@ impl<'a, K: ArrowDictionaryKeyType, V> TypedDictionaryArray<'a, K, V> {
856858
}
857859
}
858860

861+
impl<K: ArrowDictionaryKeyType, V: Sync> super::private::Sealed for TypedDictionaryArray<'_, K, V> {}
862+
859863
impl<K: ArrowDictionaryKeyType, V: Sync> Array for TypedDictionaryArray<'_, K, V> {
860864
fn as_any(&self) -> &dyn Any {
861865
self.dictionary

arrow-array/src/array/fixed_size_binary_array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ impl std::fmt::Debug for FixedSizeBinaryArray {
602602
}
603603
}
604604

605+
impl super::private::Sealed for FixedSizeBinaryArray {}
606+
605607
impl Array for FixedSizeBinaryArray {
606608
fn as_any(&self) -> &dyn Any {
607609
self

arrow-array/src/array/fixed_size_list_array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ impl From<FixedSizeListArray> for ArrayData {
462462
}
463463
}
464464

465+
impl super::private::Sealed for FixedSizeListArray {}
466+
465467
impl Array for FixedSizeListArray {
466468
fn as_any(&self) -> &dyn Any {
467469
self

arrow-array/src/array/list_array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,8 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
525525
}
526526
}
527527

528+
impl<OffsetSize: OffsetSizeTrait> super::private::Sealed for GenericListArray<OffsetSize> {}
529+
528530
impl<OffsetSize: OffsetSizeTrait> Array for GenericListArray<OffsetSize> {
529531
fn as_any(&self) -> &dyn Any {
530532
self

arrow-array/src/array/list_view_array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ impl<OffsetSize: OffsetSizeTrait> ArrayAccessor for &GenericListViewArray<Offset
415415
}
416416
}
417417

418+
impl<OffsetSize: OffsetSizeTrait> super::private::Sealed for GenericListViewArray<OffsetSize> {}
419+
418420
impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
419421
fn as_any(&self) -> &dyn Any {
420422
self

arrow-array/src/array/map_array.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ impl MapArray {
361361
}
362362
}
363363

364+
impl super::private::Sealed for MapArray {}
365+
364366
impl Array for MapArray {
365367
fn as_any(&self) -> &dyn Any {
366368
self

arrow-array/src/array/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,18 @@ pub use list_view_array::*;
7878

7979
use crate::iterator::ArrayIter;
8080

81+
mod private {
82+
/// Private marker trait to ensure [`super::Array`] can not be implemented outside this crate
83+
pub trait Sealed {}
84+
85+
impl<T: Sealed> Sealed for &T {}
86+
}
87+
8188
/// An array in the [arrow columnar format](https://arrow.apache.org/docs/format/Columnar.html)
82-
pub trait Array: std::fmt::Debug + Send + Sync {
89+
///
90+
/// This trait is sealed as it is not intended for custom array types, rather only
91+
/// those defined in this crate.
92+
pub trait Array: std::fmt::Debug + Send + Sync + private::Sealed {
8393
/// Returns the array as [`Any`] so that it can be
8494
/// downcasted to a specific implementation.
8595
///
@@ -341,6 +351,8 @@ pub trait Array: std::fmt::Debug + Send + Sync {
341351
/// A reference-counted reference to a generic `Array`
342352
pub type ArrayRef = Arc<dyn Array>;
343353

354+
impl private::Sealed for ArrayRef {}
355+
344356
/// Ergonomics: Allow use of an ArrayRef as an `&dyn Array`
345357
impl Array for ArrayRef {
346358
fn as_any(&self) -> &dyn Any {

0 commit comments

Comments
 (0)