Skip to content

Commit 4deebe6

Browse files
committed
Add unsafe markers
1 parent dcbf4f8 commit 4deebe6

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

arrow-integration-testing/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ fn cdata_integration_import_batch_and_compare_to_json(
244244
read_single_batch_from_json_file(json_name.to_str()?, batch_num.try_into().unwrap())?;
245245
let schema = json_batch.schema();
246246

247+
let data_type_for_import = DataType::Struct(schema.fields.clone());
247248
let imported_array = unsafe { FFI_ArrowArray::from_raw(c_array) };
248-
let imported_array =
249-
from_ffi_and_data_type(imported_array, DataType::Struct(schema.fields.clone()))?;
249+
let imported_array = unsafe { from_ffi_and_data_type(imported_array, data_type_for_import) }?;
250250
imported_array.validate_full()?;
251251
let imported_batch = RecordBatch::from(StructArray::from(imported_array));
252252

arrow/src/array/ffi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ mod tests {
7070
let schema = FFI_ArrowSchema::try_from(expected.data_type())?;
7171

7272
// simulate an external consumer by being the consumer
73-
let result = &from_ffi(array, &schema)?;
73+
let result = &unsafe { from_ffi(array, &schema) }?;
7474

7575
assert_eq!(result, expected);
7676
Ok(())

arrow/src/ffi.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
//! let (out_array, out_schema) = to_ffi(&data)?;
4444
//!
4545
//! // import it
46-
//! let data = from_ffi(out_array, &out_schema)?;
46+
//! let data = unsafe { from_ffi(out_array, &out_schema) }?;
4747
//! let array = Int32Array::from(data);
4848
//!
4949
//! // perform some operation
@@ -80,7 +80,7 @@
8080
//! let mut schema = FFI_ArrowSchema::empty();
8181
//! let mut array = FFI_ArrowArray::empty();
8282
//! foreign.export_to_c(addr_of_mut!(array), addr_of_mut!(schema));
83-
//! Ok(make_array(from_ffi(array, &schema)?))
83+
//! Ok(make_array(unsafe { from_ffi(array, &schema) }?))
8484
//! }
8585
//! ```
8686
@@ -234,7 +234,7 @@ pub fn to_ffi(data: &ArrayData) -> Result<(FFI_ArrowArray, FFI_ArrowSchema)> {
234234
/// # Safety
235235
///
236236
/// This struct assumes that the incoming data agrees with the C data interface.
237-
pub fn from_ffi(array: FFI_ArrowArray, schema: &FFI_ArrowSchema) -> Result<ArrayData> {
237+
pub unsafe fn from_ffi(array: FFI_ArrowArray, schema: &FFI_ArrowSchema) -> Result<ArrayData> {
238238
let dt = DataType::try_from(schema)?;
239239
let array = Arc::new(array);
240240
let tmp = ImportedArrowArray {
@@ -250,7 +250,10 @@ pub fn from_ffi(array: FFI_ArrowArray, schema: &FFI_ArrowSchema) -> Result<Array
250250
/// # Safety
251251
///
252252
/// This struct assumes that the incoming data agrees with the C data interface.
253-
pub fn from_ffi_and_data_type(array: FFI_ArrowArray, data_type: DataType) -> Result<ArrayData> {
253+
pub unsafe fn from_ffi_and_data_type(
254+
array: FFI_ArrowArray,
255+
data_type: DataType,
256+
) -> Result<ArrayData> {
254257
let array = Arc::new(array);
255258
let tmp = ImportedArrowArray {
256259
array: &array,
@@ -487,7 +490,7 @@ mod tests {
487490
let (array, schema) = to_ffi(&array.into_data()).unwrap();
488491

489492
// (simulate consumer) import it
490-
let array = Int32Array::from(from_ffi(array, &schema).unwrap());
493+
let array = Int32Array::from(unsafe { from_ffi(array, &schema) }.unwrap());
491494
let array = kernels::numeric::add(&array, &array).unwrap();
492495

493496
// verify
@@ -531,7 +534,7 @@ mod tests {
531534
let (array, schema) = to_ffi(&array.to_data())?;
532535

533536
// (simulate consumer) import it
534-
let data = from_ffi(array, &schema)?;
537+
let data = unsafe { from_ffi(array, &schema) }?;
535538
let array = make_array(data);
536539

537540
// perform some operation
@@ -561,7 +564,7 @@ mod tests {
561564
let (array, schema) = to_ffi(&original_array.to_data())?;
562565

563566
// (simulate consumer) import it
564-
let data = from_ffi(array, &schema)?;
567+
let data = unsafe { from_ffi(array, &schema) }?;
565568
let array = make_array(data);
566569

567570
// perform some operation
@@ -583,7 +586,7 @@ mod tests {
583586
let (array, schema) = to_ffi(&array.to_data())?;
584587

585588
// (simulate consumer) import it
586-
let data = from_ffi(array, &schema)?;
589+
let data = unsafe { from_ffi(array, &schema) }?;
587590
let array = make_array(data);
588591

589592
// perform some operation
@@ -652,7 +655,7 @@ mod tests {
652655
let (array, schema) = to_ffi(&array.to_data())?;
653656

654657
// (simulate consumer) import it
655-
let data = from_ffi(array, &schema)?;
658+
let data = unsafe { from_ffi(array, &schema) }?;
656659
let array = make_array(data);
657660

658661
// downcast
@@ -692,7 +695,7 @@ mod tests {
692695
let (array, schema) = to_ffi(&array.to_data())?;
693696

694697
// (simulate consumer) import it
695-
let data = from_ffi(array, &schema)?;
698+
let data = unsafe { from_ffi(array, &schema) }?;
696699
let array = make_array(data);
697700

698701
// perform some operation
@@ -737,7 +740,7 @@ mod tests {
737740
let (array, schema) = to_ffi(&array.to_data())?;
738741

739742
// (simulate consumer) import it
740-
let data = from_ffi(array, &schema)?;
743+
let data = unsafe { from_ffi(array, &schema) }?;
741744
let array = make_array(data);
742745

743746
// perform some operation
@@ -763,7 +766,7 @@ mod tests {
763766
let (array, schema) = to_ffi(&array.to_data())?;
764767

765768
// (simulate consumer) import it
766-
let data = from_ffi(array, &schema)?;
769+
let data = unsafe { from_ffi(array, &schema) }?;
767770
let array = make_array(data);
768771

769772
// perform some operation
@@ -792,7 +795,7 @@ mod tests {
792795
let (array, schema) = to_ffi(&array.to_data())?;
793796

794797
// (simulate consumer) import it
795-
let data = from_ffi(array, &schema)?;
798+
let data = unsafe { from_ffi(array, &schema) }?;
796799
let array = make_array(data);
797800

798801
// perform some operation
@@ -828,7 +831,7 @@ mod tests {
828831
let (array, schema) = to_ffi(&array.to_data())?;
829832

830833
// (simulate consumer) import it
831-
let data = from_ffi(array, &schema)?;
834+
let data = unsafe { from_ffi(array, &schema) }?;
832835
let array = make_array(data);
833836

834837
// perform some operation
@@ -889,7 +892,7 @@ mod tests {
889892
let (array, schema) = to_ffi(&list_data)?;
890893

891894
// (simulate consumer) import it
892-
let data = from_ffi(array, &schema)?;
895+
let data = unsafe { from_ffi(array, &schema) }?;
893896
let array = make_array(data);
894897

895898
// perform some operation
@@ -934,7 +937,7 @@ mod tests {
934937
let (array, schema) = to_ffi(&dict_array.to_data())?;
935938

936939
// (simulate consumer) import it
937-
let data = from_ffi(array, &schema)?;
940+
let data = unsafe { from_ffi(array, &schema) }?;
938941
let array = make_array(data);
939942

940943
// perform some operation
@@ -972,7 +975,7 @@ mod tests {
972975
}
973976

974977
// (simulate consumer) import it
975-
let data = from_ffi(out_array, &out_schema)?;
978+
let data = unsafe { from_ffi(out_array, &out_schema) }?;
976979
let array = make_array(data);
977980

978981
// perform some operation
@@ -993,7 +996,7 @@ mod tests {
993996
let (array, schema) = to_ffi(&array.to_data())?;
994997

995998
// (simulate consumer) import it
996-
let data = from_ffi(array, &schema)?;
999+
let data = unsafe { from_ffi(array, &schema) }?;
9971000
let array = make_array(data);
9981001

9991002
// perform some operation
@@ -1030,7 +1033,7 @@ mod tests {
10301033
let (array, schema) = to_ffi(&map_array.to_data())?;
10311034

10321035
// (simulate consumer) import it
1033-
let data = from_ffi(array, &schema)?;
1036+
let data = unsafe { from_ffi(array, &schema) }?;
10341037
let array = make_array(data);
10351038

10361039
// perform some operation
@@ -1053,7 +1056,7 @@ mod tests {
10531056
let (array, schema) = to_ffi(&struct_array.to_data())?;
10541057

10551058
// (simulate consumer) import it
1056-
let data = from_ffi(array, &schema)?;
1059+
let data = unsafe { from_ffi(array, &schema) }?;
10571060
let array = make_array(data);
10581061

10591062
// perform some operation
@@ -1077,7 +1080,7 @@ mod tests {
10771080
let (array, schema) = to_ffi(&union.to_data())?;
10781081

10791082
// (simulate consumer) import it
1080-
let data = from_ffi(array, &schema)?;
1083+
let data = unsafe { from_ffi(array, &schema) }?;
10811084
let array = make_array(data);
10821085

10831086
let array = array.as_any().downcast_ref::<UnionArray>().unwrap();
@@ -1138,7 +1141,7 @@ mod tests {
11381141
let (array, schema) = to_ffi(&union.to_data())?;
11391142

11401143
// (simulate consumer) import it
1141-
let data = from_ffi(array, &schema)?;
1144+
let data = unsafe { from_ffi(array, &schema) }?;
11421145
let array = UnionArray::from(data);
11431146

11441147
let expected_type_ids = vec![0_i8, 0, 1, 0];

arrow/src/ffi_stream.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,11 @@ impl Iterator for ArrowArrayStreamReader {
357357
}
358358

359359
let schema_ref = self.schema();
360+
// NOTE: this parses the FFI_ArrowSchema again on each iterator call;
361+
// should probably use from_ffi_and_data_type() instead.
360362
let schema = FFI_ArrowSchema::try_from(schema_ref.as_ref()).ok()?;
361363

362-
let data = from_ffi(array, &schema).ok()?;
364+
let data = unsafe { from_ffi(array, &schema) }.ok()?;
363365

364366
let record_batch = RecordBatch::from(StructArray::from(data));
365367

@@ -464,7 +466,7 @@ mod tests {
464466
break;
465467
}
466468

467-
let array = from_ffi(ffi_array, &ffi_schema).unwrap();
469+
let array = unsafe { from_ffi(ffi_array, &ffi_schema) }.unwrap();
468470

469471
let record_batch = RecordBatch::from(StructArray::from(array));
470472
produced_batches.push(record_batch);

0 commit comments

Comments
 (0)