Skip to content

Commit ba38ebe

Browse files
authored
Seal ArrowNativeType and OffsetSizeTrait (#1028) (#1819)
* Seal ArrowNativeType (#1028) * Add docs
1 parent 1d31d30 commit ba38ebe

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

arrow/src/datatypes/native.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,36 @@ use super::DataType;
1919
use half::f16;
2020
use serde_json::{Number, Value};
2121

22+
mod private {
23+
pub trait Sealed {}
24+
}
25+
2226
/// Trait declaring any type that is serializable to JSON. This includes all primitive types (bool, i32, etc.).
2327
pub trait JsonSerializable: 'static {
2428
fn into_json_value(self) -> Option<Value>;
2529
}
2630

2731
/// Trait expressing a Rust type that has the same in-memory representation
2832
/// as Arrow. This includes `i16`, `f32`, but excludes `bool` (which in arrow is represented in bits).
33+
///
2934
/// In little endian machines, types that implement [`ArrowNativeType`] can be memcopied to arrow buffers
3035
/// as is.
36+
///
37+
/// # Transmute Safety
38+
///
39+
/// A type T implementing this trait means that any arbitrary slice of bytes of length and
40+
/// alignment `size_of::<T>()` can be safely interpreted as a value of that type without
41+
/// being unsound, i.e. potentially resulting in undefined behaviour.
42+
///
43+
/// Note: in the case of floating point numbers this transmutation can result in a signalling
44+
/// NaN, which, whilst sound, can be unwieldy. In general, whilst it is perfectly sound to
45+
/// reinterpret bytes as different types using this trait, it is likely unwise
46+
///
47+
/// Note: `bool` is restricted to `0` or `1`, and so `bool: !ArrowNativeType`
48+
///
49+
/// # Sealed
50+
///
51+
/// Due to the above restrictions, this trait is sealed to prevent accidental misuse
3152
pub trait ArrowNativeType:
3253
std::fmt::Debug
3354
+ Send
@@ -37,6 +58,7 @@ pub trait ArrowNativeType:
3758
+ std::str::FromStr
3859
+ Default
3960
+ JsonSerializable
61+
+ private::Sealed
4062
{
4163
/// Convert native type from usize.
4264
#[inline]
@@ -109,6 +131,7 @@ impl JsonSerializable for i8 {
109131
}
110132
}
111133

134+
impl private::Sealed for i8 {}
112135
impl ArrowNativeType for i8 {
113136
#[inline]
114137
fn from_usize(v: usize) -> Option<Self> {
@@ -132,6 +155,7 @@ impl JsonSerializable for i16 {
132155
}
133156
}
134157

158+
impl private::Sealed for i16 {}
135159
impl ArrowNativeType for i16 {
136160
#[inline]
137161
fn from_usize(v: usize) -> Option<Self> {
@@ -155,6 +179,7 @@ impl JsonSerializable for i32 {
155179
}
156180
}
157181

182+
impl private::Sealed for i32 {}
158183
impl ArrowNativeType for i32 {
159184
#[inline]
160185
fn from_usize(v: usize) -> Option<Self> {
@@ -184,6 +209,7 @@ impl JsonSerializable for i64 {
184209
}
185210
}
186211

212+
impl private::Sealed for i64 {}
187213
impl ArrowNativeType for i64 {
188214
#[inline]
189215
fn from_usize(v: usize) -> Option<Self> {
@@ -217,6 +243,7 @@ impl JsonSerializable for i128 {
217243
}
218244
}
219245

246+
impl private::Sealed for i128 {}
220247
impl ArrowNativeType for i128 {
221248
#[inline]
222249
fn from_usize(v: usize) -> Option<Self> {
@@ -246,6 +273,7 @@ impl JsonSerializable for u8 {
246273
}
247274
}
248275

276+
impl private::Sealed for u8 {}
249277
impl ArrowNativeType for u8 {
250278
#[inline]
251279
fn from_usize(v: usize) -> Option<Self> {
@@ -269,6 +297,7 @@ impl JsonSerializable for u16 {
269297
}
270298
}
271299

300+
impl private::Sealed for u16 {}
272301
impl ArrowNativeType for u16 {
273302
#[inline]
274303
fn from_usize(v: usize) -> Option<Self> {
@@ -292,6 +321,7 @@ impl JsonSerializable for u32 {
292321
}
293322
}
294323

324+
impl private::Sealed for u32 {}
295325
impl ArrowNativeType for u32 {
296326
#[inline]
297327
fn from_usize(v: usize) -> Option<Self> {
@@ -315,6 +345,7 @@ impl JsonSerializable for u64 {
315345
}
316346
}
317347

348+
impl private::Sealed for u64 {}
318349
impl ArrowNativeType for u64 {
319350
#[inline]
320351
fn from_usize(v: usize) -> Option<Self> {
@@ -351,8 +382,11 @@ impl JsonSerializable for f64 {
351382
}
352383

353384
impl ArrowNativeType for f16 {}
385+
impl private::Sealed for f16 {}
354386
impl ArrowNativeType for f32 {}
387+
impl private::Sealed for f32 {}
355388
impl ArrowNativeType for f64 {}
389+
impl private::Sealed for f64 {}
356390

357391
/// Allows conversion from supported Arrow types to a byte slice.
358392
pub trait ToByteSlice {

0 commit comments

Comments
 (0)