Add dealloc shim for Box to dealloc without running T's Drop
diff --git a/gen/src/write.rs b/gen/src/write.rs
index fc0872f..cbad93c 100644
--- a/gen/src/write.rs
+++ b/gen/src/write.rs
@@ -1353,6 +1353,11 @@
);
writeln!(
out,
+ "void cxxbridge1$box${}$dealloc({} *) noexcept;",
+ instance, inner,
+ );
+ writeln!(
+ out,
"void cxxbridge1$box${}$drop(::rust::Box<{}> *ptr) noexcept;",
instance, inner,
);
@@ -1420,6 +1425,15 @@
writeln!(out, "}}");
writeln!(out, "template <>");
+ writeln!(
+ out,
+ "void Box<{}>::dealloc({} *ptr) noexcept {{",
+ inner, inner,
+ );
+ writeln!(out, " cxxbridge1$box${}$dealloc(ptr);", instance);
+ writeln!(out, "}}");
+
+ writeln!(out, "template <>");
writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
writeln!(out, " cxxbridge1$box${}$drop(this);", instance);
writeln!(out, "}}");
diff --git a/include/cxx.h b/include/cxx.h
index 34b8771..a1e1700 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -229,6 +229,7 @@
class uninit;
Box(uninit) noexcept;
static T *alloc() noexcept;
+ static void dealloc(T *) noexcept;
void drop() noexcept;
T *ptr;
};
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index 4a17161..433eca6 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -952,10 +952,12 @@
fn expand_rust_box(ident: &RustName, types: &Types) -> TokenStream {
let link_prefix = format!("cxxbridge1$box${}$", types.resolve(ident).to_symbol());
let link_alloc = format!("{}alloc", link_prefix);
+ let link_dealloc = format!("{}dealloc", link_prefix);
let link_drop = format!("{}drop", link_prefix);
let local_prefix = format_ident!("{}__box_", &ident.rust);
let local_alloc = format_ident!("{}alloc", local_prefix);
+ let local_dealloc = format_ident!("{}dealloc", local_prefix);
let local_drop = format_ident!("{}drop", local_prefix);
let span = ident.span();
@@ -968,6 +970,11 @@
::std::boxed::Box::into_raw(::std::boxed::Box::new(::std::mem::MaybeUninit::uninit()))
}
#[doc(hidden)]
+ #[export_name = #link_dealloc]
+ unsafe extern "C" fn #local_dealloc(ptr: *mut ::std::mem::MaybeUninit<#ident>) {
+ ::std::boxed::Box::from_raw(ptr);
+ }
+ #[doc(hidden)]
#[export_name = #link_drop]
unsafe extern "C" fn #local_drop(this: *mut ::std::boxed::Box<#ident>) {
::std::ptr::drop_in_place(this);