Skip to content

Commit a345666

Browse files
ahayzen-kdabBe-ing
authored andcommitted
examples: use Result in qml_features and test_inputs
And add a test to ensure we can catch a thrown exception. Related to #404
1 parent 638053e commit a345666

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
lines changed

crates/cxx-qt-gen/test_inputs/invokables.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ mod ffi {
3535

3636
#[qinvokable(cxx_virtual)]
3737
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>;
3844
}
3945

4046
impl cxx_qt::Threading for MyObject {}

crates/cxx-qt-gen/test_outputs/invokables.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ MyObject::invokableVirtual() const
7878
invokableVirtualWrapper();
7979
}
8080

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+
8195
static_assert(alignof(MyObjectCxxQtThread) <= alignof(::std::size_t),
8296
"unexpected aligment");
8397
static_assert(sizeof(MyObjectCxxQtThread) == sizeof(::std::size_t[4]),

crates/cxx-qt-gen/test_outputs/invokables.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class MyObject : public QObject
3636
Q_INVOKABLE void invokableFinal() const final;
3737
Q_INVOKABLE void invokableOverride() const override;
3838
Q_INVOKABLE virtual void invokableVirtual() const;
39+
Q_INVOKABLE void invokableResultTuple() const;
40+
Q_INVOKABLE ::rust::String invokableResultType() const;
3941
MyObjectCxxQtThread qtThread() const;
4042
explicit MyObject(::std::int32_t arg0, QObject* arg1);
4143

@@ -50,6 +52,8 @@ class MyObject : public QObject
5052
void invokableFinalWrapper() const noexcept;
5153
void invokableOverrideWrapper() const noexcept;
5254
void invokableVirtualWrapper() const noexcept;
55+
void invokableResultTupleWrapper() const;
56+
::rust::String invokableResultTypeWrapper() const;
5357
explicit MyObject(
5458
cxx_qt::my_object::cxx_qt_my_object::CxxQtConstructorArguments0&& args);
5559

crates/cxx-qt-gen/test_outputs/invokables.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ mod ffi {
7575
#[cxx_name = "invokableVirtualWrapper"]
7676
fn invokable_virtual(self: &MyObject);
7777
}
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+
}
7888
unsafe extern "C++" {
7989
#[doc(hidden)]
8090
type MyObjectCxxQtThread = cxx_qt::CxxQtThread<MyObject>;

examples/qml_features/rust/src/invokables.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub mod qobject {
2424
unsafe extern "RustQt" {
2525
/// Immutable invokable method that returns the QColor
2626
#[qinvokable]
27-
fn load_color(self: &RustInvokables) -> QColor;
27+
fn load_color(self: &RustInvokables) -> Result<QColor>;
2828

2929
/// Mutable invokable method that stores a color
3030
#[qinvokable]
@@ -61,8 +61,8 @@ impl Default for RustInvokablesRust {
6161
// ANCHOR: book_invokable_impl
6262
impl qobject::RustInvokables {
6363
/// 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())
6666
}
6767

6868
/// Mutable invokable method that stores a color

tests/basic_cxx_qt/cpp/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,21 @@ private Q_SLOTS:
260260

261261
// Tests that we can build an empty QObject end to end
262262
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+
}
263278
};
264279

265280
QTEST_MAIN(CxxQtTest)

tests/basic_cxx_qt/rust/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ mod qobject {
4545

4646
#[qinvokable]
4747
fn fetch_update_call_count(self: &MyObject) -> i32;
48+
49+
#[qinvokable]
50+
fn throw_exception(self: &MyObject) -> Result<i32>;
4851
}
4952
}
5053

@@ -124,4 +127,8 @@ impl qobject::MyObject {
124127
fn fetch_update_call_count(&self) -> i32 {
125128
self.update_call_count
126129
}
130+
131+
fn throw_exception(&self) -> Result<i32, String> {
132+
Err("RustException".to_string())
133+
}
127134
}

0 commit comments

Comments
 (0)