hu6360567 opened a new issue #994:
URL: https://github.com/apache/arrow-rs/issues/994


   **Which part is this question about**
   FFI apis and raw pointer management
   
   **Describe your question**
   I'm trying to import/export arrow arrays between C++ and Rust, when it 
crashes when C++ is compiled in release mode.
   ref: https://github.com/apache/arrow/issues/11846
   
   To be confused, when import/expore array/schema on Rust side, the input 
pointer is `const`.
   However, C++ API`arrow::ImportRecordBatch` moves the payload from input 
array/schema pointers.
   Is there any code to show best practice on import/export between Rust and 
C++?
   
   My implementation of exporting arrow array allocated from Rust
   Rust part:
   ```rust
   fn export<T>(array: T, content: *mut *const FFI_ArrowArray, schema: *mut 
*const FFI_ArrowSchema) where T: Array {
       match array.to_raw() {
           Ok((c, s)) => unsafe {
               // Is it right to directly copy pointer address, when does `c` 
and `s` actually dropped?
               // Should I use `copy_to` or `replace` from `std::mem::ptr` ?
               *content = c;
               *schema = s;
           }
           Err(e) => {
               eprintln!("{}", e);
           }
       }
   }
   
   #[no_mangle]
   pub extern "C" fn export_array(content: *mut *const FFI_ArrowArray, schema: 
*mut *const FFI_ArrowSchema) {
       let sa = StringArray::from(vec!["a", "b", "c"]);
   
       export(sa, content, schema);
   }
   ```
   C++ part
   ```C++
   void func(){
       const ArrowArray *content = nullptr;
       const ArrowSchema *schema = nullptr;
       export_array(&content, &schema);
   
       // I have to const_cast the pointers
       auto array = *arrow::ImportRecordBatch(const_cast<ArrowArray 
*>(content), const_cast<ArrowSchema *>(schema));
       std::cout << array->ToString() << std::endl;
   }
   ```
   
   
   **Additional context**
   ref: https://github.com/apache/arrow/issues/11846
   


-- 
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