File tree 7 files changed +59
-3
lines changed
examples/qml_features/rust/src
7 files changed +59
-3
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,12 @@ mod ffi {
35
35
36
36
#[ qinvokable( cxx_virtual) ]
37
37
fn invokable_virtual ( self : & MyObject ) ;
38
+
39
+ #[ qinvokable]
40
+ fn invokable_result_tuple ( self : & MyObject ) -> Result < ( ) > ;
41
+
42
+ #[ qinvokable]
43
+ fn invokable_result_type ( self : & MyObject ) -> Result < String > ;
38
44
}
39
45
40
46
impl cxx_qt:: Threading for MyObject { }
Original file line number Diff line number Diff line change @@ -78,6 +78,20 @@ MyObject::invokableVirtual() const
78
78
invokableVirtualWrapper ();
79
79
}
80
80
81
+ void
82
+ MyObject::invokableResultTuple () const
83
+ {
84
+ const ::std::lock_guard<::std::recursive_mutex> guard (*m_rustObjMutex);
85
+ invokableResultTupleWrapper ();
86
+ }
87
+
88
+ ::rust::String
89
+ MyObject::invokableResultType () const
90
+ {
91
+ const ::std::lock_guard<::std::recursive_mutex> guard (*m_rustObjMutex);
92
+ return invokableResultTypeWrapper ();
93
+ }
94
+
81
95
static_assert (alignof(MyObjectCxxQtThread) <= alignof(::std::size_t ),
82
96
" unexpected aligment" );
83
97
static_assert (sizeof (MyObjectCxxQtThread) == sizeof (::std::size_t [4 ]),
Original file line number Diff line number Diff line change @@ -36,6 +36,8 @@ class MyObject : public QObject
36
36
Q_INVOKABLE void invokableFinal () const final ;
37
37
Q_INVOKABLE void invokableOverride () const override ;
38
38
Q_INVOKABLE virtual void invokableVirtual () const ;
39
+ Q_INVOKABLE void invokableResultTuple () const ;
40
+ Q_INVOKABLE ::rust::String invokableResultType () const ;
39
41
MyObjectCxxQtThread qtThread () const ;
40
42
explicit MyObject (::std::int32_t arg0, QObject* arg1);
41
43
@@ -50,6 +52,8 @@ class MyObject : public QObject
50
52
void invokableFinalWrapper () const noexcept ;
51
53
void invokableOverrideWrapper () const noexcept ;
52
54
void invokableVirtualWrapper () const noexcept ;
55
+ void invokableResultTupleWrapper () const ;
56
+ ::rust::String invokableResultTypeWrapper () const ;
53
57
explicit MyObject (
54
58
cxx_qt::my_object::cxx_qt_my_object::CxxQtConstructorArguments0&& args);
55
59
Original file line number Diff line number Diff line change @@ -75,6 +75,16 @@ mod ffi {
75
75
#[ cxx_name = "invokableVirtualWrapper" ]
76
76
fn invokable_virtual ( self : & MyObject ) ;
77
77
}
78
+ extern "Rust" {
79
+ #[ doc( hidden) ]
80
+ #[ cxx_name = "invokableResultTupleWrapper" ]
81
+ fn invokable_result_tuple ( self : & MyObject ) -> Result < ( ) > ;
82
+ }
83
+ extern "Rust" {
84
+ #[ doc( hidden) ]
85
+ #[ cxx_name = "invokableResultTypeWrapper" ]
86
+ fn invokable_result_type ( self : & MyObject ) -> Result < String > ;
87
+ }
78
88
unsafe extern "C++" {
79
89
#[ doc( hidden) ]
80
90
type MyObjectCxxQtThread = cxx_qt:: CxxQtThread < MyObject > ;
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ pub mod qobject {
24
24
unsafe extern "RustQt" {
25
25
/// Immutable invokable method that returns the QColor
26
26
#[ qinvokable]
27
- fn load_color ( self : & RustInvokables ) -> QColor ;
27
+ fn load_color ( self : & RustInvokables ) -> Result < QColor > ;
28
28
29
29
/// Mutable invokable method that stores a color
30
30
#[ qinvokable]
@@ -61,8 +61,8 @@ impl Default for RustInvokablesRust {
61
61
// ANCHOR: book_invokable_impl
62
62
impl qobject:: RustInvokables {
63
63
/// Immutable invokable method that returns the QColor
64
- fn load_color ( & self ) -> QColor {
65
- self . as_qcolor ( )
64
+ fn load_color ( & self ) -> Result < QColor , i32 > {
65
+ Ok ( self . as_qcolor ( ) )
66
66
}
67
67
68
68
/// Mutable invokable method that stores a color
Original file line number Diff line number Diff line change @@ -260,6 +260,21 @@ private Q_SLOTS:
260
260
261
261
// Tests that we can build an empty QObject end to end
262
262
void testEmpty () { Empty empty; }
263
+
264
+ void testThrowException ()
265
+ {
266
+ cxx_qt::my_object::MyObject obj;
267
+ bool thrown = false ;
268
+ try {
269
+ obj.throwException ();
270
+ Q_UNREACHABLE ();
271
+ } catch (const rust::Error& e) {
272
+ QCOMPARE (e.what (), " RustException" );
273
+ thrown = true ;
274
+ }
275
+
276
+ QCOMPARE (thrown, true );
277
+ }
263
278
};
264
279
265
280
QTEST_MAIN (CxxQtTest)
Original file line number Diff line number Diff line change @@ -45,6 +45,9 @@ mod qobject {
45
45
46
46
#[ qinvokable]
47
47
fn fetch_update_call_count ( self : & MyObject ) -> i32 ;
48
+
49
+ #[ qinvokable]
50
+ fn throw_exception ( self : & MyObject ) -> Result < i32 > ;
48
51
}
49
52
}
50
53
@@ -124,4 +127,8 @@ impl qobject::MyObject {
124
127
fn fetch_update_call_count ( & self ) -> i32 {
125
128
self . update_call_count
126
129
}
130
+
131
+ fn throw_exception ( & self ) -> Result < i32 , String > {
132
+ Err ( "RustException" . to_string ( ) )
133
+ }
127
134
}
You can’t perform that action at this time.
0 commit comments