Skip to content

Commit

Permalink
examples: cfg so we do not generate exceptions for wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Nov 18, 2024
1 parent 110adb1 commit 0fe170f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
18 changes: 16 additions & 2 deletions examples/qml_features/rust/src/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ pub mod qobject {
/// Immutable invokable method that returns the QColor
#[qinvokable]
#[cxx_name = "loadColor"]
fn load_color(self: &RustInvokables) -> Result<QColor>;
#[cfg(not(target_family = "wasm"))]
fn load_color_result(self: &RustInvokables) -> Result<QColor>;

/// Immutable invokable method that returns the QColor
#[qinvokable]
#[cxx_name = "loadColor"]
#[cfg(target_family = "wasm")]
fn load_color(self: &RustInvokables) -> QColor;

/// Mutable invokable method that stores a color
#[qinvokable]
Expand Down Expand Up @@ -96,7 +103,14 @@ impl Default for RustInvokablesRust {
// ANCHOR: book_invokable_impl
impl qobject::RustInvokables {
/// Immutable invokable method that returns the QColor
pub fn load_color(&self) -> Result<QColor, i32> {
#[cfg(target_family = "wasm")]
pub fn load_color(&self) -> QColor {
self.as_qcolor()
}

/// Immutable invokable method that returns the QColor
#[cfg(not(target_family = "wasm"))]
pub fn load_color_result(&self) -> Result<QColor, i32> {
Ok(self.as_qcolor())
}

Expand Down
2 changes: 2 additions & 0 deletions tests/basic_cxx_qt/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private Q_SLOTS:
// Tests that we can build an empty QObject end to end
void testEmpty() { Empty empty; }

#ifndef RUST_CXX_NO_EXCEPTION
void testThrowException()
{
cxx_qt::my_object::MyObject obj;
Expand All @@ -216,6 +217,7 @@ private Q_SLOTS:

QCOMPARE(thrown, true);
}
#endif
};

QTEST_MAIN(CxxQtTest)
Expand Down
2 changes: 2 additions & 0 deletions tests/basic_cxx_qt/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod qobject {
#[cxx_name = "fetchUpdateCallCount"]
fn fetch_update_call_count(self: &MyObject) -> i32;

#[cfg(not(target_family = "wasm"))]
#[cxx_name = "throwException"]
fn throw_exception(self: &MyObject) -> Result<i32>;
}
Expand Down Expand Up @@ -129,6 +130,7 @@ impl qobject::MyObject {
self.update_call_count
}

#[cfg(not(target_family = "wasm"))]
fn throw_exception(&self) -> Result<i32, String> {
Err("RustException".to_string())
}
Expand Down

0 comments on commit 0fe170f

Please sign in to comment.