Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Sealed 2 traits to forbid downstream implementations #621

Merged
merged 1 commit into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/array/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ use num_traits::Num;

use crate::types::Index;

/// Trait describing types that can be used as offsets as per Arrow specification.
/// This trait is only implemented for `i32` and `i64`, the two sizes part of the specification.
/// # Safety
/// Do not implement.
pub unsafe trait Offset: Index + Num + Ord + num_traits::CheckedAdd {
mod private {
pub trait Sealed {}

impl Sealed for i32 {}
impl Sealed for i64 {}
}

/// Sealed trait describing types that can be used as offsets in Arrow (`i32` and `i64`).
pub trait Offset: private::Sealed + Index + Num + Ord + num_traits::CheckedAdd {
/// Whether it is `i32` or `i64`
fn is_large() -> bool;

Expand All @@ -19,7 +23,7 @@ pub unsafe trait Offset: Index + Num + Ord + num_traits::CheckedAdd {
fn from_isize(value: isize) -> Option<Self>;
}

unsafe impl Offset for i32 {
impl Offset for i32 {
#[inline]
fn is_large() -> bool {
false
Expand All @@ -36,7 +40,7 @@ unsafe impl Offset for i32 {
}
}

unsafe impl Offset for i64 {
impl Offset for i64 {
#[inline]
fn is_large() -> bool {
true
Expand Down
4 changes: 2 additions & 2 deletions src/io/parquet/write/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ impl<O: Offset> Iterator for DefLevelsIter<'_, O> {

#[derive(Debug)]
pub struct NestedInfo<'a, O: Offset> {
is_optional: bool,
_is_optional: bool,
offsets: &'a [O],
validity: Option<&'a Bitmap>,
}

impl<'a, O: Offset> NestedInfo<'a, O> {
pub fn new(offsets: &'a [O], validity: Option<&'a Bitmap>, is_optional: bool) -> Self {
Self {
is_optional,
_is_optional: is_optional,
offsets,
validity,
}
Expand Down
37 changes: 27 additions & 10 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,33 @@ pub trait NaturalDataType {
const DATA_TYPE: DataType;
}

mod private {
pub trait Sealed {}

impl Sealed for u8 {}
impl Sealed for u16 {}
impl Sealed for u32 {}
impl Sealed for u64 {}
impl Sealed for i8 {}
impl Sealed for i16 {}
impl Sealed for i32 {}
impl Sealed for i64 {}
impl Sealed for i128 {}
impl Sealed for f32 {}
impl Sealed for f64 {}
impl Sealed for super::days_ms {}
impl Sealed for super::months_days_ns {}
}

/// describes whether a [`DataType`] is valid.
pub unsafe trait Relation {
pub trait Relation: private::Sealed {
/// Whether `data_type` is a valid [`DataType`].
fn is_valid(data_type: &DataType) -> bool;
}

macro_rules! create_relation {
($native_ty:ty, $physical_ty:expr) => {
unsafe impl Relation for $native_ty {
impl Relation for $native_ty {
#[inline]
fn is_valid(data_type: &DataType) -> bool {
data_type.to_physical_type() == $physical_ty
Expand All @@ -49,11 +67,10 @@ macro_rules! natural_type {
};
}

/// Declares any type that can be allocated, serialized and deserialized by this crate.
/// All data-heavy memory operations are implemented for this trait alone.
/// # Safety
/// Do not implement.
pub unsafe trait NativeType:
/// Sealed trait that implemented by all types that can be allocated,
/// serialized and deserialized by this crate.
/// All O(N) in-memory allocations are implemented for this trait alone.
pub trait NativeType:
Relation
+ NaturalDataType
+ Send
Expand Down Expand Up @@ -89,7 +106,7 @@ pub unsafe trait NativeType:

macro_rules! native {
($type:ty) => {
unsafe impl NativeType for $type {
impl NativeType for $type {
type Bytes = [u8; std::mem::size_of::<Self>()];
#[inline]
fn to_le_bytes(&self) -> Self::Bytes {
Expand Down Expand Up @@ -166,7 +183,7 @@ impl std::fmt::Display for days_ms {
}
}

unsafe impl NativeType for days_ms {
impl NativeType for days_ms {
type Bytes = [u8; 8];
#[inline]
fn to_le_bytes(&self) -> Self::Bytes {
Expand Down Expand Up @@ -266,7 +283,7 @@ impl std::fmt::Display for months_days_ns {
}
}

unsafe impl NativeType for months_days_ns {
impl NativeType for months_days_ns {
type Bytes = [u8; 16];
#[inline]
fn to_le_bytes(&self) -> Self::Bytes {
Expand Down