Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rejig typeconversionpolicy #1129

Closed
wants to merge 27 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Introduce cpp_pin_uniqueptr to allow swapping impls later
adetaylor committed Jun 25, 2022
commit 60417503c4db7d8035e4bf9af548d1d38fc64c7d
13 changes: 8 additions & 5 deletions engine/src/conversion/codegen_rs/mod.rs
Original file line number Diff line number Diff line change
@@ -204,16 +204,19 @@ fn get_cppref_items() -> Vec<Item> {
}),
Item::Impl(parse_quote! {
impl<T: ::cxx::private::UniquePtrTarget> CppUniquePtrPin<T> {
/// Make this object available to C++ by reference.
/// This takes ownership of the object, thus proving you
/// have no existing Rust references. Any reference to this
/// object from Rust after this point is unsafe, since Rust
/// and C++ reference semantics are incompatible.
pub fn new(item: ::cxx::UniquePtr<T>) -> Self {
Self(item)
}
}
}),
Item::Fn(parse_quote! {
/// Pin this item so that we can create C++ references to it.
/// This makes it impossible to hold Rust references because Rust
/// references are fundamentally incompatible with C++ references.
pub fn cpp_pin_uniqueptr<T: ::cxx::private::UniquePtrTarget> (item: ::cxx::UniquePtr<T>) -> CppUniquePtrPin<T> {
CppUniquePtrPin::new(item)
}
})
]
.to_vec()
}
2 changes: 1 addition & 1 deletion examples/reference-wrappers/src/main.rs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ include_cpp! {

fn main() {
let field = ffi::Field::new().within_unique_ptr();
let field = ffi::CppUniquePtrPin::new(field);
let field = ffi::cpp_pin_uniqueptr(field);
let another_goat = field.as_cpp_ref().get_goat();
assert_eq!(
another_goat
2 changes: 1 addition & 1 deletion integration-tests/tests/cpprefs_test.rs
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ fn test_method_call_const() {
"},
quote! {
let goat = ffi::Goat::new().within_unique_ptr();
let goat = ffi::CppUniquePtrPin::new(goat);
let goat = ffi::cpp_pin_uniqueptr(goat);
goat.as_cpp_ref().describe();
},
&["Goat"],