@@ -774,15 +774,26 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
774
774
writeln ! ( w, "\t /// this to be true and invalidate the object pointed to by inner." ) . unwrap ( ) ;
775
775
writeln ! ( w, "\t pub is_owned: bool," ) . unwrap ( ) ;
776
776
writeln ! ( w, "}}\n " ) . unwrap ( ) ;
777
+
778
+ writeln ! ( w, "impl core::ops::Deref for {} {{" , struct_name) . unwrap ( ) ;
779
+ writeln ! ( w, "\t type Target = native{};" , struct_name) . unwrap ( ) ;
780
+ writeln ! ( w, "\t fn deref(&self) -> &Self::Target {{ unsafe {{ &*ObjOps::untweak_ptr(self.inner) }} }}" ) . unwrap ( ) ;
781
+ writeln ! ( w, "}}" ) . unwrap ( ) ;
782
+
783
+ writeln ! ( w, "unsafe impl core::marker::Send for {} {{ }}" , struct_name) . unwrap ( ) ;
784
+ writeln ! ( w, "unsafe impl core::marker::Sync for {} {{ }}" , struct_name) . unwrap ( ) ;
785
+
777
786
writeln ! ( w, "impl Drop for {} {{\n \t fn drop(&mut self) {{" , struct_name) . unwrap ( ) ;
778
787
writeln ! ( w, "\t \t if self.is_owned && !<*mut native{}>::is_null(self.inner) {{" , ident) . unwrap ( ) ;
779
788
writeln ! ( w, "\t \t \t let _ = unsafe {{ Box::from_raw(ObjOps::untweak_ptr(self.inner)) }};\n \t \t }}\n \t }}\n }}" ) . unwrap ( ) ;
789
+
780
790
writeln ! ( w, "/// Frees any resources used by the {}, if is_owned is set and inner is non-NULL." , struct_name) . unwrap ( ) ;
781
791
writeln ! ( w, "#[no_mangle]\n pub extern \" C\" fn {}_free(this_obj: {}) {{ }}" , struct_name, struct_name) . unwrap ( ) ;
782
792
writeln ! ( w, "#[allow(unused)]" ) . unwrap ( ) ;
783
793
writeln ! ( w, "/// Used only if an object of this type is returned as a trait impl by a method" ) . unwrap ( ) ;
784
794
writeln ! ( w, "pub(crate) extern \" C\" fn {}_free_void(this_ptr: *mut c_void) {{" , struct_name) . unwrap ( ) ;
785
795
writeln ! ( w, "\t let _ = unsafe {{ Box::from_raw(this_ptr as *mut native{}) }};\n }}" , struct_name) . unwrap ( ) ;
796
+
786
797
writeln ! ( w, "#[allow(unused)]" ) . unwrap ( ) ;
787
798
writeln ! ( w, "impl {} {{" , struct_name) . unwrap ( ) ;
788
799
writeln ! ( w, "\t pub(crate) fn get_native_ref(&self) -> &'static native{} {{" , struct_name) . unwrap ( ) ;
@@ -797,6 +808,9 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
797
808
writeln ! ( w, "\t \t let ret = ObjOps::untweak_ptr(self.inner);" ) . unwrap ( ) ;
798
809
writeln ! ( w, "\t \t self.inner = core::ptr::null_mut();" ) . unwrap ( ) ;
799
810
writeln ! ( w, "\t \t ret" ) . unwrap ( ) ;
811
+ writeln ! ( w, "\t }}" ) . unwrap ( ) ;
812
+ writeln ! ( w, "\t pub(crate) fn as_ref_to(&self) -> Self {{" ) . unwrap ( ) ;
813
+ writeln ! ( w, "\t \t Self {{ inner: self.inner, is_owned: false }}" ) . unwrap ( ) ;
800
814
writeln ! ( w, "\t }}\n }}" ) . unwrap ( ) ;
801
815
802
816
write_cpp_wrapper ( cpp_headers, & format ! ( "{}" , ident) , true , None ) ;
0 commit comments