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

Commit

Permalink
make trait unsafe to implement
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 1, 2022
1 parent 0f6c841 commit 3aa86b9
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/array/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ use super::{new_empty_array, primitive::PrimitiveArray, Array};
use super::{new_null_array, specification::check_indexes};

/// Trait denoting [`NativeType`]s that can be used as keys of a dictionary.
pub trait DictionaryKey: NativeType + TryInto<usize> + TryFrom<usize> {
/// # Safety
///
/// Any implementation of this trait must ensure that `always_fits_usize` only
/// returns `true` if all values succeeds on `value::try_into::<usize>().unwrap()`.
pub unsafe trait DictionaryKey: NativeType + TryInto<usize> + TryFrom<usize> {
/// The corresponding [`IntegerType`] of this key
const KEY_TYPE: IntegerType;

Expand All @@ -39,45 +43,46 @@ pub trait DictionaryKey: NativeType + TryInto<usize> + TryFrom<usize> {
}
}

/// If the key type always can be converted to `usize`.
fn always_fits_usize() -> bool {
false
}
}

impl DictionaryKey for i8 {
unsafe impl DictionaryKey for i8 {
const KEY_TYPE: IntegerType = IntegerType::Int8;
}
impl DictionaryKey for i16 {
unsafe impl DictionaryKey for i16 {
const KEY_TYPE: IntegerType = IntegerType::Int16;
}
impl DictionaryKey for i32 {
unsafe impl DictionaryKey for i32 {
const KEY_TYPE: IntegerType = IntegerType::Int32;
}
impl DictionaryKey for i64 {
unsafe impl DictionaryKey for i64 {
const KEY_TYPE: IntegerType = IntegerType::Int64;
}
impl DictionaryKey for u8 {
unsafe impl DictionaryKey for u8 {
const KEY_TYPE: IntegerType = IntegerType::UInt8;

fn always_fits_usize() -> bool {
true
}
}
impl DictionaryKey for u16 {
unsafe impl DictionaryKey for u16 {
const KEY_TYPE: IntegerType = IntegerType::UInt16;

fn always_fits_usize() -> bool {
true
}
}
impl DictionaryKey for u32 {
unsafe impl DictionaryKey for u32 {
const KEY_TYPE: IntegerType = IntegerType::UInt32;

fn always_fits_usize() -> bool {
true
}
}
impl DictionaryKey for u64 {
unsafe impl DictionaryKey for u64 {
const KEY_TYPE: IntegerType = IntegerType::UInt64;

#[cfg(target_pointer_width = "64")]
Expand Down Expand Up @@ -145,7 +150,7 @@ impl<K: DictionaryKey> DictionaryArray<K> {
if K::always_fits_usize() {
// safety: we just checked that conversion to `usize` always
// succeeds
unsafe { check_indexes_unchecked(keys.values(), len) }?;
unsafe { check_indexes_unchecked(keys.values(), values.len()) }?;
} else {
check_indexes(keys.values(), values.len())?;
}
Expand Down

0 comments on commit 3aa86b9

Please sign in to comment.