Skip to content

Commit

Permalink
WIP: cxx-qt-gen: add support for nested object properties
Browse files Browse the repository at this point in the history
Closes KDAB#299
  • Loading branch information
ahayzen-kdab committed Nov 28, 2022
1 parent 3b61444 commit 6671605
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
10 changes: 8 additions & 2 deletions crates/cxx-qt-gen/src/generator/rust/property/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ pub fn generate(
let getter_mutable_rust = &idents.getter_mutable.rust;
let ident = &idents.name.rust;

// TODO: need to read the types of the method input and output to see if any of them are considered unsafe
// eg if there is a TypePtr in them
let has_unsafe = {
quote! { unsafe }
};

RustFragmentPair {
cxx_bridge: vec![quote! {
extern "Rust" {
Expand All @@ -32,14 +38,14 @@ pub fn generate(
implementation: vec![
quote! {
impl #rust_struct_name_rust {
pub fn #getter_rust<'a>(&'a self, cpp: &'a #cpp_class_name_rust) -> &'a #ty {
pub #has_unsafe fn #getter_rust<'a>(&'a self, cpp: &'a #cpp_class_name_rust) -> &'a #ty {
cpp.#getter_rust()
}
}
},
quote! {
impl #cpp_class_name_rust {
pub fn #getter_rust(&self) -> &#ty {
pub #has_unsafe fn #getter_rust(&self) -> &#ty {
&self.rust().#ident
}
}
Expand Down
12 changes: 9 additions & 3 deletions crates/cxx-qt-gen/src/generator/rust/property/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,30 @@ pub fn generate(
let ident = &idents.name.rust;
let notify_ident = &idents.notify.rust;

// TODO: need to read the types of the method input and output to see if any of them are considered unsafe
// eg if there is a TypePtr in them
let has_unsafe = {
quote! { unsafe }
};

RustFragmentPair {
cxx_bridge: vec![quote! {
extern "Rust" {
#[cxx_name = #setter_cpp]
fn #setter_rust(self: &mut #rust_struct_name_rust, cpp: Pin<&mut #cpp_class_name_rust>, value: #ty);
#has_unsafe fn #setter_rust(self: &mut #rust_struct_name_rust, cpp: Pin<&mut #cpp_class_name_rust>, value: #ty);
}
}],
implementation: vec![
quote! {
impl #rust_struct_name_rust {
pub fn #setter_rust(&mut self, cpp: Pin<&mut #cpp_class_name_rust>, value: #ty) {
pub #has_unsafe fn #setter_rust(&mut self, cpp: Pin<&mut #cpp_class_name_rust>, value: #ty) {
cpp.#setter_rust(value);
}
}
},
quote! {
impl #cpp_class_name_rust {
pub fn #setter_rust(mut self: Pin<&mut Self>, value: #ty) {
pub #has_unsafe fn #setter_rust(mut self: Pin<&mut Self>, value: #ty) {
unsafe {
self.as_mut().rust_mut().#ident = value;
}
Expand Down
18 changes: 9 additions & 9 deletions examples/qml_features/rust/src/nested_qobjects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ mod ffi {
#[cxx_qt::qobject]
#[derive(Default)]
pub struct OuterObject {
// #[qproperty]
// inner: *mut InnerObjectPtr,
#[qproperty]
inner: *mut InnerObjectPtr,
}

// impl Default for OuterObject {
// fn default() -> Self {
// Self {
// // inner: // what goes here?
// }
// }
// }
impl Default for OuterObject {
fn default() -> Self {
Self {
inner: std::ptr::null_mut(),
}
}
}

impl qobject::OuterObject {
// InnerObjectPtr needs to be called InnerObject in our C++ generation...
Expand Down

0 comments on commit 6671605

Please sign in to comment.