From 10c80e149ef3a1d899a9fb2729a9b27105504d82 Mon Sep 17 00:00:00 2001 From: Andrew Hayzen Date: Tue, 13 Jun 2023 12:07:23 +0100 Subject: [PATCH] cxx-qt-gen: remove field parsing stage as it's not used anymore Closes #559 --- crates/cxx-qt-gen/src/generator/rust/field.rs | 161 ------------------ crates/cxx-qt-gen/src/generator/rust/mod.rs | 1 - .../cxx-qt-gen/src/generator/rust/qobject.rs | 10 +- crates/cxx-qt-gen/src/parser/property.rs | 10 -- crates/cxx-qt-gen/src/parser/qobject.rs | 28 +-- 5 files changed, 7 insertions(+), 203 deletions(-) delete mode 100644 crates/cxx-qt-gen/src/generator/rust/field.rs diff --git a/crates/cxx-qt-gen/src/generator/rust/field.rs b/crates/cxx-qt-gen/src/generator/rust/field.rs deleted file mode 100644 index 38a134504..000000000 --- a/crates/cxx-qt-gen/src/generator/rust/field.rs +++ /dev/null @@ -1,161 +0,0 @@ -// SPDX-FileCopyrightText: 2022 Klarälvdalens Datakonsult AB, a KDAB Group company -// SPDX-FileContributor: Andrew Hayzen -// -// SPDX-License-Identifier: MIT OR Apache-2.0 - -use crate::{ - generator::{ - naming::{property::QPropertyName, qobject::QObjectName}, - rust::{fragment::RustFragmentPair, qobject::GeneratedRustQObjectBlocks}, - }, - parser::property::ParsedRustField, -}; -use quote::quote; -use syn::Result; - -pub fn generate_rust_fields( - fields: &Vec, - qobject_idents: &QObjectName, -) -> Result { - let mut generated = GeneratedRustQObjectBlocks::default(); - let cpp_class_name_rust = &qobject_idents.cpp_class.rust; - - for field in fields { - let idents = QPropertyName::from(&field.ident); - let getter_rust = &idents.getter.rust; - let getter_mutable_rust = &idents.getter_mutable.rust; - let setter_rust = &idents.setter.rust; - let ident = &idents.name.rust; - let ty = &field.ty; - let vis = &field.vis; - - let fragment = RustFragmentPair { - cxx_bridge: vec![], - implementation: vec![ - quote! { - impl #cpp_class_name_rust { - #vis fn #getter_rust(&self) -> &#ty { - &self.rust().#ident - } - } - }, - quote! { - impl #cpp_class_name_rust { - #vis fn #getter_mutable_rust<'a>(self: Pin<&'a mut Self>) -> &'a mut #ty { - unsafe { &mut self.rust_mut().get_unchecked_mut().#ident } - } - } - }, - quote! { - impl #cpp_class_name_rust { - #vis fn #setter_rust(self: Pin<&mut Self>, value: #ty) { - self.rust_mut().#ident = value; - } - } - }, - ], - }; - - generated - .cxx_qt_mod_contents - .append(&mut fragment.implementation_as_items()?); - } - - Ok(generated) -} - -#[cfg(test)] -mod tests { - use super::*; - - use crate::{generator::naming::qobject::tests::create_qobjectname, tests::assert_tokens_eq}; - use quote::{format_ident, quote}; - use syn::parse_quote; - - #[test] - fn test_generate_rust_properties() { - let properties = vec![ - ParsedRustField { - ident: format_ident!("private_field"), - ty: parse_quote! { i32 }, - vis: syn::Visibility::Inherited, - }, - ParsedRustField { - ident: format_ident!("public_field"), - ty: parse_quote! { f64 }, - vis: parse_quote! { pub }, - }, - ]; - let qobject_idents = create_qobjectname(); - - let generated = generate_rust_fields(&properties, &qobject_idents).unwrap(); - - // Check that we have the expected number of blocks - assert_eq!(generated.cxx_mod_contents.len(), 0); - assert_eq!(generated.cxx_qt_mod_contents.len(), 6); - - // Private Field - assert_tokens_eq( - &generated.cxx_qt_mod_contents[0], - quote! { - impl MyObjectQt { - fn private_field(&self) -> &i32 { - &self.rust().private_field - } - } - }, - ); - assert_tokens_eq( - &generated.cxx_qt_mod_contents[1], - quote! { - impl MyObjectQt { - fn private_field_mut<'a>(self: Pin<&'a mut Self>) -> &'a mut i32 { - unsafe { &mut self.rust_mut().get_unchecked_mut().private_field } - } - } - }, - ); - assert_tokens_eq( - &generated.cxx_qt_mod_contents[2], - quote! { - impl MyObjectQt { - fn set_private_field(self: Pin<&mut Self>, value: i32) { - self.rust_mut().private_field = value; - } - } - }, - ); - - // Public Field - assert_tokens_eq( - &generated.cxx_qt_mod_contents[3], - quote! { - impl MyObjectQt { - pub fn public_field(&self) -> &f64 { - &self.rust().public_field - } - } - }, - ); - assert_tokens_eq( - &generated.cxx_qt_mod_contents[4], - quote! { - impl MyObjectQt { - pub fn public_field_mut<'a>(self: Pin<&'a mut Self>) -> &'a mut f64 { - unsafe { &mut self.rust_mut().get_unchecked_mut().public_field } - } - } - }, - ); - assert_tokens_eq( - &generated.cxx_qt_mod_contents[5], - quote! { - impl MyObjectQt { - pub fn set_public_field(self: Pin<&mut Self>, value: f64) { - self.rust_mut().public_field = value; - } - } - }, - ); - } -} diff --git a/crates/cxx-qt-gen/src/generator/rust/mod.rs b/crates/cxx-qt-gen/src/generator/rust/mod.rs index 746fa12da..b17fffbdc 100644 --- a/crates/cxx-qt-gen/src/generator/rust/mod.rs +++ b/crates/cxx-qt-gen/src/generator/rust/mod.rs @@ -3,7 +3,6 @@ // // SPDX-License-Identifier: MIT OR Apache-2.0 -pub mod field; pub mod fragment; pub mod inherit; pub mod invokable; diff --git a/crates/cxx-qt-gen/src/generator/rust/qobject.rs b/crates/cxx-qt-gen/src/generator/rust/qobject.rs index b59e10d73..6be4db08a 100644 --- a/crates/cxx-qt-gen/src/generator/rust/qobject.rs +++ b/crates/cxx-qt-gen/src/generator/rust/qobject.rs @@ -7,9 +7,8 @@ use crate::{ generator::{ naming::{namespace::NamespaceName, qobject::QObjectName}, rust::{ - field::generate_rust_fields, fragment::RustFragmentPair, inherit, - invokable::generate_rust_invokables, property::generate_rust_properties, - signals::generate_rust_signals, threading, + fragment::RustFragmentPair, inherit, invokable::generate_rust_invokables, + property::generate_rust_properties, signals::generate_rust_signals, threading, }, }, parser::qobject::ParsedQObject, @@ -69,14 +68,11 @@ impl GeneratedRustQObject { .cxx_qt_mod_contents .push(syn::Item::Struct(qobject.qobject_struct.clone())); - // Generate methods for the properties, fields, invokables, signals + // Generate methods for the properties, invokables, signals generated.blocks.append(&mut generate_rust_properties( &qobject.properties, &qobject_idents, )?); - generated - .blocks - .append(&mut generate_rust_fields(&qobject.fields, &qobject_idents)?); generated.blocks.append(&mut generate_rust_invokables( &qobject.invokables, &qobject_idents, diff --git a/crates/cxx-qt-gen/src/parser/property.rs b/crates/cxx-qt-gen/src/parser/property.rs index 87bdd9f7d..c66d1b97a 100644 --- a/crates/cxx-qt-gen/src/parser/property.rs +++ b/crates/cxx-qt-gen/src/parser/property.rs @@ -5,16 +5,6 @@ use syn::{Ident, Type, Visibility}; -/// Describes a single field for a struct -pub struct ParsedRustField { - /// The [syn::Ident] of the field - pub ident: Ident, - /// The [syn::Type] of the field - pub ty: Type, - /// The [syn::Visibility] of the field - pub vis: Visibility, -} - /// Describes a single Q_PROPERTY for a struct pub struct ParsedQProperty { /// The [syn::Ident] of the property diff --git a/crates/cxx-qt-gen/src/parser/qobject.rs b/crates/cxx-qt-gen/src/parser/qobject.rs index 10604dd91..0d8fee6ef 100644 --- a/crates/cxx-qt-gen/src/parser/qobject.rs +++ b/crates/cxx-qt-gen/src/parser/qobject.rs @@ -9,9 +9,7 @@ use crate::syntax::{ }; use crate::{ parser::{ - inherit::ParsedInheritedMethod, - invokable::ParsedQInvokable, - property::{ParsedQProperty, ParsedRustField}, + inherit::ParsedInheritedMethod, invokable::ParsedQInvokable, property::ParsedQProperty, signals::ParsedSignal, }, syntax::path::path_compare_str, @@ -60,8 +58,6 @@ pub struct ParsedQObject { /// /// These will be exposed as Q_PROPERTY on the C++ object pub properties: Vec, - /// List of Rust fields on the struct that need getters and setters generated - pub fields: Vec, /// List of specifiers to register with in QML pub qml_metadata: Option, /// Whether locking is enabled for this QObject @@ -99,7 +95,7 @@ impl ParsedQObject { // Parse any properties in the struct // and remove the #[qproperty] attribute - let (properties, _) = Self::parse_struct_fields(&mut qobject_struct.fields)?; + let properties = Self::parse_struct_fields(&mut qobject_struct.fields)?; // Ensure that the QObject is marked as pub otherwise the error is non obvious // https://github.com/KDAB/cxx-qt/issues/457 @@ -119,11 +115,6 @@ impl ParsedQObject { inherited_methods: vec![], passthrough_impl_items: vec![], properties, - // Do not generate helpers for fields as they are going to move out of the bridge - // - // TODO: we may bring #[field(T, NAME)] as a way of doing this - // if we want to keep the unsafety vs safety of property changes with or without notify - fields: vec![], qml_metadata, locking: true, threading: false, @@ -265,11 +256,8 @@ impl ParsedQObject { } /// Extract all the properties from [syn::Fields] from a [syn::ItemStruct] - fn parse_struct_fields( - fields: &mut Fields, - ) -> Result<(Vec, Vec)> { + fn parse_struct_fields(fields: &mut Fields) -> Result> { let mut properties = vec![]; - let mut rust_fields = vec![]; for field in fields_to_named_fields_mut(fields)? { // Try to find any properties defined within the struct if let Some(index) = attribute_find_path(&field.attrs, &["qproperty"]) { @@ -281,16 +269,10 @@ impl ParsedQObject { ty: field.ty.clone(), vis: field.vis.clone(), }); - } else { - rust_fields.push(ParsedRustField { - ident: field.ident.clone().unwrap(), - ty: field.ty.clone(), - vis: field.vis.clone(), - }) } } - Ok((properties, rust_fields)) + Ok(properties) } } @@ -349,7 +331,6 @@ pub mod tests { let qobject = ParsedQObject::from_struct(&qobject_struct, 0).unwrap(); assert_eq!(qobject.properties.len(), 2); - assert_eq!(qobject.qobject_struct.fields.len(), 3); } #[test] @@ -363,7 +344,6 @@ pub mod tests { let qobject = ParsedQObject::from_struct(&qobject_struct, 0).unwrap(); assert_eq!(qobject.properties.len(), 0); - assert_eq!(qobject.qobject_struct.fields.len(), 1); } #[test]