Skip to content

Commit

Permalink
cxx-qt-gen: move QObject struct outside the bridge and use a type alias
Browse files Browse the repository at this point in the history
Related to KDAB#559
  • Loading branch information
ahayzen-kdab committed Jul 18, 2023
1 parent f05b4a3 commit 6948bd8
Show file tree
Hide file tree
Showing 50 changed files with 1,340 additions and 1,151 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `#[qinvokable]` is now defined as a signature in `extern "RustQt"`
- `rust_mut` is now safe to call
- `#[qproperty]` is now defined as an attribute on the qobject rather than the field
- QObject struct is now split between the bridge and implementation outside via a type alias

### Fixed

Expand Down
18 changes: 12 additions & 6 deletions crates/cxx-qt-gen/src/generator/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -71,8 +73,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -88,8 +92,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand Down
30 changes: 20 additions & 10 deletions crates/cxx-qt-gen/src/generator/cpp/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -188,8 +190,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(base = "QStringListModel")]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject(base = "QStringListModel")]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -209,8 +213,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_name = "MyQmlElement")]
pub struct MyNamedObject;
extern "RustQt" {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_name = "MyQmlElement")]
type MyNamedObject = super::MyNamedObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -233,8 +239,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -258,8 +266,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_uncreatable)]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_uncreatable)]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/src/generator/naming/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct NamespaceName {

impl From<&ParsedQObject> for NamespaceName {
fn from(qobject: &ParsedQObject) -> Self {
NamespaceName::from_pair_str(&qobject.namespace, &qobject.qobject_struct.ident)
NamespaceName::from_pair_str(&qobject.namespace, &qobject.qobject_ty.ident_left)
}
}

Expand Down
32 changes: 11 additions & 21 deletions crates/cxx-qt-gen/src/generator/naming/qobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@ pub struct QObjectName {

impl From<&ParsedQObject> for QObjectName {
fn from(qobject: &ParsedQObject) -> Self {
Self::from(&qobject.qobject_struct.ident)
}
}

impl From<&Ident> for QObjectName {
fn from(ident: &Ident) -> Self {
let ident_left = &qobject.qobject_ty.ident_left;
Self {
ident: ident.clone(),
cpp_class: CombinedIdent::cpp_class_from_rust_struct(ident.clone()),
rust_struct: CombinedIdent::from_rust_struct(ident.clone()),
cxx_qt_thread_class: cxx_qt_thread_class_from_ident(ident),
cxx_qt_thread_queued_fn_struct: cxx_qt_thread_queued_fn_struct_from_ident(ident),
ident: ident_left.clone(),
// TODO: later we may support cxx_name or rust_name so keep these are CombinedIdents for now
cpp_class: CombinedIdent::from_ident(ident_left.clone()),
rust_struct: CombinedIdent::from_ident(qobject.qobject_ty.ident_right.clone()),
cxx_qt_thread_class: cxx_qt_thread_class_from_ident(ident_left),
cxx_qt_thread_queued_fn_struct: cxx_qt_thread_queued_fn_struct_from_ident(ident_left),
}
}
}
Expand All @@ -61,16 +57,10 @@ fn cxx_qt_thread_queued_fn_struct_from_ident(ident: &Ident) -> Ident {
}

impl CombinedIdent {
/// For a given ident generate the C++ class name
fn cpp_class_from_rust_struct(ident: Ident) -> Self {
let rust = format_ident!("{ident}Qt");
Self { cpp: ident, rust }
}

/// For a given ident generate the Rust and C++ names
fn from_rust_struct(ident: Ident) -> Self {
fn from_ident(ident: Ident) -> Self {
Self {
cpp: format_ident!("{ident}Rust"),
cpp: ident.clone(),
rust: ident,
}
}
Expand All @@ -90,9 +80,9 @@ pub mod tests {
fn test_parsed_property() {
let names = QObjectName::from(&create_parsed_qobject());
assert_eq!(names.cpp_class.cpp, format_ident!("MyObject"));
assert_eq!(names.cpp_class.rust, format_ident!("MyObjectQt"));
assert_eq!(names.cpp_class.rust, format_ident!("MyObject"));
assert_eq!(names.rust_struct.cpp, format_ident!("MyObjectRust"));
assert_eq!(names.rust_struct.rust, format_ident!("MyObject"));
assert_eq!(names.rust_struct.rust, format_ident!("MyObjectRust"));
assert_eq!(
names.cxx_qt_thread_class,
format_ident!("MyObjectCxxQtThread")
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/src/generator/rust/inherit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ mod tests {
quote! {
unsafe extern "C++" {
#[cxx_name = "testCxxQtInherit"]
fn test(self: Pin<&mut MyObjectQt>, a: B, b: C);
fn test(self: Pin<&mut MyObject>, a: B, b: C);
}
},
);
Expand All @@ -118,7 +118,7 @@ mod tests {
quote! {
unsafe extern "C++" {
#[cxx_name = "testCxxQtInherit"]
fn test(self: &MyObjectQt, a: B, b: C);
fn test(self: &MyObject, a: B, b: C);
}
},
);
Expand All @@ -143,7 +143,7 @@ mod tests {
quote! {
extern "C++" {
#[cxx_name = "testCxxQtInherit"]
unsafe fn test(self: &MyObjectQt,);
unsafe fn test(self: &MyObject,);
}
},
);
Expand Down
24 changes: 12 additions & 12 deletions crates/cxx-qt-gen/src/generator/rust/invokable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,16 @@ mod tests {
quote! {
extern "Rust" {
#[cxx_name = "voidInvokableWrapper"]
fn void_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt);
fn void_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject);
}
},
);
assert_tokens_eq(
&generated.cxx_qt_mod_contents[0],
quote! {
impl MyObject {
impl MyObjectRust {
#[doc(hidden)]
pub fn void_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt) {
pub fn void_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject) {
cpp.void_invokable();
}
}
Expand All @@ -194,16 +194,16 @@ mod tests {
quote! {
extern "Rust" {
#[cxx_name = "trivialInvokableWrapper"]
fn trivial_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt, param: i32) -> i32;
fn trivial_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject, param: i32) -> i32;
}
},
);
assert_tokens_eq(
&generated.cxx_qt_mod_contents[1],
quote! {
impl MyObject {
impl MyObjectRust {
#[doc(hidden)]
pub fn trivial_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt, param: i32) -> i32 {
pub fn trivial_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject, param: i32) -> i32 {
return cpp.trivial_invokable(param);
}
}
Expand All @@ -216,16 +216,16 @@ mod tests {
quote! {
extern "Rust" {
#[cxx_name = "opaqueInvokableWrapper"]
fn opaque_invokable_wrapper(self: &mut MyObject, cpp: Pin<&mut MyObjectQt>, param: &QColor) -> UniquePtr<QColor>;
fn opaque_invokable_wrapper(self: &mut MyObjectRust, cpp: Pin<&mut MyObject>, param: &QColor) -> UniquePtr<QColor>;
}
},
);
assert_tokens_eq(
&generated.cxx_qt_mod_contents[2],
quote! {
impl MyObject {
impl MyObjectRust {
#[doc(hidden)]
pub fn opaque_invokable_wrapper(self: &mut MyObject, cpp: Pin<&mut MyObjectQt>, param: &QColor) -> UniquePtr<QColor> {
pub fn opaque_invokable_wrapper(self: &mut MyObjectRust, cpp: Pin<&mut MyObject>, param: &QColor) -> UniquePtr<QColor> {
return cpp.opaque_invokable(param);
}
}
Expand All @@ -238,16 +238,16 @@ mod tests {
quote! {
extern "Rust" {
#[cxx_name = "unsafeInvokableWrapper"]
unsafe fn unsafe_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt, param: *mut T) -> *mut T;
unsafe fn unsafe_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject, param: *mut T) -> *mut T;
}
},
);
assert_tokens_eq(
&generated.cxx_qt_mod_contents[3],
quote! {
impl MyObject {
impl MyObjectRust {
#[doc(hidden)]
pub unsafe fn unsafe_invokable_wrapper(self: &MyObject, cpp: &MyObjectQt, param: *mut T) -> *mut T {
pub unsafe fn unsafe_invokable_wrapper(self: &MyObjectRust, cpp: &MyObject, param: *mut T) -> *mut T {
return cpp.unsafe_invokable(param);
}
}
Expand Down
24 changes: 16 additions & 8 deletions crates/cxx-qt-gen/src/generator/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -100,8 +102,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(namespace = "cxx_qt")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -121,8 +125,10 @@ mod tests {
mod ffi {
use std::collections::HashMap;

#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand All @@ -140,8 +146,10 @@ mod tests {
let module: ItemMod = parse_quote! {
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
mod ffi {
#[cxx_qt::qobject]
pub struct MyObject;
extern "RustQt" {
#[cxx_qt::qobject]
type MyObject = super::MyObjectRust;
}
}
};
let parser = Parser::from(module).unwrap();
Expand Down
Loading

0 comments on commit 6948bd8

Please sign in to comment.