diff --git a/gen/src/write.rs b/gen/src/write.rs index 616f90be..8eef0a76 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -1904,6 +1904,15 @@ fn write_cxx_vector(out: &mut OutFile, key: NamedImplKey) { out.include.utility = true; out.builtin.destroy = true; + begin_function_definition(out); + writeln!( + out, + "::std::vector<{}> *cxxbridge1$std$vector${}$new() noexcept {{", + inner, instance, + ); + writeln!(out, " return new ::std::vector<{}>();", inner); + writeln!(out, "}}"); + begin_function_definition(out); writeln!( out, @@ -1944,20 +1953,6 @@ fn write_cxx_vector(out: &mut OutFile, key: NamedImplKey) { writeln!(out, "}}"); } - let ty = UniquePtr::CxxVector(element); - out.include.memory = true; - write_unique_ptr_common(out, ty); - - let inner = ty.to_typename(out.types); - let instance = ty.to_mangled(out.types); - - begin_function_definition(out); - writeln!( - out, - "{} *cxxbridge1$unique_ptr${}$new() noexcept {{", - inner, instance, - ); - writeln!(out, " return new {}();", inner); - writeln!(out, "}}"); + write_unique_ptr_common(out, UniquePtr::CxxVector(element)); } diff --git a/macro/src/expand.rs b/macro/src/expand.rs index 9f605bd3..dcc00810 100644 --- a/macro/src/expand.rs +++ b/macro/src/expand.rs @@ -1613,6 +1613,7 @@ fn expand_cxx_vector( let name = elem.to_string(); let resolve = types.resolve(elem); let prefix = format!("cxxbridge1$std$vector${}$", resolve.name.to_symbol()); + let link_new = format!("{}new", prefix); let link_size = format!("{}size", prefix); let link_get_unchecked = format!("{}get_unchecked", prefix); let link_push_back = format!("{}push_back", prefix); @@ -1622,7 +1623,6 @@ fn expand_cxx_vector( resolve.name.to_symbol(), ); let link_unique_ptr_null = format!("{}null", unique_ptr_prefix); - let link_unique_ptr_new = format!("{}new", unique_ptr_prefix); let link_unique_ptr_raw = format!("{}raw", unique_ptr_prefix); let link_unique_ptr_get = format!("{}get", unique_ptr_prefix); let link_unique_ptr_release = format!("{}release", unique_ptr_prefix); @@ -1673,6 +1673,13 @@ fn expand_cxx_vector( fn __typename(f: &mut ::cxx::core::fmt::Formatter<'_>) -> ::cxx::core::fmt::Result { f.write_str(#name) } + fn __vector_new() -> *mut ::cxx::CxxVector { + extern "C" { + #[link_name = #link_new] + fn __vector_new #impl_generics() -> *mut ::cxx::CxxVector<#elem #ty_generics>; + } + unsafe { __vector_new() } + } fn __vector_size(v: &::cxx::CxxVector) -> usize { extern "C" { #[link_name = #link_size] @@ -1700,13 +1707,6 @@ fn expand_cxx_vector( unsafe { __unique_ptr_null(&mut repr) } repr } - fn __unique_ptr_new() -> *mut ::cxx::CxxVector { - extern "C" { - #[link_name = #link_unique_ptr_new] - fn __unique_ptr_new #impl_generics() -> *mut ::cxx::CxxVector<#elem #ty_generics>; - } - unsafe { __unique_ptr_new() } - } unsafe fn __unique_ptr_raw(raw: *mut ::cxx::CxxVector) -> ::cxx::core::mem::MaybeUninit<*mut ::cxx::core::ffi::c_void> { extern "C" { #[link_name = #link_unique_ptr_raw] diff --git a/src/cxx.cc b/src/cxx.cc index 8c361c08..2522d61a 100644 --- a/src/cxx.cc +++ b/src/cxx.cc @@ -593,6 +593,9 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *), } // namespace #define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \ + std::vector *cxxbridge1$std$vector$##RUST_TYPE##$new() noexcept { \ + return new std::vector(); \ + } \ std::size_t cxxbridge1$std$vector$##RUST_TYPE##$size( \ const std::vector &s) noexcept { \ return s.size(); \ @@ -605,10 +608,6 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *), std::unique_ptr> *ptr) noexcept { \ new (ptr) std::unique_ptr>(); \ } \ - std::vector \ - *cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$new() noexcept { \ - return new std::vector(); \ - } \ void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$raw( \ std::unique_ptr> *ptr, \ std::vector *raw) noexcept { \ diff --git a/src/cxx_vector.rs b/src/cxx_vector.rs index 9587a751..2f8a280e 100644 --- a/src/cxx_vector.rs +++ b/src/cxx_vector.rs @@ -41,7 +41,7 @@ where /// /// The C++ vector is default constructed. pub fn new() -> UniquePtr { - unsafe { UniquePtr::from_raw(T::__unique_ptr_new()) } + unsafe { UniquePtr::from_raw(T::__vector_new()) } } /// Returns the number of elements in the vector. @@ -342,6 +342,8 @@ pub unsafe trait VectorElement: Sized { #[doc(hidden)] fn __typename(f: &mut fmt::Formatter) -> fmt::Result; #[doc(hidden)] + fn __vector_new() -> *mut CxxVector; + #[doc(hidden)] fn __vector_size(v: &CxxVector) -> usize; #[doc(hidden)] unsafe fn __get_unchecked(v: *mut CxxVector, pos: usize) -> *mut Self; @@ -364,8 +366,6 @@ pub unsafe trait VectorElement: Sized { #[doc(hidden)] fn __unique_ptr_null() -> MaybeUninit<*mut c_void>; #[doc(hidden)] - fn __unique_ptr_new() -> *mut CxxVector; - #[doc(hidden)] unsafe fn __unique_ptr_raw(raw: *mut CxxVector) -> MaybeUninit<*mut c_void>; #[doc(hidden)] unsafe fn __unique_ptr_get(repr: MaybeUninit<*mut c_void>) -> *const CxxVector; @@ -408,6 +408,15 @@ macro_rules! impl_vector_element { fn __typename(f: &mut fmt::Formatter) -> fmt::Result { f.write_str($name) } + fn __vector_new() -> *mut CxxVector { + extern "C" { + attr! { + #[link_name = concat!("cxxbridge1$std$vector$", $segment, "$new")] + fn __vector_new() -> *mut CxxVector<$ty>; + } + } + unsafe { __vector_new() } + } fn __vector_size(v: &CxxVector<$ty>) -> usize { extern "C" { attr! { @@ -438,15 +447,6 @@ macro_rules! impl_vector_element { unsafe { __unique_ptr_null(&mut repr) } repr } - fn __unique_ptr_new() -> *mut CxxVector { - extern "C" { - attr! { - #[link_name = concat!("cxxbridge1$unique_ptr$std$vector$", $segment, "$new")] - fn __unique_ptr_new() -> *mut CxxVector<$ty>; - } - } - unsafe { __unique_ptr_new() } - } unsafe fn __unique_ptr_raw(raw: *mut CxxVector) -> MaybeUninit<*mut c_void> { extern "C" { attr! {