Add construction and assignment for rust::Vec
diff --git a/gen/write.rs b/gen/write.rs
index ccd6c2f..cdbfc75 100644
--- a/gen/write.rs
+++ b/gen/write.rs
@@ -1028,6 +1028,11 @@
writeln!(out, "#define CXXBRIDGE02_RUST_VEC_{}", instance);
writeln!(
out,
+ "void cxxbridge02$rust_vec${}$new(const ::rust::Vec<{}> *ptr) noexcept;",
+ instance, inner,
+ );
+ writeln!(
+ out,
"void cxxbridge02$rust_vec${}$drop(::rust::Vec<{}> *ptr) noexcept;",
instance, inner,
);
@@ -1075,6 +1080,11 @@
let instance = to_mangled(&out.namespace, &element);
writeln!(out, "template <>");
+ writeln!(out, "Vec<{}>::Vec() noexcept {{", inner);
+ writeln!(out, " cxxbridge02$rust_vec${}$new(this);", instance);
+ writeln!(out, "}}");
+
+ writeln!(out, "template <>");
writeln!(out, "void Vec<{}>::drop() noexcept {{", inner);
writeln!(
out,
diff --git a/include/cxx.h b/include/cxx.h
index 3dc1c4d..c02d3d9 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -213,8 +213,22 @@
public:
using value_type = T;
+ Vec() noexcept;
+ Vec(Vec &&other) noexcept {
+ this->repr = other.repr;
+ new (&other) Vec();
+ }
~Vec() noexcept { this->drop(); }
+ Vec &operator=(Vec &&other) noexcept {
+ if (this != &other) {
+ this->drop();
+ this->repr = other.repr;
+ new (&other) Vec();
+ }
+ return *this;
+ }
+
size_t size() const noexcept;
bool empty() const noexcept { return size() == 0; }
const T *data() const noexcept;
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index af404d0..2c8cc87 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -543,12 +543,14 @@
fn expand_rust_vec(namespace: &Namespace, elem: &Ident) -> TokenStream {
let link_prefix = format!("cxxbridge02$rust_vec${}{}$", namespace, elem);
+ let link_new = format!("{}new", link_prefix);
let link_drop = format!("{}drop", link_prefix);
let link_len = format!("{}len", link_prefix);
let link_data = format!("{}data", link_prefix);
let link_stride = format!("{}stride", link_prefix);
let local_prefix = format_ident!("{}__vec_", elem);
+ let local_new = format_ident!("{}new", local_prefix);
let local_drop = format_ident!("{}drop", local_prefix);
let local_len = format_ident!("{}len", local_prefix);
let local_data = format_ident!("{}data", local_prefix);
@@ -557,6 +559,11 @@
let span = elem.span();
quote_spanned! {span=>
#[doc(hidden)]
+ #[export_name = #link_new]
+ unsafe extern "C" fn #local_new(this: *mut ::cxx::private::RustVec<#elem>) {
+ ::std::ptr::write(this, ::cxx::private::RustVec::new());
+ }
+ #[doc(hidden)]
#[export_name = #link_drop]
unsafe extern "C" fn #local_drop(this: *mut ::cxx::private::RustVec<#elem>) {
::std::ptr::drop_in_place(this);
diff --git a/src/cxx.cc b/src/cxx.cc
index 63b2166..7465f56 100644
--- a/src/cxx.cc
+++ b/src/cxx.cc
@@ -237,6 +237,8 @@
}
#define RUST_VEC_EXTERNS(RUST_TYPE, CXX_TYPE) \
+ void cxxbridge02$rust_vec$##RUST_TYPE##$new( \
+ rust::Vec<CXX_TYPE> *ptr) noexcept; \
void cxxbridge02$rust_vec$##RUST_TYPE##$drop( \
rust::Vec<CXX_TYPE> *ptr) noexcept; \
size_t cxxbridge02$rust_vec$##RUST_TYPE##$len( \
@@ -247,6 +249,10 @@
#define RUST_VEC_OPS(RUST_TYPE, CXX_TYPE) \
template <> \
+ rust::Vec<CXX_TYPE>::Vec() noexcept { \
+ cxxbridge02$rust_vec$##RUST_TYPE##$new(this); \
+ } \
+ template <> \
void rust::Vec<CXX_TYPE>::drop() noexcept { \
return cxxbridge02$rust_vec$##RUST_TYPE##$drop(this); \
} \
diff --git a/src/rust_vec.rs b/src/rust_vec.rs
index 564851e..d5de489 100644
--- a/src/rust_vec.rs
+++ b/src/rust_vec.rs
@@ -7,6 +7,10 @@
}
impl<T> RustVec<T> {
+ pub fn new() -> Self {
+ RustVec { repr: Vec::new() }
+ }
+
pub fn from(v: Vec<T>) -> Self {
RustVec { repr: v }
}
@@ -43,6 +47,12 @@
const _: () = {
attr! {
+ #[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$new")]
+ unsafe extern "C" fn __new(this: *mut RustVec<$ty>) {
+ ptr::write(this, RustVec::new());
+ }
+ }
+ attr! {
#[export_name = concat!("cxxbridge02$rust_vec$", stringify!($ty), "$drop")]
unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) {
ptr::drop_in_place(this);