Skip to content

Commit

Permalink
threading: do not use exceptions and instead use return values
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Nov 18, 2024
1 parent 19bd854 commit 4391cbb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 33 deletions.
20 changes: 14 additions & 6 deletions crates/cxx-qt-gen/src/generator/rust/threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn generate(
cxx_qt_thread: &#cxx_qt_thread_ident,
func: fn(Pin<&mut #cpp_struct_ident>, Box<#cxx_qt_thread_queued_fn_ident>),
arg: Box<#cxx_qt_thread_queued_fn_ident>,
) -> Result<()>;
) -> bool;

#[doc(hidden)]
#(#thread_clone_attrs)*
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn generate(
}

#[doc(hidden)]
fn queue<F>(cxx_qt_thread: &#module_ident::#cxx_qt_thread_ident, f: F) -> std::result::Result<(), cxx::Exception>
fn queue<F>(cxx_qt_thread: &#module_ident::#cxx_qt_thread_ident, f: F) -> std::result::Result<(), ()>
where
F: FnOnce(core::pin::Pin<&mut #qualified_impl>),
F: Send + 'static,
Expand All @@ -138,7 +138,11 @@ pub fn generate(
(arg.inner)(obj)
}
let arg = #cxx_qt_thread_queued_fn_ident { inner: std::boxed::Box::new(f) };
#thread_queue_qualified(cxx_qt_thread, func, std::boxed::Box::new(arg))
if #thread_queue_qualified(cxx_qt_thread, func, std::boxed::Box::new(arg)) {
Ok(())
} else {
Err(())
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -221,7 +225,7 @@ mod tests {
cxx_qt_thread: &MyObjectCxxQtThread,
func: fn(Pin<&mut MyObject>, Box<MyObjectCxxQtThreadQueuedFn>),
arg: Box<MyObjectCxxQtThreadQueuedFn>,
) -> Result<()>;
) -> bool;

#[doc(hidden)]
#[cxx_name = "cxxQtThreadClone"]
Expand Down Expand Up @@ -269,7 +273,7 @@ mod tests {
}

#[doc(hidden)]
fn queue<F>(cxx_qt_thread: &qobject::MyObjectCxxQtThread, f: F) -> std::result::Result<(), cxx::Exception>
fn queue<F>(cxx_qt_thread: &qobject::MyObjectCxxQtThread, f: F) -> std::result::Result<(), ()>
where
F: FnOnce(core::pin::Pin<&mut qobject::MyObject>),
F: Send + 'static,
Expand All @@ -286,7 +290,11 @@ mod tests {
(arg.inner)(obj)
}
let arg = MyObjectCxxQtThreadQueuedFn { inner: std::boxed::Box::new(f) };
qobject::cxx_qt_ffi_MyObject_cxxQtThreadQueue(cxx_qt_thread, func, std::boxed::Box::new(arg))
if qobject::cxx_qt_ffi_MyObject_cxxQtThreadQueue(cxx_qt_thread, func, std::boxed::Box::new(arg)) {
Ok(())
} else {
Err(())
}
}

#[doc(hidden)]
Expand Down
14 changes: 8 additions & 6 deletions crates/cxx-qt-gen/test_outputs/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ mod ffi {
cxx_qt_thread: &MyObjectCxxQtThread,
func: fn(Pin<&mut MyObject>, Box<MyObjectCxxQtThreadQueuedFn>),
arg: Box<MyObjectCxxQtThreadQueuedFn>,
) -> Result<()>;
) -> bool;
#[doc(hidden)]
#[cxx_name = "cxxQtThreadClone"]
#[namespace = "rust::cxxqt1"]
Expand Down Expand Up @@ -259,10 +259,7 @@ impl cxx_qt::Threading for ffi::MyObject {
ffi::cxx_qt_ffi_MyObject_cxxQtThreadIsDestroyed(cxx_qt_thread)
}
#[doc(hidden)]
fn queue<F>(
cxx_qt_thread: &ffi::MyObjectCxxQtThread,
f: F,
) -> std::result::Result<(), cxx::Exception>
fn queue<F>(cxx_qt_thread: &ffi::MyObjectCxxQtThread, f: F) -> std::result::Result<(), ()>
where
F: FnOnce(core::pin::Pin<&mut ffi::MyObject>),
F: Send + 'static,
Expand All @@ -278,7 +275,12 @@ impl cxx_qt::Threading for ffi::MyObject {
let arg = MyObjectCxxQtThreadQueuedFn {
inner: std::boxed::Box::new(f),
};
ffi::cxx_qt_ffi_MyObject_cxxQtThreadQueue(cxx_qt_thread, func, std::boxed::Box::new(arg))
if ffi::cxx_qt_ffi_MyObject_cxxQtThreadQueue(cxx_qt_thread, func, std::boxed::Box::new(arg))
{
Ok(())
} else {
Err(())
}
}
#[doc(hidden)]
fn threading_clone(cxx_qt_thread: &ffi::MyObjectCxxQtThread) -> ffi::MyObjectCxxQtThread {
Expand Down
28 changes: 9 additions & 19 deletions crates/cxx-qt/include/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,15 @@ class CxxQtThread final
}

template<typename A>
void queue(::rust::Fn<void(T& self, ::rust::Box<A> arg)> func,
bool queue(::rust::Fn<void(T& self, ::rust::Box<A> arg)> func,
::rust::Box<A> arg) const
{
// Ensure that we can read the pointer and it's not being written to
const auto guard = ::std::shared_lock(m_obj->mutex);
if (!m_obj->ptr) {
#if defined(RUST_CXX_NO_EXCEPTIONS)
std::cerr << "Cannot queue function pointer as object has been destroyed"
<< std::endl;
std::abort();
#else
throw ::std::runtime_error(
"Cannot queue function pointer as object has been destroyed");
#endif
return;
qWarning()
<< "Cannot queue function pointer as object has been destroyed";
return false;
}

// Construct the lambda
Expand All @@ -88,16 +82,12 @@ class CxxQtThread final
// Add the lambda to the queue
if (!QMetaObject::invokeMethod(
m_obj->ptr, ::std::move(lambda), Qt::QueuedConnection)) {
#if defined(RUST_CXX_NO_EXCEPTIONS)
std::cerr
<< "Cannot queue function pointer as invokeMethod on object failed"
<< std::endl;
std::abort();
#else
throw ::std::runtime_error(
"Cannot queue function pointer as invokeMethod on object failed");
#endif
qWarning()
<< "Cannot queue function pointer as invokeMethod on object failed";
return false;
}

return true;
}

private:
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub trait Threading: Sized {
fn is_destroyed(cxx_qt_thread: &CxxQtThread<Self>) -> bool;

#[doc(hidden)]
fn queue<F>(cxx_qt_thread: &CxxQtThread<Self>, f: F) -> Result<(), cxx::Exception>
fn queue<F>(cxx_qt_thread: &CxxQtThread<Self>, f: F) -> Result<(), ()>
where
F: FnOnce(core::pin::Pin<&mut Self>),
F: Send + 'static;
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt/src/threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ where
///
/// The first argument of the closure is a pinned mutable reference to the QObject.
/// With this parameter, you can then update the QObject to reflect any state changes that have occured in the background thread.
pub fn queue<F>(&self, f: F) -> Result<(), cxx::Exception>
pub fn queue<F>(&self, f: F) -> Result<(), ()>
where
F: FnOnce(Pin<&mut T>),
F: Send + 'static,
Expand Down

0 comments on commit 4391cbb

Please sign in to comment.