66use super :: qnamespace:: ParsedQNamespace ;
77use super :: trait_impl:: TraitImpl ;
88use crate :: naming:: cpp:: err_unsupported_item;
9- use crate :: parser:: method:: MethodFields ;
109use crate :: parser:: CaseConversion ;
10+ use crate :: preprocessor:: self_inlining:: try_inline_self_invokables;
1111use crate :: {
1212 parser:: {
1313 externcxxqt:: ParsedExternCxxQt , inherit:: ParsedInheritedMethod , method:: ParsedMethod ,
@@ -19,7 +19,6 @@ use crate::{
1919 } ,
2020} ;
2121use quote:: format_ident;
22- use std:: ops:: DerefMut ;
2322use syn:: {
2423 spanned:: Spanned , Error , ForeignItem , Ident , Item , ItemEnum , ItemForeignMod , ItemImpl ,
2524 ItemMacro , Meta , Result ,
@@ -68,38 +67,6 @@ impl ParsedCxxQtData {
6867 }
6968 }
7069
71- /// Inline any `Self` types in the methods signatures with the Ident of a qobject passed in
72- ///
73- /// If there are unresolved methods in the list, but inline is false, it will error,
74- /// as the self inlining is only available if there is exactly one `QObject` in the block,
75- /// and this indicates that no inlining can be done, but some `Self` types were present.
76- pub fn try_inline_self_types (
77- inline : bool ,
78- type_to_inline : & Option < Ident > ,
79- invokables : & mut [ impl DerefMut < Target = MethodFields > ] ,
80- ) -> Result < ( ) > {
81- for method in invokables. iter_mut ( ) {
82- if method. self_unresolved {
83- if inline {
84- if let Some ( inline_type) = type_to_inline. clone ( ) {
85- method. qobject_ident = inline_type;
86- } else {
87- return Err ( Error :: new (
88- method. method . span ( ) ,
89- "Expected a type to inline, no `qobject` typename was passed!" ,
90- ) ) ;
91- }
92- } else {
93- return Err ( Error :: new (
94- method. method . span ( ) ,
95- "`Self` type can only be inferred if the extern block contains only one `qobject`." ,
96- ) ) ;
97- }
98- }
99- }
100- Ok ( ( ) )
101- }
102-
10370 /// Determine if the given [syn::Item] is a CXX-Qt related item
10471 /// If it is then add the [syn::Item] into qobjects BTreeMap
10572 /// Otherwise return the [syn::Item] to pass through to CXX
@@ -253,9 +220,9 @@ impl ParsedCxxQtData {
253220 . last ( )
254221 . map ( |obj| format_ident ! ( "{}" , obj. declaration. ident_left) ) ;
255222
256- Self :: try_inline_self_types ( inline_self, & inline_ident, & mut methods) ?;
257- Self :: try_inline_self_types ( inline_self, & inline_ident, & mut signals) ?;
258- Self :: try_inline_self_types ( inline_self, & inline_ident, & mut inherited) ?;
223+ try_inline_self_invokables ( inline_self, & inline_ident, & mut methods) ?;
224+ try_inline_self_invokables ( inline_self, & inline_ident, & mut signals) ?;
225+ try_inline_self_invokables ( inline_self, & inline_ident, & mut inherited) ?;
259226
260227 self . qobjects . extend ( qobjects) ;
261228 self . methods . extend ( methods) ;
@@ -792,92 +759,4 @@ mod tests {
792759 Some ( "b" )
793760 ) ;
794761 }
795-
796- #[ test]
797- fn test_self_inlining_ref ( ) {
798- let mut parsed_cxxqtdata = ParsedCxxQtData :: new ( format_ident ! ( "ffi" ) , None ) ;
799- let extern_rust_qt: Item = parse_quote ! {
800- unsafe extern "RustQt" {
801- #[ qobject]
802- type MyObject = super :: T ;
803-
804- fn my_method( & self ) ;
805-
806- #[ inherit]
807- fn my_inherited_method( & self ) ;
808- }
809- } ;
810-
811- parsed_cxxqtdata. parse_cxx_qt_item ( extern_rust_qt) . unwrap ( ) ;
812- }
813-
814- #[ test]
815- fn test_self_inlining_pin ( ) {
816- let mut parsed_cxxqtdata = ParsedCxxQtData :: new ( format_ident ! ( "ffi" ) , None ) ;
817- let extern_rust_qt: Item = parse_quote ! {
818- unsafe extern "RustQt" {
819- #[ qobject]
820- type MyObject = super :: T ;
821-
822- #[ qsignal]
823- fn my_signal( self : Pin <& mut Self >) ;
824- }
825- } ;
826-
827- let extern_cpp_qt: Item = parse_quote ! {
828- unsafe extern "C++Qt" {
829- #[ qobject]
830- type MyObject ;
831-
832- #[ qsignal]
833- fn my_signal( self : Pin <& mut Self >) ;
834- }
835- } ;
836-
837- parsed_cxxqtdata. parse_cxx_qt_item ( extern_rust_qt) . unwrap ( ) ;
838- parsed_cxxqtdata. parse_cxx_qt_item ( extern_cpp_qt) . unwrap ( ) ;
839- }
840-
841- #[ test]
842- fn test_self_inlining_methods_invalid ( ) {
843- assert_parse_errors ! {
844- |item| ParsedCxxQtData :: new( format_ident!( "ffi" ) , None ) . parse_cxx_qt_item( item) =>
845- // No QObject in block
846- {
847- extern "RustQt" {
848- fn my_method( & self ) ;
849- }
850- }
851-
852- {
853- extern "RustQt" {
854- fn my_method( self : Pin <& mut Self >) ;
855- }
856- }
857- // More than 1 QObjects in block
858- {
859- extern "RustQt" {
860- #[ qobject]
861- type MyObject = super :: T ;
862-
863- #[ qobject]
864- type MyOtherObject = super :: S ;
865-
866- fn my_method( & self ) ;
867- }
868- }
869- }
870- }
871-
872- #[ test]
873- fn test_invalid_inline_call ( ) {
874- let method_sig = parse_quote ! {
875- fn test( & self ) ;
876- } ;
877- let mut methods = vec ! [ ParsedMethod :: mock_qinvokable( & method_sig) ] ;
878-
879- // If inlining is set to take place, an Ident is required to inline, here it is `None`
880- let data = ParsedCxxQtData :: try_inline_self_types ( true , & None , & mut methods) ;
881- assert ! ( data. is_err( ) ) ;
882- }
883762}
0 commit comments