pitrou commented on code in PR #5080:
URL: https://github.com/apache/arrow-rs/pull/5080#discussion_r1395499722


##########
arrow-integration-testing/src/lib.rs:
##########
@@ -100,3 +166,147 @@ pub fn read_gzip_json(version: &str, path: &str) -> 
ArrowJson {
     let arrow_json: ArrowJson = serde_json::from_str(&s).unwrap();
     arrow_json
 }
+
+//
+// C Data Integration entrypoints
+//
+
+fn cdata_integration_export_schema_from_json(
+    c_json_name: *const i8,
+    out: *mut FFI_ArrowSchema,
+) -> Result<()> {
+    let json_name = unsafe { CStr::from_ptr(c_json_name) };
+    let f = read_json_file_metadata(json_name.to_str()?)?;
+    let c_schema = FFI_ArrowSchema::try_from(&f.schema)?;
+    // Move exported schema into output struct
+    unsafe { ptr::write(out, c_schema) };
+    Ok(())
+}
+
+fn cdata_integration_export_batch_from_json(
+    c_json_name: *const i8,
+    batch_num: c_int,
+    out: *mut FFI_ArrowArray,
+) -> Result<()> {
+    let json_name = unsafe { CStr::from_ptr(c_json_name) };
+    let b = read_single_batch_from_json_file(json_name.to_str()?, 
batch_num.try_into().unwrap())?;
+    let a = StructArray::from(b).into_data();
+    let c_array = FFI_ArrowArray::new(&a);
+    // Move exported array into output struct
+    unsafe { ptr::write(out, c_array) };
+    Ok(())
+}
+
+fn cdata_integration_import_schema_and_compare_to_json(
+    c_json_name: *const i8,
+    c_schema: *mut FFI_ArrowSchema,
+) -> Result<()> {
+    let json_name = unsafe { CStr::from_ptr(c_json_name) };
+    let json_schema = read_json_file_metadata(json_name.to_str()?)?.schema;
+
+    // The source ArrowSchema will be released when this is dropped
+    let imported_schema = unsafe { std::ptr::replace(c_schema, 
FFI_ArrowSchema::empty()) };

Review Comment:
   This is also why I would suggest higher-level functions to avoid reinventing 
this in every consumer:
   ```rust
   
   /// Import schema from C Data Interface
   ///
   /// # Safety
   ///
   /// This function ensures the given ArrowSchema struct has been filled
   /// by a compliant exporter.
   pub unsafe fn import_schema(c_schema: *mut FFI_ArrowSchema) -> 
Result<Schema> {
       let imported_schema = unsafe { std::ptr::replace(c_schema, 
FFI_ArrowSchema::empty()) };
       Schema::try_from(&imported_schema)
   }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to