Skip to content

Commit 946931b

Browse files
committed
Add unsafe to Array trait and SAFETY commands in all unsafe impl Array
1 parent 21b3265 commit 946931b

15 files changed

+43
-19
lines changed

arrow-array/src/array/boolean_array.rs

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

289-
impl Array for BooleanArray {
289+
/// SAFETY: Correctly implements the contract of Arrow Arrays
290+
unsafe impl Array for BooleanArray {
290291
fn as_any(&self) -> &dyn Any {
291292
self
292293
}

arrow-array/src/array/byte_array.rs

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

465-
impl<T: ByteArrayType> Array for GenericByteArray<T> {
465+
/// SAFETY: Correctly implements the contract of Arrow Arrays
466+
unsafe impl<T: ByteArrayType> Array for GenericByteArray<T> {
466467
fn as_any(&self) -> &dyn Any {
467468
self
468469
}

arrow-array/src/array/byte_view_array.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,8 @@ impl<T: ByteViewType + ?Sized> Debug for GenericByteViewArray<T> {
885885
}
886886
}
887887

888-
impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> {
888+
/// SAFETY: Correctly implements the contract of Arrow Arrays
889+
unsafe impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> {
889890
fn as_any(&self) -> &dyn Any {
890891
self
891892
}

arrow-array/src/array/dictionary_array.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,8 @@ impl<'a, T: ArrowDictionaryKeyType> FromIterator<&'a str> for DictionaryArray<T>
692692
}
693693
}
694694

695-
impl<T: ArrowDictionaryKeyType> Array for DictionaryArray<T> {
695+
/// SAFETY: Correctly implements the contract of Arrow Arrays
696+
unsafe impl<T: ArrowDictionaryKeyType> Array for DictionaryArray<T> {
696697
fn as_any(&self) -> &dyn Any {
697698
self
698699
}
@@ -851,7 +852,7 @@ impl<'a, K: ArrowDictionaryKeyType, V> TypedDictionaryArray<'a, K, V> {
851852
}
852853
}
853854

854-
impl<K: ArrowDictionaryKeyType, V: Sync> Array for TypedDictionaryArray<'_, K, V> {
855+
unsafe impl<K: ArrowDictionaryKeyType, V: Sync> Array for TypedDictionaryArray<'_, K, V> {
855856
fn as_any(&self) -> &dyn Any {
856857
self.dictionary
857858
}

arrow-array/src/array/fixed_size_binary_array.rs

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

605-
impl Array for FixedSizeBinaryArray {
605+
/// SAFETY: Correctly implements the contract of Arrow Arrays
606+
unsafe impl Array for FixedSizeBinaryArray {
606607
fn as_any(&self) -> &dyn Any {
607608
self
608609
}

arrow-array/src/array/fixed_size_list_array.rs

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

465-
impl Array for FixedSizeListArray {
465+
/// SAFETY: Correctly implements the contract of Arrow Arrays
466+
unsafe impl Array for FixedSizeListArray {
466467
fn as_any(&self) -> &dyn Any {
467468
self
468469
}

arrow-array/src/array/list_array.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
527527
}
528528
}
529529

530-
impl<OffsetSize: OffsetSizeTrait> Array for GenericListArray<OffsetSize> {
530+
/// SAFETY: Correctly implements the contract of Arrow Arrays
531+
unsafe impl<OffsetSize: OffsetSizeTrait> Array for GenericListArray<OffsetSize> {
531532
fn as_any(&self) -> &dyn Any {
532533
self
533534
}

arrow-array/src/array/list_view_array.rs

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

418-
impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
418+
/// SAFETY: Correctly implements the contract of Arrow Arrays
419+
unsafe impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
419420
fn as_any(&self) -> &dyn Any {
420421
self
421422
}

arrow-array/src/array/map_array.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ impl MapArray {
362362
}
363363
}
364364

365-
impl Array for MapArray {
365+
/// SAFETY: Correctly implements the contract of Arrow Arrays
366+
unsafe impl Array for MapArray {
366367
fn as_any(&self) -> &dyn Any {
367368
self
368369
}

arrow-array/src/array/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,16 @@ pub use list_view_array::*;
7979
use crate::iterator::ArrayIter;
8080

8181
/// An array in the [arrow columnar format](https://arrow.apache.org/docs/format/Columnar.html)
82-
pub trait Array: std::fmt::Debug + Send + Sync {
82+
///
83+
/// # Safety
84+
///
85+
/// Implementations of this trait must ensure that all methods implementations comply with
86+
/// the Arrow specification. No safety guards are placed and failing to comply with it can
87+
/// translate into panics or undefined behavior. For example, a value computed based on `len`
88+
/// may be used as a direct index into memory regions without checks.
89+
///
90+
/// Use it at your own risk knowing that this trait might be sealed in the future.
91+
pub unsafe trait Array: std::fmt::Debug + Send + Sync {
8392
/// Returns the array as [`Any`] so that it can be
8493
/// downcasted to a specific implementation.
8594
///
@@ -342,7 +351,7 @@ pub trait Array: std::fmt::Debug + Send + Sync {
342351
pub type ArrayRef = Arc<dyn Array>;
343352

344353
/// Ergonomics: Allow use of an ArrayRef as an `&dyn Array`
345-
impl Array for ArrayRef {
354+
unsafe impl Array for ArrayRef {
346355
fn as_any(&self) -> &dyn Any {
347356
self.as_ref().as_any()
348357
}
@@ -421,7 +430,7 @@ impl Array for ArrayRef {
421430
}
422431
}
423432

424-
impl<T: Array> Array for &T {
433+
unsafe impl<T: Array> Array for &T {
425434
fn as_any(&self) -> &dyn Any {
426435
T::as_any(self)
427436
}

0 commit comments

Comments
 (0)