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
Get the Pin thing working
adetaylor committed Jun 24, 2022
commit cc3d4f1cc62958701b7571f0b6ba3bccf13da10c
15 changes: 8 additions & 7 deletions engine/src/conversion/codegen_rs/mod.rs
Original file line number Diff line number Diff line change
@@ -190,20 +190,21 @@ fn get_cppref_items() -> Vec<Item> {
}
}),
Item::Struct(parse_quote! {
/// "Pins" an object so that C++-compatible references can be created.
/// "Pins" a `UniquePtr` to an object, so that C++-compatible references can be created.
#[repr(transparent)]
pub struct CppPin<T>(T);
pub struct CppUniquePtrPin<T: ::cxx::private::UniquePtrTarget>(::cxx::UniquePtr<T>);
}),
Item::Impl(parse_quote! {
impl<'a, T: 'a> autocxx::CppPin<'a, T> for CppPin<T>
impl<'a, T: 'a + ::cxx::private::UniquePtrTarget> autocxx::CppPin<'a, T> for CppUniquePtrPin<T>
{
type CppRef = CppRef<'a, T>;
type CppMutRef = CppMutRef<'a, T>;
fn as_ptr(&self) -> *const T {
::std::ptr::addr_of!(self.0)
// TODO add as_ptr to cxx to avoid the ephemeral reference
self.0.as_ref().unwrap() as *const T
}
fn as_mut_ptr(&mut self) -> *mut T {
::std::ptr::addr_of_mut!(self.0)
unsafe { ::std::pin::Pin::into_inner_unchecked(self.0.as_mut().unwrap()) as *mut T }
}
fn as_cpp_ref(&self) -> Self::CppRef {
CppRef(self.as_ptr(), ::std::marker::PhantomData)
@@ -214,13 +215,13 @@ fn get_cppref_items() -> Vec<Item> {
}
}),
Item::Impl(parse_quote! {
impl<T> CppPin<T> {
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: T) -> CppPin<T> {
pub fn new(item: ::cxx::UniquePtr<T>) -> Self {
Self(item)
}
}
27 changes: 1 addition & 26 deletions examples/reference-wrappers/src/main.rs
Original file line number Diff line number Diff line change
@@ -16,33 +16,8 @@ include_cpp! {
}

fn main() {
let mut goat = ffi::Goat::new().within_box();
let mut goat = ffi::CppMutRef::from_box(&mut goat);
goat.add_a_horn();
goat.add_a_horn();
assert_eq!(
goat.as_cpp_ref()
.describe()
.as_ref()
.unwrap()
.to_string_lossy(),
"This goat has 2 horns."
);
let mut field = ffi::Field::new().within_box();
let field = ffi::CppMutRef::from_box(&mut field);
let another_goat = field.as_cpp_ref().get_goat();
assert_eq!(
another_goat
.describe()
.as_ref()
.unwrap()
.to_string_lossy(),
"This goat has 0 horns."
);


let field = ffi::Field::new().within_unique_ptr();
let field = ffi::CppPin::new(field);
let field = ffi::CppUniquePtrPin::new(field);
let another_goat = field.as_cpp_ref().get_goat();
assert_eq!(
another_goat
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -616,16 +616,16 @@ pub mod prelude {
pub use crate::c_void;
pub use crate::cpp_semantics;
pub use crate::include_cpp;
pub use crate::CppMutRef;
pub use crate::CppPin;
pub use crate::CppRef;
pub use crate::PinMut;
pub use crate::RValueParam;
pub use crate::ValueParam;
pub use crate::WithinBox;
pub use crate::WithinBoxTrivial;
pub use crate::WithinUniquePtr;
pub use crate::WithinUniquePtrTrivial;
pub use crate::CppMutRef;
pub use crate::CppRef;
pub use crate::CppPin;
pub use cxx::UniquePtr;
pub use moveit::moveit;
pub use moveit::new::New;