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

Mut casts #1177

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions demo/src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ inline std::string Goat::describe() const {
oss << "This goat has " << horns << " horn" << plural << ".";
return oss.str();
}

class A {
public:
virtual void foo() const {};
virtual void foo_mut() {};
virtual ~A() {}
};
class B : public A {
public:
void bar() const {}
};
inline std::unique_ptr<B> get_b() { return std::make_unique<B>(); }
7 changes: 7 additions & 0 deletions demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ include_cpp! {
safety!(unsafe_ffi)
generate!("DoMath")
generate!("Goat")
generate!("A")
generate!("B")
generate!("get_b")
}

fn main() {
Expand All @@ -23,4 +26,8 @@ fn main() {
goat.describe().as_ref().unwrap().to_string_lossy(),
"This goat has 2 horns."
);

let mut b = ffi::get_b();
b.as_ref().unwrap().as_ref().foo();
b.as_mut().unwrap().pin_mut().foo_mut();
}
2 changes: 1 addition & 1 deletion engine/src/conversion/analysis/casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn cast_types() -> impl Iterator<Item = CastMutability> {
]
.into_iter()
} else {
vec![CastMutability::ConstToConst].into_iter()
vec![CastMutability::ConstToConst, CastMutability::MutToMut].into_iter()
}
}

Expand Down
11 changes: 9 additions & 2 deletions engine/src/conversion/analysis/fun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ impl<'a> FnAnalyzer<'a> {
autocxx::moveit::new:: #trait_id
},
unsafety: Some(parse_quote! { unsafe }),
lifetime: None,
},
avoid_self: true,
method_name: make_ident(method_name),
Expand Down Expand Up @@ -909,6 +910,7 @@ impl<'a> FnAnalyzer<'a> {
Drop
},
unsafety: None,
lifetime: None,
},
avoid_self: false,
method_name: make_ident("drop"),
Expand Down Expand Up @@ -1473,13 +1475,14 @@ impl<'a> FnAnalyzer<'a> {
let from_type = self_ty.as_ref().unwrap();
let from_type_path = from_type.to_type_path();
let to_type = to_type.to_type_path();
let (trait_signature, ty, method_name) = match *mutable {
let (trait_signature, ty, method_name, trait_lifetime) = match *mutable {
CastMutability::ConstToConst => (
parse_quote! {
AsRef < #to_type >
},
Type::Path(from_type_path),
"as_ref",
None,
),
CastMutability::MutToConst => (
parse_quote! {
Expand All @@ -1489,15 +1492,17 @@ impl<'a> FnAnalyzer<'a> {
&'a mut ::std::pin::Pin < &'a mut #from_type_path >
},
"as_ref",
parse_quote! { 'a }
),
CastMutability::MutToMut => (
parse_quote! {
autocxx::PinMut < #to_type >
},
parse_quote! {
::std::pin::Pin < &'a mut #from_type_path >
std::pin::Pin < &'a mut #from_type_path >
},
"pin_mut",
parse_quote! { 'a }
),
};
let method_name = make_ident(method_name);
Expand All @@ -1510,6 +1515,7 @@ impl<'a> FnAnalyzer<'a> {
ty,
trait_signature,
unsafety: None,
lifetime: trait_lifetime,
},
avoid_self: false,
method_name,
Expand Down Expand Up @@ -1554,6 +1560,7 @@ impl<'a> FnAnalyzer<'a> {
ty: Type::Path(typ),
trait_signature: parse_quote! { autocxx::moveit::MakeCppStorage },
unsafety: Some(parse_quote! { unsafe }),
lifetime: None,
},
avoid_self: false,
method_name: make_ident(method_name),
Expand Down
5 changes: 4 additions & 1 deletion engine/src/conversion/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use syn::{
punctuated::Punctuated,
token::{Comma, Unsafe},
Attribute, FnArg, Ident, ItemConst, ItemEnum, ItemStruct, ItemType, ItemUse, LitBool, LitInt,
Pat, ReturnType, Type, Visibility,
Pat, ReturnType, Type, Visibility, Lifetime,
};

use super::{
Expand Down Expand Up @@ -181,6 +181,7 @@ pub(crate) struct TraitImplSignature {
pub(crate) trait_signature: Type,
/// The trait is 'unsafe' itself
pub(crate) unsafety: Option<Unsafe>,
pub(crate) lifetime: Option<Lifetime>,
}

impl Eq for TraitImplSignature {}
Expand All @@ -190,6 +191,7 @@ impl PartialEq for TraitImplSignature {
totokens_equal(&self.unsafety, &other.unsafety)
&& totokens_equal(&self.ty, &other.ty)
&& totokens_equal(&self.trait_signature, &other.trait_signature)
&& totokens_equal(&self.lifetime, &other.lifetime)
}
}

Expand All @@ -211,6 +213,7 @@ impl std::hash::Hash for TraitImplSignature {
hash_totokens(&self.ty, state);
hash_totokens(&self.trait_signature, state);
hash_totokens(&self.unsafety, state);
hash_totokens(&self.lifetime, state);
}
}

Expand Down
3 changes: 2 additions & 1 deletion engine/src/conversion/codegen_rs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,9 @@ impl<'a> RsCodeGenerator<'a> {
let unsafety = key.unsafety;
let ty = key.ty;
let trt = key.trait_signature;
let lt = key.lifetime.map(|lt| quote! { < #lt > });
output_items.push(Item::Impl(parse_quote! {
#unsafety impl #trt for #ty {
#unsafety impl #lt #trt for #ty {
#(#entries)*
}
}))
Expand Down
17 changes: 14 additions & 3 deletions integration-tests/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9826,6 +9826,7 @@ fn test_call_superclass() {
class A {
public:
virtual void foo() const {};
virtual void foo_mut() {};
virtual ~A() {}
};
class B : public A {
Expand All @@ -9835,8 +9836,9 @@ fn test_call_superclass() {
inline std::unique_ptr<B> get_b() { return std::make_unique<B>(); }
"};
let rs = quote! {
let b = ffi::get_b();
let mut b = ffi::get_b();
b.as_ref().unwrap().as_ref().foo();
b.as_mut().unwrap().pin_mut().foo_mut();
};
run_test("", hdr, rs, &["A", "B", "get_b"], &[]);
}
Expand All @@ -9848,6 +9850,7 @@ fn test_pass_superclass() {
class A {
public:
virtual void foo() const {};
virtual void foo_mut() {};
virtual ~A() {}
};
class B : public A {
Expand All @@ -9856,12 +9859,20 @@ fn test_pass_superclass() {
};
inline std::unique_ptr<B> get_b() { return std::make_unique<B>(); }
inline void take_a(const A&) {}
inline void take_a_mut(A&) {}
"};
let rs = quote! {
let b = ffi::get_b();
let mut b = ffi::get_b();
ffi::take_a(b.as_ref().unwrap().as_ref());
ffi::take_a_mut(b.as_mut().unwrap().pin_mut());
};
run_test("", hdr, rs, &["A", "B", "get_b", "take_a"], &[]);
run_test(
"",
hdr,
rs,
&["A", "B", "get_b", "take_a", "take_a_mut"],
&[],
);
}

#[test]
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,12 @@ pub mod extern_rust {

/// Equivalent to [`std::convert::AsMut`], but returns a pinned mutable reference
/// such that cxx methods can be called on it.
pub trait PinMut<T>: AsRef<T> {
/// Unlike [`std::convert::AsMut`] this does not require that the type also
/// implement [`AsRef`], because this trait primarily applies to `Pin<T>` types,
/// and they don't support `AsRef`.
pub trait PinMut<T> {
/// Return a pinned mutable reference to a type.
fn pin_mut(&mut self) -> std::pin::Pin<&mut T>;
fn pin_mut<'a>(&'a mut self) -> std::pin::Pin<&'a mut T>;
}

/// Provides utility functions to emplace any [`moveit::New`] into a
Expand Down