From f41eb899dc42d79e9b2edddcdab6b80057643ecf Mon Sep 17 00:00:00 2001 From: Cory Grinstead Date: Thu, 20 Jun 2024 12:13:03 -0500 Subject: [PATCH] chore(rust): lock toolchain & fix clippy (#1389) - fix some clippy errors from ci running a different toolchain. - add some saftey notes about some unsafe blocks. - locks the toolchain so that it is consistent across dev and CI. --- rust-toolchain.toml | 2 ++ rust/lancedb/src/polars_arrow_convertors.rs | 14 ++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 00000000..628740b1 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "1.79.0" diff --git a/rust/lancedb/src/polars_arrow_convertors.rs b/rust/lancedb/src/polars_arrow_convertors.rs index 79db4fa2..91383037 100644 --- a/rust/lancedb/src/polars_arrow_convertors.rs +++ b/rust/lancedb/src/polars_arrow_convertors.rs @@ -84,7 +84,8 @@ pub fn convert_polars_arrow_array_to_arrow_rs_array( arrow_datatype: arrow_schema::DataType, ) -> std::result::Result { let polars_c_array = polars_arrow::ffi::export_array_to_c(polars_array); - let arrow_c_array = unsafe { mem::transmute(polars_c_array) }; + // Safety: `polars_arrow::ffi::ArrowArray` has the same memory layout as `arrow::ffi::FFI_ArrowArray`. + let arrow_c_array: arrow_data::ffi::FFI_ArrowArray = unsafe { mem::transmute(polars_c_array) }; Ok(arrow_array::make_array(unsafe { arrow::ffi::from_ffi_and_data_type(arrow_c_array, arrow_datatype) }?)) @@ -96,7 +97,8 @@ fn convert_arrow_rs_array_to_polars_arrow_array( polars_arrow_dtype: polars::datatypes::ArrowDataType, ) -> Result> { let arrow_c_array = arrow::ffi::FFI_ArrowArray::new(&arrow_rs_array.to_data()); - let polars_c_array = unsafe { mem::transmute(arrow_c_array) }; + // Safety: `polars_arrow::ffi::ArrowArray` has the same memory layout as `arrow::ffi::FFI_ArrowArray`. + let polars_c_array: polars_arrow::ffi::ArrowArray = unsafe { mem::transmute(arrow_c_array) }; Ok(unsafe { polars_arrow::ffi::import_array_from_c(polars_c_array, polars_arrow_dtype) }?) } @@ -104,7 +106,9 @@ fn convert_polars_arrow_field_to_arrow_rs_field( polars_arrow_field: polars_arrow::datatypes::Field, ) -> Result { let polars_c_schema = polars_arrow::ffi::export_field_to_c(&polars_arrow_field); - let arrow_c_schema: arrow::ffi::FFI_ArrowSchema = unsafe { mem::transmute(polars_c_schema) }; + // Safety: `polars_arrow::ffi::ArrowSchema` has the same memory layout as `arrow::ffi::FFI_ArrowSchema`. + let arrow_c_schema: arrow::ffi::FFI_ArrowSchema = + unsafe { mem::transmute::<_, _>(polars_c_schema) }; let arrow_rs_dtype = arrow_schema::DataType::try_from(&arrow_c_schema)?; Ok(arrow_schema::Field::new( polars_arrow_field.name, @@ -118,6 +122,8 @@ fn convert_arrow_rs_field_to_polars_arrow_field( ) -> Result { let arrow_rs_dtype = arrow_rs_field.data_type(); let arrow_c_schema = arrow::ffi::FFI_ArrowSchema::try_from(arrow_rs_dtype)?; - let polars_c_schema: polars_arrow::ffi::ArrowSchema = unsafe { mem::transmute(arrow_c_schema) }; + // Safety: `polars_arrow::ffi::ArrowSchema` has the same memory layout as `arrow::ffi::FFI_ArrowSchema`. + let polars_c_schema: polars_arrow::ffi::ArrowSchema = + unsafe { mem::transmute::<_, _>(arrow_c_schema) }; Ok(unsafe { polars_arrow::ffi::import_field_from_c(&polars_c_schema) }?) }