chitralverma commented on code in PR #9335:
URL: https://github.com/apache/opendal/pull/9335#discussion_r4725808999


##########
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:
   (AI-generated reply)
   
   Fixed in f0fc112e37c5042a3528480f37ad2af0aee2ce6c. The template now 
generates `opendal_list_options::to_core` as a `pub(crate) unsafe fn` instead 
of the `From` impl. Its `# Safety` section requires a non-null `start_after` to 
point to a readable, nul-terminated C string for the duration of the copy. 
`opendal_operator_list_with_cancel` calls it in its existing unsafe context, 
with a `// SAFETY:` comment that cites that caller contract. 
`test_list_options_to_core` (renamed from `test_list_options_into_core`) uses 
the new function, and a `dev` test fails if the template emits a `From` impl 
again. `opendal.h` is byte-identical to the previous head.
   



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