Skip to content

Commit

Permalink
WIP: examples: use Result in qml_features and test_inputs
Browse files Browse the repository at this point in the history
Related to #404
  • Loading branch information
ahayzen-kdab committed Jul 19, 2023
1 parent 5212017 commit 3618ed4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
6 changes: 6 additions & 0 deletions crates/cxx-qt-gen/test_inputs/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ mod ffi {

#[qinvokable(cxx_virtual)]
fn invokable_virtual(self: &MyObject);

#[qinvokable]
fn invokable_result_tuple(self: &MyObject) -> Result<()>;

#[qinvokable]
fn invokable_result_type(self: &MyObject) -> Result<String>;
}

impl cxx_qt::Threading for MyObject {}
Expand Down
14 changes: 14 additions & 0 deletions crates/cxx-qt-gen/test_outputs/invokables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ MyObject::invokableVirtual() const
m_rustObj->invokableVirtualWrapper(*this);
}

void
MyObject::invokableResultTuple() const
{
const ::std::lock_guard<::std::recursive_mutex> guard(*m_rustObjMutex);
m_rustObj->invokableResultTupleWrapper(*this);
}

::rust::String
MyObject::invokableResultType() const
{
const ::std::lock_guard<::std::recursive_mutex> guard(*m_rustObjMutex);
return m_rustObj->invokableResultTypeWrapper(*this);
}

static_assert(alignof(MyObjectCxxQtThread) <= alignof(::std::size_t),
"unexpected aligment");
static_assert(sizeof(MyObjectCxxQtThread) == sizeof(::std::size_t[4]),
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-gen/test_outputs/invokables.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class MyObject : public QObject
Q_INVOKABLE void invokableFinal() const final;
Q_INVOKABLE void invokableOverride() const override;
Q_INVOKABLE virtual void invokableVirtual() const;
Q_INVOKABLE void invokableResultTuple() const;
Q_INVOKABLE ::rust::String invokableResultType() const;
MyObjectCxxQtThread qtThread() const;
explicit MyObject(::std::int32_t arg0, QObject* arg1);

Expand Down
23 changes: 23 additions & 0 deletions crates/cxx-qt-gen/test_outputs/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ mod ffi {
#[cxx_name = "invokableVirtualWrapper"]
fn invokable_virtual_wrapper(self: &MyObjectRust, cpp: &MyObject);
}
extern "Rust" {
#[cxx_name = "invokableResultTupleWrapper"]
fn invokable_result_tuple_wrapper(self: &MyObjectRust, cpp: &MyObject) -> Result<()>;
}
extern "Rust" {
#[cxx_name = "invokableResultTypeWrapper"]
fn invokable_result_type_wrapper(self: &MyObjectRust, cpp: &MyObject) -> Result<String>;
}
unsafe extern "C++" {
#[doc(hidden)]
type MyObjectCxxQtThread = cxx_qt::CxxQtThread<MyObject>;
Expand Down Expand Up @@ -234,6 +242,21 @@ pub mod cxx_qt_ffi {
cpp.invokable_virtual();
}
}
impl MyObjectRust {
#[doc(hidden)]
pub fn invokable_result_tuple_wrapper(self: &MyObjectRust, cpp: &MyObject) -> Result<()> {
return cpp.invokable_result_tuple();
}
}
impl MyObjectRust {
#[doc(hidden)]
pub fn invokable_result_type_wrapper(
self: &MyObjectRust,
cpp: &MyObject,
) -> Result<String> {
return cpp.invokable_result_type();
}
}
impl cxx_qt::Threading for MyObject {
type BoxedQueuedFn = MyObjectCxxQtThreadQueuedFn;
type ThreadingTypeId = cxx::type_id!("cxx_qt::my_object::MyObjectCxxQtThread");
Expand Down
6 changes: 3 additions & 3 deletions examples/qml_features/rust/src/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod qobject {
unsafe extern "RustQt" {
/// Immutable invokable method that returns the QColor
#[qinvokable]
fn load_color(self: &RustInvokables) -> QColor;
fn load_color(self: &RustInvokables) -> Result<QColor>;

/// Mutable invokable method that stores a color
#[qinvokable]
Expand Down Expand Up @@ -61,8 +61,8 @@ impl Default for RustInvokablesRust {
// ANCHOR: book_invokable_impl
impl qobject::RustInvokables {
/// Immutable invokable method that returns the QColor
fn load_color(&self) -> QColor {
self.as_qcolor()
fn load_color(&self) -> Result<QColor, i32> {
Ok(self.as_qcolor())
}

/// Mutable invokable method that stores a color
Expand Down

0 comments on commit 3618ed4

Please sign in to comment.