@@ -19,15 +19,36 @@ use super::DataType;
19
19
use half:: f16;
20
20
use serde_json:: { Number , Value } ;
21
21
22
+ mod private {
23
+ pub trait Sealed { }
24
+ }
25
+
22
26
/// Trait declaring any type that is serializable to JSON. This includes all primitive types (bool, i32, etc.).
23
27
pub trait JsonSerializable : ' static {
24
28
fn into_json_value ( self ) -> Option < Value > ;
25
29
}
26
30
27
31
/// Trait expressing a Rust type that has the same in-memory representation
28
32
/// as Arrow. This includes `i16`, `f32`, but excludes `bool` (which in arrow is represented in bits).
33
+ ///
29
34
/// In little endian machines, types that implement [`ArrowNativeType`] can be memcopied to arrow buffers
30
35
/// 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
31
52
pub trait ArrowNativeType :
32
53
std:: fmt:: Debug
33
54
+ Send
@@ -37,6 +58,7 @@ pub trait ArrowNativeType:
37
58
+ std:: str:: FromStr
38
59
+ Default
39
60
+ JsonSerializable
61
+ + private:: Sealed
40
62
{
41
63
/// Convert native type from usize.
42
64
#[ inline]
@@ -109,6 +131,7 @@ impl JsonSerializable for i8 {
109
131
}
110
132
}
111
133
134
+ impl private:: Sealed for i8 { }
112
135
impl ArrowNativeType for i8 {
113
136
#[ inline]
114
137
fn from_usize ( v : usize ) -> Option < Self > {
@@ -132,6 +155,7 @@ impl JsonSerializable for i16 {
132
155
}
133
156
}
134
157
158
+ impl private:: Sealed for i16 { }
135
159
impl ArrowNativeType for i16 {
136
160
#[ inline]
137
161
fn from_usize ( v : usize ) -> Option < Self > {
@@ -155,6 +179,7 @@ impl JsonSerializable for i32 {
155
179
}
156
180
}
157
181
182
+ impl private:: Sealed for i32 { }
158
183
impl ArrowNativeType for i32 {
159
184
#[ inline]
160
185
fn from_usize ( v : usize ) -> Option < Self > {
@@ -184,6 +209,7 @@ impl JsonSerializable for i64 {
184
209
}
185
210
}
186
211
212
+ impl private:: Sealed for i64 { }
187
213
impl ArrowNativeType for i64 {
188
214
#[ inline]
189
215
fn from_usize ( v : usize ) -> Option < Self > {
@@ -217,6 +243,7 @@ impl JsonSerializable for i128 {
217
243
}
218
244
}
219
245
246
+ impl private:: Sealed for i128 { }
220
247
impl ArrowNativeType for i128 {
221
248
#[ inline]
222
249
fn from_usize ( v : usize ) -> Option < Self > {
@@ -246,6 +273,7 @@ impl JsonSerializable for u8 {
246
273
}
247
274
}
248
275
276
+ impl private:: Sealed for u8 { }
249
277
impl ArrowNativeType for u8 {
250
278
#[ inline]
251
279
fn from_usize ( v : usize ) -> Option < Self > {
@@ -269,6 +297,7 @@ impl JsonSerializable for u16 {
269
297
}
270
298
}
271
299
300
+ impl private:: Sealed for u16 { }
272
301
impl ArrowNativeType for u16 {
273
302
#[ inline]
274
303
fn from_usize ( v : usize ) -> Option < Self > {
@@ -292,6 +321,7 @@ impl JsonSerializable for u32 {
292
321
}
293
322
}
294
323
324
+ impl private:: Sealed for u32 { }
295
325
impl ArrowNativeType for u32 {
296
326
#[ inline]
297
327
fn from_usize ( v : usize ) -> Option < Self > {
@@ -315,6 +345,7 @@ impl JsonSerializable for u64 {
315
345
}
316
346
}
317
347
348
+ impl private:: Sealed for u64 { }
318
349
impl ArrowNativeType for u64 {
319
350
#[ inline]
320
351
fn from_usize ( v : usize ) -> Option < Self > {
@@ -351,8 +382,11 @@ impl JsonSerializable for f64 {
351
382
}
352
383
353
384
impl ArrowNativeType for f16 { }
385
+ impl private:: Sealed for f16 { }
354
386
impl ArrowNativeType for f32 { }
387
+ impl private:: Sealed for f32 { }
355
388
impl ArrowNativeType for f64 { }
389
+ impl private:: Sealed for f64 { }
356
390
357
391
/// Allows conversion from supported Arrow types to a byte slice.
358
392
pub trait ToByteSlice {
0 commit comments