kszucs commented on a change in pull request #439: URL: https://github.com/apache/arrow-rs/pull/439#discussion_r655509305
########## File path: arrow/src/ffi.rs ########## @@ -122,66 +132,61 @@ unsafe extern "C" fn release_schema(schema: *mut FFI_ArrowSchema) { let schema = &mut *schema; // take ownership back to release it. - CString::from_raw(schema.format as *mut std::os::raw::c_char); - CString::from_raw(schema.name as *mut std::os::raw::c_char); - let private = Box::from_raw(schema.private_data as *mut SchemaPrivateData); - for child in private.children_ptr.iter() { - let _ = Box::from_raw(*child); + CString::from_raw(schema.format as *mut c_char); + if !schema.name.is_null() { + CString::from_raw(schema.name as *mut c_char); + } + if !schema.private_data.is_null() { + let private_data = Box::from_raw(schema.private_data as *mut SchemaPrivateData); + for child in private_data.children.iter() { + drop(Box::from_raw(*child)) + } + drop(private_data); } schema.release = None; } impl FFI_ArrowSchema { /// create a new [`Ffi_ArrowSchema`]. This fails if the fields' [`DataType`] is not supported. - fn try_new(field: Field) -> Result<FFI_ArrowSchema> { - let format = to_format(field.data_type())?; - let name = field.name().clone(); - - // allocate (and hold) the children - let children_vec = match field.data_type() { - DataType::List(field) => { - vec![Box::new(FFI_ArrowSchema::try_new(field.as_ref().clone())?)] - } - DataType::LargeList(field) => { - vec![Box::new(FFI_ArrowSchema::try_new(field.as_ref().clone())?)] - } - DataType::Struct(fields) => fields - .iter() - .map(|field| Ok(Box::new(FFI_ArrowSchema::try_new(field.clone())?))) - .collect::<Result<Vec<_>>>()?, - _ => vec![], - }; - // note: this cannot be done along with the above because the above is fallible and this op leaks. - let children_ptr = children_vec + pub fn try_new(format: &str, children: Vec<FFI_ArrowSchema>) -> Result<Self> { + let mut this = Self::empty(); + + // note: this op leaks. + let mut children_ptr = children .into_iter() + .map(Box::new) .map(Box::into_raw) .collect::<Box<_>>(); - let n_children = children_ptr.len() as i64; - let flags = field.is_nullable() as i64 * 2; + this.format = CString::new(format).unwrap().into_raw(); + this.release = Some(release_schema); + this.n_children = children_ptr.len() as i64; + this.children = children_ptr.as_mut_ptr(); - let mut private = Box::new(SchemaPrivateData { - field, - children_ptr, + let private_data = Box::new(SchemaPrivateData { + children: children_ptr, }); + this.private_data = Box::into_raw(private_data) as *mut c_void; - // <https://arrow.apache.org/docs/format/CDataInterface.html#c.ArrowSchema> - Ok(FFI_ArrowSchema { - format: CString::new(format).unwrap().into_raw(), - name: CString::new(name).unwrap().into_raw(), - metadata: std::ptr::null_mut(), - flags, - n_children, - children: private.children_ptr.as_mut_ptr(), - dictionary: std::ptr::null_mut(), - release: Some(release_schema), - private_data: Box::into_raw(private) as *mut ::std::os::raw::c_void, - }) + Ok(this) + } + + pub fn with_name(mut self, name: &str) -> Result<Self> { + self.name = CString::new(name).unwrap().into_raw(); + Ok(self) + } + + pub fn with_flags(mut self, flags: Flags) -> Result<Self> { + self.flags = flags.bits(); + Ok(self) } + // pub fn with_dictionary() {} + // pub fn with_metadata() {} Review comment: Metadata: https://github.com/apache/arrow-rs/issues/478 Dictionary: https://github.com/apache/arrow-rs/issues/479 -- 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: us...@infra.apache.org