kenz-gelsoft / wxRust2

re-exploration Rust binding to wx
MIT License
78 stars 10 forks source link

Returns WeakRef for wx managed pointers #52

Closed kenz-gelsoft closed 2 years ago

kenz-gelsoft commented 2 years ago

closes https://github.com/kenz-gelsoft/wxRust2/issues/51

kenz-gelsoft commented 2 years ago

Using ManuallyDrop for maybe unowned pointers is not safe.

diff --git a/codegen2.py b/codegen2.py
index 3d528a9..b922772 100644
--- a/codegen2.py
+++ b/codegen2.py
@@ -48,6 +48,7 @@ def generated_rs(classes, config, libname):
 #![allow(unused_parens)]

 use std::mem;
+use std::mem::ManuallyDrop;
 use std::os::raw::{c_double, c_int, c_long, c_uchar, c_void};
 use std::ptr;

diff --git a/doxybindgen/binding.py b/doxybindgen/binding.py
index db9694a..d483ce6 100644
--- a/doxybindgen/binding.py
+++ b/doxybindgen/binding.py
@@ -73,8 +73,8 @@ class RustClassBinding:
         yield "    pub fn none() -> Option<&'static Self> {"
         yield '        None'
         yield '    }'
-        yield '    pub unsafe fn from_ptr(ptr: *mut c_void) -> Self {'
-        yield '        %s(ptr)' % (unprefixed,)
+        yield '    pub unsafe fn from_ptr(ptr: *mut c_void) -> ManuallyDrop<Self> {'
+        yield '        ManuallyDrop::new(%s(ptr))' % (unprefixed,)
         yield '    }'
         yield '}'

@@ -241,6 +241,8 @@ class RustMethodBinding:
                 returns = wrapped[2:]
                 if self.__model.returns.is_str():
                     returns = 'String'
+                if self.__model.returns.is_ptr_to_binding():
+                    returns = 'ManuallyDrop<%s>' % (returns,)
         return ' -> %s' % (returns,)

     def _make_params_generic(self):
kenz-gelsoft commented 2 years ago

Recreated another PR https://github.com/kenz-gelsoft/wxRust2/pull/56

close without merge.