Skip to content

Commit 4e96dfd

Browse files
authored
Merge pull request #567 from google/expose-string-make-unique
Expose string creation method formally
2 parents f9876a0 + 0f813e8 commit 4e96dfd

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

engine/src/conversion/codegen_rs/mod.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ enum Use {
7070
Custom(Box<Item>),
7171
}
7272

73-
fn get_string_items(config: &IncludeCppConfig) -> Vec<Item> {
74-
let makestring_name = make_ident(config.get_makestring_name());
73+
fn get_string_items() -> Vec<Item> {
7574
[
7675
Item::Trait(parse_quote! {
7776
pub trait ToCppString {
@@ -84,21 +83,21 @@ fn get_string_items(config: &IncludeCppConfig) -> Vec<Item> {
8483
Item::Impl(parse_quote! {
8584
impl ToCppString for &str {
8685
fn into_cpp(self) -> cxx::UniquePtr<cxx::CxxString> {
87-
cxxbridge::#makestring_name(self)
86+
make_string(self)
8887
}
8988
}
9089
}),
9190
Item::Impl(parse_quote! {
9291
impl ToCppString for String {
9392
fn into_cpp(self) -> cxx::UniquePtr<cxx::CxxString> {
94-
cxxbridge::#makestring_name(&self)
93+
make_string(&self)
9594
}
9695
}
9796
}),
9897
Item::Impl(parse_quote! {
9998
impl ToCppString for &String {
10099
fn into_cpp(self) -> cxx::UniquePtr<cxx::CxxString> {
101-
cxxbridge::#makestring_name(self)
100+
make_string(self)
102101
}
103102
}
104103
}),
@@ -402,10 +401,10 @@ impl<'a> RsCodeGenerator<'a> {
402401
fn #make_string_name(str_: &str) -> UniquePtr<CxxString>;
403402
))),
404403
bridge_items: Vec::new(),
405-
global_items: get_string_items(self.config),
404+
global_items: get_string_items(),
406405
bindgen_mod_item: None,
407406
impl_entry: None,
408-
materialization: Use::Unused,
407+
materialization: Use::UsedFromCxxBridgeWithAlias(make_ident("make_string")),
409408
}
410409
}
411410
Api::ConcreteType { .. } => RsCodegenResult {

engine/src/integration_tests.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,6 +2913,19 @@ fn test_make_string() {
29132913
run_test("", hdr, rs, &["Bob"], &[]);
29142914
}
29152915

2916+
#[test]
2917+
fn test_string_make_unique() {
2918+
let hdr = indoc! {"
2919+
#include <string>
2920+
inline void take_string(const std::string*) {};
2921+
"};
2922+
let rs = quote! {
2923+
let s = ffi::make_string("");
2924+
unsafe { ffi::take_string(s.as_ref().unwrap()) };
2925+
};
2926+
run_test("", hdr, rs, &["take_string"], &[]);
2927+
}
2928+
29162929
#[test]
29172930
fn test_string_constant() {
29182931
let hdr = indoc! {"

src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ use autocxx_engine::IncludeCppEngine;
225225
///
226226
/// There's not a significant ergonomic problem from the use of [`cxx::UniquePtr`].
227227
/// The main negative of the automatic boxing into [`cxx::UniquePtr`] is performance:
228-
/// specifiaclly, the need to
228+
/// specifically, the need to
229229
/// allocate heap cells on the C++ side and move data into and out of them.
230230
/// You don't want to be doing this inside a tight loop (but if you're calling
231231
/// across the C++/Rust boundary in a tight loop, perhaps reconsider that boundary
@@ -330,14 +330,12 @@ use autocxx_engine::IncludeCppEngine;
330330
///
331331
/// If you need to create a blank `UniquePtr<CxxString>` in Rust, such that
332332
/// (for example) you can pass its mutable reference or pointer into some
333-
/// pre-existing C++ API, there's currently no built in support for that.
334-
/// You should add an extra C++ API:
333+
/// pre-existing C++ API, call `ffi::make_string("")` which will return
334+
/// a blank `UniquePtr<CxxString>`.
335335
///
336-
/// ```cpp
337-
/// std::string make_blank_string() { return std::string(); }
338-
/// ```
339-
///
340-
/// and then use [`generate`] to make bindings for that.
336+
/// Don't attempt to use [cxx::let_cpp_string] which will allocate the
337+
/// string on the stack, and is generally incompatible with the
338+
/// [cxx::UniquePtr]-based approaches we use here.
341339
///
342340
/// ## Preprocessor symbols
343341
///

0 commit comments

Comments
 (0)