Skip to content

Add Transitive casting and testing code #1252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/src/generator/rust/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl GeneratedRustFragment {
}
}],
cxx_qt_mod_contents: vec![parse_quote! {
impl ::cxx_qt::Upcast<#base_qualified> for #struct_name {
unsafe impl ::cxx_qt::Upcast<#base_qualified> for #struct_name {
unsafe fn upcast_ptr(this: *const Self) -> *const #base_qualified {
#upcast_fn_qualified(this)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/cxx-qt-gen/test_outputs/cfgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ cxx_qt::static_assertions::assert_eq_size!(
cxx_qt::signalhandler::CxxQtSignalHandler<QObjectEnabledCxxQtSignalClosuresignal_enabled>,
[usize; 2]
);
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectEnabled {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectEnabled {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_QObjectEnabled_upcastPtr(this)
}
Expand Down Expand Up @@ -847,7 +847,7 @@ cxx_qt::static_assertions::assert_eq_size!(
cxx_qt::signalhandler::CxxQtSignalHandler<QObjectDisabledCxxQtSignalClosuresignal_enabled>,
[usize; 2]
);
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectDisabled {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectDisabled {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_QObjectDisabled_upcastPtr(this)
}
Expand Down Expand Up @@ -878,7 +878,7 @@ impl ::cxx_qt::CxxQtType for ffi::QObjectDisabled {
ffi::cxx_qt_ffi_QObjectDisabled_unsafeRustMut(self)
}
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectExternEnabled {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectExternEnabled {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_QObjectExternEnabled_upcastPtr(this)
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ cxx_qt::static_assertions::assert_eq_size!(
>,
[usize; 2]
);
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectExternDisabled {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectExternDisabled {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_QObjectExternDisabled_upcastPtr(this)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-gen/test_outputs/inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ mod inheritance {
type QObject = cxx_qt::QObject;
}
}
impl ::cxx_qt::Upcast<inheritance::QAbstractItemModel> for inheritance::MyObject {
unsafe impl ::cxx_qt::Upcast<inheritance::QAbstractItemModel> for inheritance::MyObject {
unsafe fn upcast_ptr(this: *const Self) -> *const inheritance::QAbstractItemModel {
inheritance::cxx_qt_ffi_MyObject_upcastPtr(this)
}
Expand Down Expand Up @@ -158,15 +158,15 @@ impl ::cxx_qt::CxxQtType for inheritance::MyObject {
inheritance::cxx_qt_ffi_MyObject_unsafeRustMut(self)
}
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for inheritance::QPushButton {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for inheritance::QPushButton {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
inheritance::cxx_qt_ffi_QPushButton_upcastPtr(this)
}
unsafe fn from_base_ptr(base: *const ::cxx_qt::QObject) -> *const Self {
inheritance::cxx_qt_ffi_QPushButton_downcastPtr(base)
}
}
impl ::cxx_qt::Upcast<inheritance::QPushButton> for inheritance::QPushButtonChild {
unsafe impl ::cxx_qt::Upcast<inheritance::QPushButton> for inheritance::QPushButtonChild {
unsafe fn upcast_ptr(this: *const Self) -> *const inheritance::QPushButton {
inheritance::cxx_qt_ffi_QPushButtonChild_upcastPtr(this)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/invokables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl cxx_qt::Threading for ffi::MyObject {
pub struct MyObjectCxxQtThreadQueuedFn {
inner: std::boxed::Box<dyn FnOnce(core::pin::Pin<&mut ffi::MyObject>) + Send>,
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/cxx-qt-gen/test_outputs/passthrough_and_naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ cxx_qt::static_assertions::assert_eq_size!(
cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosureready>,
[usize; 2]
);
impl ::cxx_qt::Upcast<ffi::QStringListModel> for ffi::MyObject {
unsafe impl ::cxx_qt::Upcast<ffi::QStringListModel> for ffi::MyObject {
unsafe fn upcast_ptr(this: *const Self) -> *const ffi::QStringListModel {
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
}
Expand Down Expand Up @@ -822,7 +822,7 @@ cxx_qt::static_assertions::assert_eq_size!(
cxx_qt::signalhandler::CxxQtSignalHandler<SecondObjectCxxQtSignalClosureready>,
[usize; 2]
);
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::SecondObject {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::SecondObject {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_SecondObject_upcastPtr(this)
}
Expand Down Expand Up @@ -850,7 +850,7 @@ impl ::cxx_qt::CxxQtType for ffi::SecondObject {
ffi::cxx_qt_ffi_SecondObject_unsafeRustMut(self)
}
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyRustName {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyRustName {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_MyCxxName_upcastPtr(this)
}
Expand Down Expand Up @@ -878,15 +878,15 @@ impl ::cxx_qt::CxxQtType for ffi::MyRustName {
ffi::cxx_qt_ffi_MyCxxName_unsafeRustMut(self)
}
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QPushButton {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QPushButton {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_QPushButton_upcastPtr(this)
}
unsafe fn from_base_ptr(base: *const ::cxx_qt::QObject) -> *const Self {
ffi::cxx_qt_ffi_QPushButton_downcastPtr(base)
}
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::ExternObject {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::ExternObject {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_ExternObjectCpp_upcastPtr(this)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-gen/test_outputs/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ cxx_qt::static_assertions::assert_eq_size!(
cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosuremy_on_changed>,
[usize; 2]
);
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cxx-qt-gen/test_outputs/qenum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ mod ffi {
type QObject = cxx_qt::QObject;
}
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
}
Expand Down Expand Up @@ -197,7 +197,7 @@ impl ::cxx_qt::CxxQtType for ffi::MyObject {
ffi::cxx_qt_ffi_MyObject_unsafeRustMut(self)
}
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyRenamedObject {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyRenamedObject {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_CxxName_upcastPtr(this)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cxx-qt-gen/test_outputs/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ cxx_qt::static_assertions::assert_eq_size!(
cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosurenewData>,
[usize; 2]
);
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
}
Expand Down Expand Up @@ -483,7 +483,7 @@ impl ::cxx_qt::CxxQtType for ffi::MyObject {
ffi::cxx_qt_ffi_MyObject_unsafeRustMut(self)
}
}
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QTimer {
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QTimer {
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
ffi::cxx_qt_ffi_QTimer_upcastPtr(this)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-lib/src/core/qstringlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl DerefMut for QStringList {
}
}

impl Upcast<QList_QString> for QStringList {
unsafe impl Upcast<QList_QString> for QStringList {
unsafe fn upcast_ptr(this: *const Self) -> *const QList_QString {
ffi::upcast_qstringlist(this)
}
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 @@ -130,7 +130,7 @@ pub trait Threading: Sized {
/// This trait is automatically implemented by CXX-Qt and you most likely do not need to manually implement it.
/// Allows upcasting to either [QObject] or the provided base class of a type.
/// Will not be implemented if no types inherit from [QObject] or have the `#[base = T]` attribute.
pub trait Upcast<T> {
pub unsafe trait Upcast<T> {
#[doc(hidden)]
/// # Safety
///
Expand Down
53 changes: 50 additions & 3 deletions examples/qml_features/rust/src/custom_base_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#[cxx_qt::bridge]
pub mod qobject {
// ANCHOR: book_base_include
unsafe extern "C++" {
unsafe extern "C++Qt" {
include!(<QtCore/QAbstractListModel>);
/// Base for Qt type
#[qobject]
type QAbstractListModel;
}
// ANCHOR_END: book_base_include
Expand Down Expand Up @@ -272,11 +273,51 @@ pub mod qobject {
}
}

use crate::custom_base_class::qobject::CustomBaseClass;
macro_rules! impl_transitive_cast {
($first:ty, $second:ty, $third:ty) => {
unsafe impl cxx_qt::Upcast<$third> for $first {
unsafe fn upcast_ptr(this: *const Self) -> *const $third {
let base = Self::upcast_ptr(this);
<$second as Upcast<$third>>::upcast_ptr(base)
}

unsafe fn from_base_ptr(base: *const $third) -> *const Self {
let base = <$second as Upcast<$third>>::from_base_ptr(base);
if base.is_null() {
std::ptr::null()
} else {
Self::from_base_ptr(base)
}
}
}
};
}

macro_rules! chain_cast {
($first:ty, $second:ty, $third:ty) => {
impl_transitive_cast!($first, $second, $third);
};

($first:ty, $second:ty, $third:ty, $($rest:ty),*) => {
impl_transitive_cast!($first, $second, $third);
$(impl_transitive_cast!($first, $third, $rest);)*
};
}

use crate::custom_base_class::qobject::{
AbstractBaseClass, CustomBaseClass, QAbstractListModel, QObject,
};
use core::pin::Pin;
use cxx_qt::{CxxQtType, Threading};
use cxx_qt::{CxxQtType, Threading, Upcast};
use cxx_qt_lib::{QByteArray, QHash, QHashPair_i32_QByteArray, QModelIndex, QVariant, QVector};

chain_cast!(
CustomBaseClass,
AbstractBaseClass,
QAbstractListModel,
QObject
);

impl Default for qobject::State {
fn default() -> Self {
Self::Idle
Expand All @@ -300,6 +341,12 @@ pub struct CustomBaseClassRust {
impl qobject::CustomBaseClass {
/// Virtual method for logging type
pub fn log(self: &CustomBaseClass) {
let _abs: &AbstractBaseClass = self.upcast();
let _qabs: &QAbstractListModel = self.upcast();
let qobj: &QObject = self.upcast();

qobj.dump_object_info();

println!(
"state: {}\npending adds: {}\nid: {}\nvector: {:?}\n",
self.state.repr, self.pending_adds, self.id, self.vector
Expand Down
Loading