Skip to content

Commit

Permalink
Remove unique_ptr from name of std::vector<>::new symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 29, 2023
1 parent 4213e92 commit 2ff5e37
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 39 deletions.
25 changes: 10 additions & 15 deletions gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
16 changes: 8 additions & 8 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<Self> {
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<Self>) -> usize {
extern "C" {
#[link_name = #link_size]
Expand Down Expand Up @@ -1700,13 +1707,6 @@ fn expand_cxx_vector(
unsafe { __unique_ptr_null(&mut repr) }
repr
}
fn __unique_ptr_new() -> *mut ::cxx::CxxVector<Self> {
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<Self>) -> ::cxx::core::mem::MaybeUninit<*mut ::cxx::core::ffi::c_void> {
extern "C" {
#[link_name = #link_unique_ptr_raw]
Expand Down
7 changes: 3 additions & 4 deletions src/cxx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *),
} // namespace

#define STD_VECTOR_OPS(RUST_TYPE, CXX_TYPE) \
std::vector<CXX_TYPE> *cxxbridge1$std$vector$##RUST_TYPE##$new() noexcept { \
return new std::vector<CXX_TYPE>(); \
} \
std::size_t cxxbridge1$std$vector$##RUST_TYPE##$size( \
const std::vector<CXX_TYPE> &s) noexcept { \
return s.size(); \
Expand All @@ -605,10 +608,6 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *),
std::unique_ptr<std::vector<CXX_TYPE>> *ptr) noexcept { \
new (ptr) std::unique_ptr<std::vector<CXX_TYPE>>(); \
} \
std::vector<CXX_TYPE> \
*cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$new() noexcept { \
return new std::vector<CXX_TYPE>(); \
} \
void cxxbridge1$unique_ptr$std$vector$##RUST_TYPE##$raw( \
std::unique_ptr<std::vector<CXX_TYPE>> *ptr, \
std::vector<CXX_TYPE> *raw) noexcept { \
Expand Down
24 changes: 12 additions & 12 deletions src/cxx_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
///
/// The C++ vector is default constructed.
pub fn new() -> UniquePtr<Self> {
unsafe { UniquePtr::from_raw(T::__unique_ptr_new()) }
unsafe { UniquePtr::from_raw(T::__vector_new()) }
}

/// Returns the number of elements in the vector.
Expand Down Expand Up @@ -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<Self>;
#[doc(hidden)]
fn __vector_size(v: &CxxVector<Self>) -> usize;
#[doc(hidden)]
unsafe fn __get_unchecked(v: *mut CxxVector<Self>, pos: usize) -> *mut Self;
Expand All @@ -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<Self>;
#[doc(hidden)]
unsafe fn __unique_ptr_raw(raw: *mut CxxVector<Self>) -> MaybeUninit<*mut c_void>;
#[doc(hidden)]
unsafe fn __unique_ptr_get(repr: MaybeUninit<*mut c_void>) -> *const CxxVector<Self>;
Expand Down Expand Up @@ -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<Self> {
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! {
Expand Down Expand Up @@ -438,15 +447,6 @@ macro_rules! impl_vector_element {
unsafe { __unique_ptr_null(&mut repr) }
repr
}
fn __unique_ptr_new() -> *mut CxxVector<Self> {
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<Self>) -> MaybeUninit<*mut c_void> {
extern "C" {
attr! {
Expand Down

0 comments on commit 2ff5e37

Please sign in to comment.