@@ -20,51 +20,58 @@ use std;
20
20
use std:: os:: raw:: c_void;
21
21
22
22
cpp ! { {
23
- #include <QtCore /QObject >
24
- #include "qmetaobject_rust.hpp"
23
+ #include <QtCore /QObject >
24
+ #include "qmetaobject_rust.hpp"
25
25
26
- // Access private function of QObject. Pretend to define QObjectPrivate.
27
- // This rely on QObjectPrivate being a friend of QObject.
28
- class QObjectPrivate {
26
+ // Access private function of QObject. Pretend to define QObjectPrivate.
27
+ // This rely on QObjectPrivate being a friend of QObject.
28
+ class QObjectPrivate {
29
29
public:
30
- static QMetaObject :: Connection rust_connectImpl( const QObject * sender, void * * signal,
31
- const QObject * receiver, void * * slotPtr,
32
- QtPrivate :: QSlotObjectBase * slot, Qt :: ConnectionType type ,
33
- const int * types, const QMetaObject * senderMetaObject) {
34
- return QObject :: connectImpl( sender, signal, receiver, slotPtr, slot, type , types, senderMetaObject) ;
35
- }
36
- } ;
37
-
38
- class RustSlotOject : public QtPrivate :: QSlotObjectBase
39
- {
40
- TraitObject slot;
41
- static void impl ( int which, QSlotObjectBase * this_, QObject * r, void * * a, bool * ret) {
42
- switch ( which) {
43
- case Destroy :
44
- delete static_cast<RustSlotOject * >( this_) ;
45
- break ;
46
- case Call : {
47
- auto slot = static_cast<RustSlotOject * >( this_) ->slot;
48
- rust!( RustSlotObject_call [ slot : * mut dyn FnMut ( * const * const c_void) as "TraitObject" ,
49
- a : * const * const c_void as "void**" ] {
50
- unsafe { ( * slot) ( a) ; }
51
- } ) ;
52
- break ;
30
+ static QMetaObject :: Connection rust_connectImpl(
31
+ const QObject * sender,
32
+ void * * signal,
33
+ const QObject * receiver,
34
+ void * * slotPtr,
35
+ QtPrivate :: QSlotObjectBase * slot,
36
+ Qt :: ConnectionType type ,
37
+ const int * types,
38
+ const QMetaObject * senderMetaObject
39
+ ) {
40
+ return QObject :: connectImpl( sender, signal, receiver, slotPtr, slot,
41
+ type , types, senderMetaObject) ;
53
42
}
54
- case Compare : // not implemented
55
- case NumOperations :
56
- Q_UNUSED ( ret) ; Q_UNUSED ( r) ;
43
+ } ;
44
+
45
+ class RustSlotOject : public QtPrivate :: QSlotObjectBase
46
+ {
47
+ TraitObject slot;
48
+ static void impl ( int which, QSlotObjectBase * this_, QObject * r, void * * a, bool * ret) {
49
+ switch ( which) {
50
+ case Destroy :
51
+ delete static_cast<RustSlotOject * >( this_) ;
52
+ break ;
53
+ case Call : {
54
+ auto slot = static_cast<RustSlotOject * >( this_) ->slot;
55
+ rust!( RustSlotObject_call [ slot : * mut dyn FnMut ( * const * const c_void) as "TraitObject" ,
56
+ a : * const * const c_void as "void**" ] {
57
+ unsafe { ( * slot) ( a) ; }
58
+ } ) ;
59
+ break ;
60
+ }
61
+ case Compare : // not implemented
62
+ case NumOperations :
63
+ Q_UNUSED ( ret) ; Q_UNUSED ( r) ;
64
+ }
57
65
}
58
- }
59
- public:
60
- explicit RustSlotOject ( TraitObject slot) : QSlotObjectBase ( & impl ) , slot( slot) { }
61
- ~RustSlotOject ( ) {
62
- rust!( RustSlotOject_destruct [ slot : * mut dyn FnMut ( * const * const c_void) as "TraitObject" ] {
63
- let _ = unsafe { Box :: from_raw( slot) } ;
64
- } ) ;
65
- }
66
- Q_DISABLE_COPY ( RustSlotOject ) ;
67
- } ;
66
+ public:
67
+ explicit RustSlotOject ( TraitObject slot) : QSlotObjectBase ( & impl ) , slot( slot) { }
68
+ ~RustSlotOject ( ) {
69
+ rust!( RustSlotOject_destruct [ slot : * mut dyn FnMut ( * const * const c_void) as "TraitObject" ] {
70
+ let _ = unsafe { Box :: from_raw( slot) } ;
71
+ } ) ;
72
+ }
73
+ Q_DISABLE_COPY ( RustSlotOject ) ;
74
+ } ;
68
75
69
76
70
77
} }
@@ -75,28 +82,31 @@ cpp_class!(
75
82
/// Can be used to detect if a connection is valid, and to disconnect a connection.
76
83
pub unsafe struct ConnectionHandle as "QMetaObject::Connection"
77
84
) ;
85
+
78
86
impl ConnectionHandle {
79
87
/// Disconnect this connection.
80
88
///
81
89
/// After this function is called, all ConnectionHandle referring to this connection will be invalided.
82
90
/// Calling disconnect on an invalided connection does nothing.
83
91
pub fn disconnect ( & mut self ) {
84
- unsafe { cpp ! ( [ self as "const QMetaObject::Connection*" ] { QObject :: disconnect( * self ) ; } ) }
92
+ cpp ! ( unsafe [ self as "const QMetaObject::Connection *" ] {
93
+ QObject :: disconnect( * self ) ;
94
+ } )
85
95
}
86
96
87
97
/// Returns true if the connection is valid.
88
98
pub fn is_valid ( & self ) -> bool {
89
- unsafe {
90
- cpp ! ( [ self as "const QMetaObject::Connection*" ] -> bool as "bool" { return * self ; } )
91
- }
99
+ cpp ! ( unsafe [ self as "const QMetaObject::Connection *" ] -> bool as "bool" {
100
+ return * self ; // implicit conversion
101
+ } )
92
102
}
93
103
}
94
104
95
105
cpp_class ! (
96
106
/// Internal class that can be used to construct C++ signal. Should only be used as an implementation
97
- /// details when writing bindings to Qt signals to construct a `CppSignal<...>`
107
+ /// details when writing bindings to Qt signals to construct a `CppSignal<...>`.
98
108
///
99
- /// It has the same size as a `void(QObject::*)()` and can be constructed from signals.
109
+ /// It has the same size as a `void (QObject::*)()` and can be constructed from signals.
100
110
pub unsafe struct SignalCppRepresentation as "SignalCppRepresentation"
101
111
) ;
102
112
@@ -108,16 +118,19 @@ pub struct CppSignal<Args> {
108
118
inner : SignalCppRepresentation ,
109
119
phantom : std:: marker:: PhantomData < Args > ,
110
120
}
121
+
111
122
impl < Args > CppSignal < Args > {
112
123
pub unsafe fn new ( inner : SignalCppRepresentation ) -> Self {
113
124
CppSignal { inner, phantom : Default :: default ( ) }
114
125
}
115
126
}
127
+
116
128
impl < Args > Clone for CppSignal < Args > {
117
129
fn clone ( & self ) -> Self {
118
130
* self
119
131
}
120
132
}
133
+
121
134
impl < Args > Copy for CppSignal < Args > { }
122
135
123
136
/// Types of signals constructed with the qt_signal! macro.
@@ -129,11 +142,16 @@ pub struct RustSignal<Args> {
129
142
phantom : std:: marker:: PhantomData < Args > ,
130
143
_u : bool , // Actually put a field so it has a size;
131
144
}
145
+
132
146
impl < Args > Default for RustSignal < Args > {
133
147
fn default ( ) -> Self {
134
- RustSignal { phantom : Default :: default ( ) , _u : false }
148
+ RustSignal {
149
+ phantom : Default :: default ( ) ,
150
+ _u : Default :: default ( )
151
+ }
135
152
}
136
153
}
154
+
137
155
impl < Args > RustSignal < Args > {
138
156
/// return a CppSignal corresponding to this signal.
139
157
///
@@ -225,11 +243,20 @@ pub unsafe fn connect<Args, F: Slot<Args>>(
225
243
let apply_closure = move |a : * const * const c_void | slot. apply ( a) ;
226
244
let b: Box < dyn FnMut ( * const * const c_void ) > = Box :: new ( apply_closure) ;
227
245
let slot_raw = Box :: into_raw ( b) ;
228
- /*unsafe*/
229
- {
230
- cpp ! ( [ sender as "const QObject*" , mut cpp_signal as "SignalCppRepresentation" , slot_raw as "TraitObject" ] -> ConnectionHandle as "QMetaObject::Connection" {
231
- return QObjectPrivate :: rust_connectImpl( sender, reinterpret_cast<void * * >( & cpp_signal) , sender, nullptr,
232
- new RustSlotOject ( slot_raw) , Qt :: DirectConnection , nullptr, sender->metaObject( ) ) ;
233
- } )
234
- }
246
+ cpp ! ( unsafe [
247
+ sender as "const QObject *" ,
248
+ mut cpp_signal as "SignalCppRepresentation" ,
249
+ slot_raw as "TraitObject"
250
+ ] -> ConnectionHandle as "QMetaObject::Connection" {
251
+ return QObjectPrivate :: rust_connectImpl(
252
+ sender,
253
+ reinterpret_cast<void * * >( & cpp_signal) ,
254
+ sender,
255
+ nullptr,
256
+ new RustSlotOject ( slot_raw) ,
257
+ Qt :: DirectConnection ,
258
+ nullptr,
259
+ sender->metaObject( )
260
+ ) ;
261
+ } )
235
262
}
0 commit comments