Skip to content

Commit cda393b

Browse files
Add Transitive casting and testing code
1 parent 358748d commit cda393b

File tree

11 files changed

+48
-51
lines changed

11 files changed

+48
-51
lines changed

crates/cxx-qt-gen/src/generator/rust/fragment.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,25 @@ impl GeneratedRustFragment {
7777
unsafe fn #downcast_fn(base: *const #base_unqualified) -> *const #struct_name_unqualified;
7878
}
7979
}],
80-
cxx_qt_mod_contents: vec![parse_quote! {
81-
impl ::cxx_qt::Upcast<#base_qualified> for #struct_name {
82-
unsafe fn upcast_ptr(this: *const Self) -> *const #base_qualified {
83-
#upcast_fn_qualified(this)
84-
}
80+
cxx_qt_mod_contents: vec![
81+
parse_quote! {
82+
unsafe impl ::cxx_qt::Upcast<#base_qualified> for #struct_name {
83+
unsafe fn upcast_ptr(this: *const Self) -> *const #base_qualified {
84+
#upcast_fn_qualified(this)
85+
}
8586

86-
unsafe fn from_base_ptr(base: *const #base_qualified) -> *const Self {
87-
#downcast_fn_qualified(base)
87+
unsafe fn from_base_ptr(base: *const #base_qualified) -> *const Self {
88+
#downcast_fn_qualified(base)
89+
}
8890
}
89-
}
90-
}],
91+
},
92+
// Add back once we figure out the bug with QObject, for automatic transitive casts
93+
// parse_quote! {
94+
// unsafe impl ::cxx_qt::MainCast for #struct_name {
95+
// type Base = #base_qualified;
96+
// }
97+
// }
98+
],
9199
})
92100
}
93101

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ cxx_qt::static_assertions::assert_eq_size!(
677677
cxx_qt::signalhandler::CxxQtSignalHandler<QObjectEnabledCxxQtSignalClosuresignal_enabled>,
678678
[usize; 2]
679679
);
680-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectEnabled {
680+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectEnabled {
681681
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
682682
ffi::cxx_qt_ffi_QObjectEnabled_upcastPtr(this)
683683
}
@@ -864,7 +864,7 @@ cxx_qt::static_assertions::assert_eq_size!(
864864
cxx_qt::signalhandler::CxxQtSignalHandler<QObjectDisabledCxxQtSignalClosuresignal_enabled>,
865865
[usize; 2]
866866
);
867-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectDisabled {
867+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectDisabled {
868868
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
869869
ffi::cxx_qt_ffi_QObjectDisabled_upcastPtr(this)
870870
}
@@ -895,7 +895,7 @@ impl ::cxx_qt::CxxQtType for ffi::QObjectDisabled {
895895
ffi::cxx_qt_ffi_QObjectDisabled_unsafeRustMut(self)
896896
}
897897
}
898-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectExternEnabled {
898+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectExternEnabled {
899899
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
900900
ffi::cxx_qt_ffi_QObjectExternEnabled_upcastPtr(this)
901901
}
@@ -1073,7 +1073,7 @@ cxx_qt::static_assertions::assert_eq_size!(
10731073
>,
10741074
[usize; 2]
10751075
);
1076-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectExternDisabled {
1076+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QObjectExternDisabled {
10771077
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
10781078
ffi::cxx_qt_ffi_QObjectExternDisabled_upcastPtr(this)
10791079
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ mod inheritance {
130130
type QObject = cxx_qt::QObject;
131131
}
132132
}
133-
impl ::cxx_qt::Upcast<inheritance::QAbstractItemModel> for inheritance::MyObject {
133+
unsafe impl ::cxx_qt::Upcast<inheritance::QAbstractItemModel> for inheritance::MyObject {
134134
unsafe fn upcast_ptr(this: *const Self) -> *const inheritance::QAbstractItemModel {
135135
inheritance::cxx_qt_ffi_MyObject_upcastPtr(this)
136136
}
@@ -158,15 +158,15 @@ impl ::cxx_qt::CxxQtType for inheritance::MyObject {
158158
inheritance::cxx_qt_ffi_MyObject_unsafeRustMut(self)
159159
}
160160
}
161-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for inheritance::QPushButton {
161+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for inheritance::QPushButton {
162162
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
163163
inheritance::cxx_qt_ffi_QPushButton_upcastPtr(this)
164164
}
165165
unsafe fn from_base_ptr(base: *const ::cxx_qt::QObject) -> *const Self {
166166
inheritance::cxx_qt_ffi_QPushButton_downcastPtr(base)
167167
}
168168
}
169-
impl ::cxx_qt::Upcast<inheritance::QPushButton> for inheritance::QPushButtonChild {
169+
unsafe impl ::cxx_qt::Upcast<inheritance::QPushButton> for inheritance::QPushButtonChild {
170170
unsafe fn upcast_ptr(this: *const Self) -> *const inheritance::QPushButton {
171171
inheritance::cxx_qt_ffi_QPushButtonChild_upcastPtr(this)
172172
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl cxx_qt::Threading for ffi::MyObject {
326326
pub struct MyObjectCxxQtThreadQueuedFn {
327327
inner: std::boxed::Box<dyn FnOnce(core::pin::Pin<&mut ffi::MyObject>) + Send>,
328328
}
329-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
329+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
330330
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
331331
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
332332
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ cxx_qt::static_assertions::assert_eq_size!(
659659
cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosureready>,
660660
[usize; 2]
661661
);
662-
impl ::cxx_qt::Upcast<ffi::QStringListModel> for ffi::MyObject {
662+
unsafe impl ::cxx_qt::Upcast<ffi::QStringListModel> for ffi::MyObject {
663663
unsafe fn upcast_ptr(this: *const Self) -> *const ffi::QStringListModel {
664664
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
665665
}
@@ -839,7 +839,7 @@ cxx_qt::static_assertions::assert_eq_size!(
839839
cxx_qt::signalhandler::CxxQtSignalHandler<SecondObjectCxxQtSignalClosureready>,
840840
[usize; 2]
841841
);
842-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::SecondObject {
842+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::SecondObject {
843843
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
844844
ffi::cxx_qt_ffi_SecondObject_upcastPtr(this)
845845
}
@@ -867,7 +867,7 @@ impl ::cxx_qt::CxxQtType for ffi::SecondObject {
867867
ffi::cxx_qt_ffi_SecondObject_unsafeRustMut(self)
868868
}
869869
}
870-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyRustName {
870+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyRustName {
871871
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
872872
ffi::cxx_qt_ffi_MyCxxName_upcastPtr(this)
873873
}
@@ -895,15 +895,15 @@ impl ::cxx_qt::CxxQtType for ffi::MyRustName {
895895
ffi::cxx_qt_ffi_MyCxxName_unsafeRustMut(self)
896896
}
897897
}
898-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QPushButton {
898+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QPushButton {
899899
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
900900
ffi::cxx_qt_ffi_QPushButton_upcastPtr(this)
901901
}
902902
unsafe fn from_base_ptr(base: *const ::cxx_qt::QObject) -> *const Self {
903903
ffi::cxx_qt_ffi_QPushButton_downcastPtr(base)
904904
}
905905
}
906-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::ExternObject {
906+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::ExternObject {
907907
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
908908
ffi::cxx_qt_ffi_ExternObjectCpp_upcastPtr(this)
909909
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ cxx_qt::static_assertions::assert_eq_size!(
11101110
cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosuremy_on_changed>,
11111111
[usize; 2]
11121112
);
1113-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
1113+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
11141114
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
11151115
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
11161116
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ mod ffi {
169169
type QObject = cxx_qt::QObject;
170170
}
171171
}
172-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
172+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
173173
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
174174
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
175175
}
@@ -197,7 +197,7 @@ impl ::cxx_qt::CxxQtType for ffi::MyObject {
197197
ffi::cxx_qt_ffi_MyObject_unsafeRustMut(self)
198198
}
199199
}
200-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyRenamedObject {
200+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyRenamedObject {
201201
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
202202
ffi::cxx_qt_ffi_CxxName_upcastPtr(this)
203203
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ cxx_qt::static_assertions::assert_eq_size!(
464464
cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosurenewData>,
465465
[usize; 2]
466466
);
467-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
467+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::MyObject {
468468
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
469469
ffi::cxx_qt_ffi_MyObject_upcastPtr(this)
470470
}
@@ -492,7 +492,7 @@ impl ::cxx_qt::CxxQtType for ffi::MyObject {
492492
ffi::cxx_qt_ffi_MyObject_unsafeRustMut(self)
493493
}
494494
}
495-
impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QTimer {
495+
unsafe impl ::cxx_qt::Upcast<::cxx_qt::QObject> for ffi::QTimer {
496496
unsafe fn upcast_ptr(this: *const Self) -> *const ::cxx_qt::QObject {
497497
ffi::cxx_qt_ffi_QTimer_upcastPtr(this)
498498
}

crates/cxx-qt-lib/src/core/qstringlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl DerefMut for QStringList {
204204
}
205205
}
206206

207-
impl Upcast<QList<QString>> for QStringList {
207+
unsafe impl Upcast<QList<QString>> for QStringList {
208208
unsafe fn upcast_ptr(this: *const Self) -> *const QList<QString> {
209209
ffi::upcast_qstringlist(this)
210210
}

crates/cxx-qt/src/lib.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pub trait Threading: Sized {
219219
/// This trait is automatically implemented by CXX-Qt and you most likely do not need to manually implement it.
220220
/// Allows upcasting to either [QObject] or the provided base class of a type.
221221
/// Will not be implemented if no types inherit from [QObject] or have the `#[base = T]` attribute.
222-
pub trait Upcast<T> {
222+
pub unsafe trait Upcast<T> {
223223
#[doc(hidden)]
224224
/// # Safety
225225
///
@@ -267,28 +267,6 @@ pub trait Upcast<T> {
267267
}
268268
}
269269

270-
impl<T> Upcast<T> for T {
271-
unsafe fn upcast_ptr(this: *const Self) -> *const Self {
272-
this
273-
}
274-
275-
unsafe fn from_base_ptr(base: *const T) -> *const Self {
276-
base
277-
}
278-
279-
fn upcast(&self) -> &Self {
280-
self
281-
}
282-
283-
fn upcast_mut(&mut self) -> &mut Self {
284-
self
285-
}
286-
287-
fn upcast_pin(self: Pin<&mut Self>) -> Pin<&mut Self> {
288-
self
289-
}
290-
}
291-
292270
/// This trait is automatically implemented by CXX-Qt and you most likely do not need to manually implement it.
293271
/// Trait for downcasting to a subclass, provided the subclass implements [Upcast] to this type.
294272
/// Returns `None` in cases where `Sub` isn't a child class of `Self`.

0 commit comments

Comments
 (0)