Xuanwo commented on code in PR #9335: URL: https://github.com/apache/opendal/pull/9335#discussion_r4725790200
########## dev/src/generate/c/options_rs.j2: ########## @@ -0,0 +1,133 @@ +// 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. + +// DO NOT EDIT IT MANUALLY. This file is generated by opendal/dev/src/generate/c.rs. + +use std::ffi::{CStr, CString}; +use std::os::raw::c_char; + +use opendal::options; +{%- for o in options %} +{% for line in o.c_doc %} +///{{ " " ~ line if line else "" }} +{%- endfor %} +#[repr(C)] +pub struct {{ o.c_name }} { +{%- for f in o.fields %} +{%- for line in f.c_doc %} + ///{{ " " ~ line if line else "" }} +{%- endfor %} + pub {{ f.name }}: {{ f.c_type }}, +{%- endfor %} +} + +impl {{ o.c_name }} { +{%- for line in o.new_doc %} + ///{{ " " ~ line if line else "" }} +{%- endfor %} + #[no_mangle] + pub extern "C" fn {{ o.c_name }}_new() -> *mut Self { + Box::into_raw(Box::new(Self::default())) + } +{%- for f in o.fields %} +{% for line in f.setter_doc %} + ///{{ " " ~ line if line else "" }} +{%- endfor %} + #[no_mangle] + pub unsafe extern "C" fn {{ o.c_name }}_set_{{ f.name }}( + opts: *mut {{ o.c_name }}, +{%- if f.repr == "owned_string" %} + {{ f.name }}: *const c_char, + ) { + if opts.is_null() { + return; + } + // Allocate before freeing the old value so self-aliasing or substring + // pointers into `opts->{{ f.name }}` remain valid while reading. + let new_ptr = if {{ f.name }}.is_null() { + std::ptr::null_mut() + } else { + let value = option_str({{ f.name }}, "{{ f.name }}"); + CString::new(value).unwrap().into_raw() + }; + let old_ptr = std::mem::replace(&mut (*opts).{{ f.name }}, new_ptr); + if !old_ptr.is_null() { + drop(CString::from_raw(old_ptr)); + } + } +{%- else %} + {{ f.name }}: {{ f.c_type }}, + ) { + if !opts.is_null() { + (*opts).{{ f.name }} = {{ f.name }}; + } + } +{%- endif %} +{%- endfor %} +{% for line in o.free_doc %} + ///{{ " " ~ line if line else "" }} +{%- endfor %} + #[no_mangle] + pub unsafe extern "C" fn {{ o.c_name }}_free(opts: *mut {{ o.c_name }}) { + if !opts.is_null() { + let o = &mut *opts; +{%- for f in o.fields if f.repr == "owned_string" %} + if !o.{{ f.name }}.is_null() { + drop(CString::from_raw(o.{{ f.name }})); + o.{{ f.name }} = std::ptr::null_mut(); + } +{%- endfor %} + drop(Box::from_raw(opts)); + } + } +} + +impl Default for {{ o.c_name }} { + fn default() -> Self { + Self { +{%- for f in o.fields %} + {{ f.name }}: {{ f.c_default }}, +{%- endfor %} + } + } +} + +impl From<&{{ o.c_name }}> for options::{{ o.core }} { Review Comment: The generated `From<&opendal_list_options>` lets safe Rust copy `start_after`, but a shared reference to the options struct cannot guarantee that its public pointer is readable. `From::from` cannot require an unsafe call. Could the template generate a `pub(crate) unsafe fn` for this conversion and document that a non-null `start_after` must point to a readable NUL-terminated C string for the duration of the copy? `opendal_operator_list_with_cancel` can invoke it under the same caller contract as the original inline conversion. -- 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]
