You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2020/09/28 11:17:09 UTC

[GitHub] [arrow] pitrou commented on a change in pull request #8287: ARROW-10111: [Rust] Added new crate with code that consumes C Data interface

pitrou commented on a change in pull request #8287:
URL: https://github.com/apache/arrow/pull/8287#discussion_r495864086



##########
File path: rust/arrow/src/array/ffi.rs
##########
@@ -0,0 +1,163 @@
+use std::sync::Arc;
+
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+/// ABI-compatible struct for FFI_ArrowSchema from C Data Interface
+#[repr(C)]
+#[derive(Debug, Clone)]
+pub struct FFI_ArrowSchema {
+    format: *const ::std::os::raw::c_char,
+    name: *const ::std::os::raw::c_char,
+    metadata: *const ::std::os::raw::c_char,
+    flags: i64,
+    n_children: i64,
+    children: *mut *mut FFI_ArrowSchema,
+    dictionary: *mut FFI_ArrowSchema,
+    release: ::std::option::Option<unsafe extern "C" fn(arg1: *mut FFI_ArrowSchema)>,
+    private_data: *mut ::std::os::raw::c_void,
+}
+
+impl FFI_ArrowSchema {
+    /// allocates this struct. Unsafe as the consumer is responsible for owning it.
+    pub fn allocate() -> *mut Self {
+        Box::into_raw(Box::new(Self::new()))
+    }
+
+    fn new() -> Self {
+        Self {
+            format: std::ptr::null_mut(),
+            name: std::ptr::null_mut(),
+            metadata: std::ptr::null_mut(),
+            flags: 0,
+            n_children: 0,
+            children: std::ptr::null_mut(),
+            dictionary: std::ptr::null_mut(),
+            release: None,
+            private_data: std::ptr::null_mut(),
+        }
+    }
+}
+
+impl Drop for FFI_ArrowSchema {
+    fn drop(&mut self) {
+        match self.release {
+            None => (),
+            Some(release) => unsafe { release(self) },
+        };
+    }
+}
+
+/// ABI-compatible struct for ArrowArray from C Data Interface
+#[repr(C)]
+#[derive(Debug, Clone)]
+pub struct FFI_ArrowArray {
+    length: i64,
+    null_count: i64,
+    offset: i64,
+    n_buffers: i64,
+    n_children: i64,
+    buffers: *mut *const ::std::os::raw::c_void,
+    children: *mut *mut FFI_ArrowArray,
+    dictionary: *mut FFI_ArrowArray,
+    release: ::std::option::Option<unsafe extern "C" fn(arg1: *mut FFI_ArrowArray)>,
+    private_data: *mut ::std::os::raw::c_void,
+}
+
+impl FFI_ArrowArray {
+    /// allocates this struct. Unsafe as the consumer is responsible for owning it.
+    pub fn allocate() -> *mut Self {
+        Box::into_raw(Box::new(Self::new()))
+    }
+
+    fn new() -> Self {
+        Self {
+            length: 0,
+            null_count: 0,
+            offset: 0,
+            n_buffers: 0,
+            n_children: 0,
+            buffers: std::ptr::null_mut(),
+            children: std::ptr::null_mut(),
+            dictionary: std::ptr::null_mut(),
+            release: None,
+            private_data: std::ptr::null_mut(),
+        }
+    }
+}
+
+impl Drop for FFI_ArrowArray {
+    fn drop(&mut self) {
+        match self.release {
+            None => (),
+            Some(release) => unsafe { release(self) },
+        };
+    }
+}
+
+/// Struct containing the necessary data and schema to move an Array from and to the C Data Interface.
+#[derive(Debug, Clone)]
+pub struct ArrowArray {
+    array: Arc<FFI_ArrowArray>,
+    schema: Arc<FFI_ArrowSchema>,
+}
+
+impl ArrowArray {
+    /// constructs an `ArrowArray` from two raw pointers.
+    pub fn allocate() -> (*mut FFI_ArrowArray, *mut FFI_ArrowSchema) {
+        (FFI_ArrowArray::allocate(), FFI_ArrowSchema::allocate())
+    }
+
+    pub unsafe fn from_raw_pointers(
+        array: *mut FFI_ArrowArray,
+        schema: *mut FFI_ArrowSchema,
+    ) -> Self {
+        ArrowArray {
+            array: Arc::from_raw(array),
+            schema: Arc::from_raw(schema),

Review comment:
       Please excuse my ignorance, but according to https://doc.rust-lang.org/beta/std/sync/struct.Arc.html#method.from_raw , this only works if the pointers come from `to_raw_pointers`?




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org