Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions engine/src/conversion/codegen_rs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ enum Use {
Custom(Box<Item>),
}

fn get_string_items(config: &IncludeCppConfig) -> Vec<Item> {
let makestring_name = make_ident(config.get_makestring_name());
fn get_string_items() -> Vec<Item> {
[
Item::Trait(parse_quote! {
pub trait ToCppString {
Expand All @@ -84,21 +83,21 @@ fn get_string_items(config: &IncludeCppConfig) -> Vec<Item> {
Item::Impl(parse_quote! {
impl ToCppString for &str {
fn into_cpp(self) -> cxx::UniquePtr<cxx::CxxString> {
cxxbridge::#makestring_name(self)
make_string(self)
}
}
}),
Item::Impl(parse_quote! {
impl ToCppString for String {
fn into_cpp(self) -> cxx::UniquePtr<cxx::CxxString> {
cxxbridge::#makestring_name(&self)
make_string(&self)
}
}
}),
Item::Impl(parse_quote! {
impl ToCppString for &String {
fn into_cpp(self) -> cxx::UniquePtr<cxx::CxxString> {
cxxbridge::#makestring_name(self)
make_string(self)
}
}
}),
Expand Down Expand Up @@ -402,10 +401,10 @@ impl<'a> RsCodeGenerator<'a> {
fn #make_string_name(str_: &str) -> UniquePtr<CxxString>;
))),
bridge_items: Vec::new(),
global_items: get_string_items(self.config),
global_items: get_string_items(),
bindgen_mod_item: None,
impl_entry: None,
materialization: Use::Unused,
materialization: Use::UsedFromCxxBridgeWithAlias(make_ident("make_string")),
}
}
Api::ConcreteType { .. } => RsCodegenResult {
Expand Down
13 changes: 13 additions & 0 deletions engine/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,19 @@ fn test_make_string() {
run_test("", hdr, rs, &["Bob"], &[]);
}

#[test]
fn test_string_make_unique() {
let hdr = indoc! {"
#include <string>
inline void take_string(const std::string*) {};
"};
let rs = quote! {
let s = ffi::make_string("");
unsafe { ffi::take_string(s.as_ref().unwrap()) };
};
run_test("", hdr, rs, &["take_string"], &[]);
}

#[test]
fn test_string_constant() {
let hdr = indoc! {"
Expand Down
14 changes: 6 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ use autocxx_engine::IncludeCppEngine;
///
/// There's not a significant ergonomic problem from the use of [`cxx::UniquePtr`].
/// The main negative of the automatic boxing into [`cxx::UniquePtr`] is performance:
/// specifiaclly, the need to
/// specifically, the need to
/// allocate heap cells on the C++ side and move data into and out of them.
/// You don't want to be doing this inside a tight loop (but if you're calling
/// across the C++/Rust boundary in a tight loop, perhaps reconsider that boundary
Expand Down Expand Up @@ -328,14 +328,12 @@ use autocxx_engine::IncludeCppEngine;
///
/// If you need to create a blank `UniquePtr<CxxString>` in Rust, such that
/// (for example) you can pass its mutable reference or pointer into some
/// pre-existing C++ API, there's currently no built in support for that.
/// You should add an extra C++ API:
/// pre-existing C++ API, call `ffi::make_string("")` which will return
/// a blank `UniquePtr<CxxString>`.
///
/// ```cpp
/// std::string make_blank_string() { return std::string(); }
/// ```
///
/// and then use [`generate`] to make bindings for that.
/// Don't attempt to use [cxx::let_cpp_string] which will allocate the
/// string on the stack, and is generally incompatible with the
/// [cxx::UniquePtr]-based approaches we use here.
///
/// ## Preprocessor symbols
///
Expand Down