Skip to content

Commit a9c1b4e

Browse files
committed
cxx-qt-gen: impl Deref for qobject::T to reach T easily
Related to #559
1 parent 7254978 commit a9c1b4e

File tree

16 files changed

+112
-42
lines changed

16 files changed

+112
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Ensure that generated Rust code works when `#![deny(missing_docs)]` is enabled
2121
- Ability to connect and disconnect from signals in Rust triggering a function pointer
2222
- `unsafe impl !cxx_qt::Locking for qobject::T` to disable internal locking
23+
- `Deref` is now implemented for `qobject::T` to reach the `T` Rust struct
2324

2425
### Changed
2526

crates/cxx-qt-gen/src/generator/rust/property/getter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn generate(
4343
#[doc = "Getter for the Q_PROPERTY "]
4444
#[doc = #ident_str]
4545
pub fn #getter_rust(&self) -> &#ty {
46-
&self.rust().#ident
46+
&self.#ident
4747
}
4848
}
4949
},

crates/cxx-qt-gen/src/generator/rust/property/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ mod tests {
117117
#[doc = "Getter for the Q_PROPERTY "]
118118
#[doc = "trivial_property"]
119119
pub fn trivial_property(&self) -> &i32 {
120-
&self.rust().trivial_property
120+
&self.trivial_property
121121
}
122122
}
123123
},
@@ -151,7 +151,7 @@ mod tests {
151151
#[doc = "Setter for the Q_PROPERTY "]
152152
#[doc = "trivial_property"]
153153
pub fn set_trivial_property(mut self: Pin<&mut Self>, value: i32) {
154-
if self.rust().trivial_property == value {
154+
if self.trivial_property == value {
155155
return;
156156
}
157157
self.as_mut().rust_mut().trivial_property = value;
@@ -191,7 +191,7 @@ mod tests {
191191
#[doc = "Getter for the Q_PROPERTY "]
192192
#[doc = "opaque_property"]
193193
pub fn opaque_property(&self) -> &UniquePtr<QColor> {
194-
&self.rust().opaque_property
194+
&self.opaque_property
195195
}
196196
}
197197
},
@@ -225,7 +225,7 @@ mod tests {
225225
#[doc = "Setter for the Q_PROPERTY "]
226226
#[doc = "opaque_property"]
227227
pub fn set_opaque_property(mut self: Pin<&mut Self>, value: UniquePtr<QColor>) {
228-
if self.rust().opaque_property == value {
228+
if self.opaque_property == value {
229229
return;
230230
}
231231
self.as_mut().rust_mut().opaque_property = value;
@@ -265,7 +265,7 @@ mod tests {
265265
#[doc = "Getter for the Q_PROPERTY "]
266266
#[doc = "unsafe_property"]
267267
pub fn unsafe_property(&self) -> &*mut T {
268-
&self.rust().unsafe_property
268+
&self.unsafe_property
269269
}
270270
}
271271
},
@@ -299,7 +299,7 @@ mod tests {
299299
#[doc = "Setter for the Q_PROPERTY "]
300300
#[doc = "unsafe_property"]
301301
pub fn set_unsafe_property(mut self: Pin<&mut Self>, value: *mut T) {
302-
if self.rust().unsafe_property == value {
302+
if self.unsafe_property == value {
303303
return;
304304
}
305305
self.as_mut().rust_mut().unsafe_property = value;

crates/cxx-qt-gen/src/generator/rust/property/setter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn generate(
5151
#[doc = "Setter for the Q_PROPERTY "]
5252
#[doc = #ident_str]
5353
pub fn #setter_rust(mut self: Pin<&mut Self>, value: #ty) {
54-
if self.rust().#ident == value {
54+
if self.#ident == value {
5555
// don't want to set the value again and reemit the signal,
5656
// as this can cause binding loops
5757
return;

crates/cxx-qt-gen/src/writer/rust/mod.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,30 @@ fn cxx_qt_common_blocks(qobject: &GeneratedRustQObject) -> Vec<TokenStream> {
3535
let cpp_struct_ident = &qobject.cpp_struct_ident;
3636
let rust_struct_ident = &qobject.rust_struct_ident;
3737

38-
vec![quote! {
39-
impl cxx_qt::CxxQtType for #cpp_struct_ident {
40-
type Rust = #rust_struct_ident;
38+
vec![
39+
quote! {
40+
impl core::ops::Deref for #cpp_struct_ident {
41+
type Target = #rust_struct_ident;
4142

42-
fn rust(&self) -> &Self::Rust {
43-
self.cxx_qt_ffi_rust()
43+
fn deref(&self) -> &Self::Target {
44+
self.cxx_qt_ffi_rust()
45+
}
4446
}
47+
},
48+
quote! {
49+
impl cxx_qt::CxxQtType for #cpp_struct_ident {
50+
type Rust = #rust_struct_ident;
51+
52+
fn rust(&self) -> &Self::Rust {
53+
self.cxx_qt_ffi_rust()
54+
}
4555

46-
fn rust_mut(self: core::pin::Pin<&mut Self>) -> Pin<&mut Self::Rust> {
47-
self.cxx_qt_ffi_rust_mut()
56+
fn rust_mut(self: core::pin::Pin<&mut Self>) -> Pin<&mut Self::Rust> {
57+
self.cxx_qt_ffi_rust_mut()
58+
}
4859
}
49-
}
50-
}]
60+
},
61+
]
5162
}
5263

5364
/// For a given GeneratedRustBlocks write this into a Rust TokenStream
@@ -365,6 +376,14 @@ mod tests {
365376
}
366377
}
367378

379+
impl core::ops::Deref for MyObject {
380+
type Target = MyObjectRust;
381+
382+
fn deref(&self) -> &Self::Target {
383+
self.cxx_qt_ffi_rust()
384+
}
385+
}
386+
368387
impl cxx_qt::CxxQtType for MyObject {
369388
type Rust = MyObjectRust;
370389
fn rust(&self) -> &Self::Rust {
@@ -466,6 +485,14 @@ mod tests {
466485
}
467486
}
468487

488+
impl core::ops::Deref for FirstObject {
489+
type Target = FirstObjectRust;
490+
491+
fn deref(&self) -> &Self::Target {
492+
self.cxx_qt_ffi_rust()
493+
}
494+
}
495+
469496
impl cxx_qt::CxxQtType for FirstObject {
470497
type Rust = FirstObjectRust;
471498
fn rust(&self) -> &Self::Rust {
@@ -485,6 +512,14 @@ mod tests {
485512
}
486513
}
487514

515+
impl core::ops::Deref for SecondObject {
516+
type Target = SecondObjectRust;
517+
518+
fn deref(&self) -> &Self::Target {
519+
self.cxx_qt_ffi_rust()
520+
}
521+
}
522+
488523
impl cxx_qt::CxxQtType for SecondObject {
489524
type Rust = SecondObjectRust;
490525
fn rust(&self) -> &Self::Rust {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ pub mod cxx_qt_inheritance {
109109
pub fn create_rs_my_object_rust() -> std::boxed::Box<MyObjectRust> {
110110
core::default::Default::default()
111111
}
112+
impl core::ops::Deref for MyObject {
113+
type Target = MyObjectRust;
114+
fn deref(&self) -> &Self::Target {
115+
self.cxx_qt_ffi_rust()
116+
}
117+
}
112118
impl cxx_qt::CxxQtType for MyObject {
113119
type Rust = MyObjectRust;
114120
fn rust(&self) -> &Self::Rust {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ pub mod cxx_qt_ffi {
319319
initialize_arguments,
320320
)
321321
}
322+
impl core::ops::Deref for MyObject {
323+
type Target = MyObjectRust;
324+
fn deref(&self) -> &Self::Target {
325+
self.cxx_qt_ffi_rust()
326+
}
327+
}
322328
impl cxx_qt::CxxQtType for MyObject {
323329
type Rust = MyObjectRust;
324330
fn rust(&self) -> &Self::Rust {

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub mod cxx_qt_ffi {
226226
#[doc = "Getter for the Q_PROPERTY "]
227227
#[doc = "property_name"]
228228
pub fn property_name(&self) -> &i32 {
229-
&self.rust().property_name
229+
&self.property_name
230230
}
231231
}
232232
impl MyObjectRust {
@@ -239,7 +239,7 @@ pub mod cxx_qt_ffi {
239239
#[doc = "Setter for the Q_PROPERTY "]
240240
#[doc = "property_name"]
241241
pub fn set_property_name(mut self: Pin<&mut Self>, value: i32) {
242-
if self.rust().property_name == value {
242+
if self.property_name == value {
243243
return;
244244
}
245245
self.as_mut().rust_mut().property_name = value;
@@ -285,6 +285,12 @@ pub mod cxx_qt_ffi {
285285
pub fn create_rs_my_object_rust() -> std::boxed::Box<MyObjectRust> {
286286
core::default::Default::default()
287287
}
288+
impl core::ops::Deref for MyObject {
289+
type Target = MyObjectRust;
290+
fn deref(&self) -> &Self::Target {
291+
self.cxx_qt_ffi_rust()
292+
}
293+
}
288294
impl cxx_qt::CxxQtType for MyObject {
289295
type Rust = MyObjectRust;
290296
fn rust(&self) -> &Self::Rust {
@@ -305,7 +311,7 @@ pub mod cxx_qt_ffi {
305311
#[doc = "Getter for the Q_PROPERTY "]
306312
#[doc = "property_name"]
307313
pub fn property_name(&self) -> &i32 {
308-
&self.rust().property_name
314+
&self.property_name
309315
}
310316
}
311317
impl SecondObjectRust {
@@ -318,7 +324,7 @@ pub mod cxx_qt_ffi {
318324
#[doc = "Setter for the Q_PROPERTY "]
319325
#[doc = "property_name"]
320326
pub fn set_property_name(mut self: Pin<&mut Self>, value: i32) {
321-
if self.rust().property_name == value {
327+
if self.property_name == value {
322328
return;
323329
}
324330
self.as_mut().rust_mut().property_name = value;
@@ -363,6 +369,12 @@ pub mod cxx_qt_ffi {
363369
pub fn create_rs_second_object_rust() -> std::boxed::Box<SecondObjectRust> {
364370
core::default::Default::default()
365371
}
372+
impl core::ops::Deref for SecondObject {
373+
type Target = SecondObjectRust;
374+
fn deref(&self) -> &Self::Target {
375+
self.cxx_qt_ffi_rust()
376+
}
377+
}
366378
impl cxx_qt::CxxQtType for SecondObject {
367379
type Rust = SecondObjectRust;
368380
fn rust(&self) -> &Self::Rust {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub mod cxx_qt_ffi {
118118
#[doc = "Getter for the Q_PROPERTY "]
119119
#[doc = "primitive"]
120120
pub fn primitive(&self) -> &i32 {
121-
&self.rust().primitive
121+
&self.primitive
122122
}
123123
}
124124
impl MyObjectRust {
@@ -131,7 +131,7 @@ pub mod cxx_qt_ffi {
131131
#[doc = "Setter for the Q_PROPERTY "]
132132
#[doc = "primitive"]
133133
pub fn set_primitive(mut self: Pin<&mut Self>, value: i32) {
134-
if self.rust().primitive == value {
134+
if self.primitive == value {
135135
return;
136136
}
137137
self.as_mut().rust_mut().primitive = value;
@@ -148,7 +148,7 @@ pub mod cxx_qt_ffi {
148148
#[doc = "Getter for the Q_PROPERTY "]
149149
#[doc = "trivial"]
150150
pub fn trivial(&self) -> &QPoint {
151-
&self.rust().trivial
151+
&self.trivial
152152
}
153153
}
154154
impl MyObjectRust {
@@ -161,7 +161,7 @@ pub mod cxx_qt_ffi {
161161
#[doc = "Setter for the Q_PROPERTY "]
162162
#[doc = "trivial"]
163163
pub fn set_trivial(mut self: Pin<&mut Self>, value: QPoint) {
164-
if self.rust().trivial == value {
164+
if self.trivial == value {
165165
return;
166166
}
167167
self.as_mut().rust_mut().trivial = value;
@@ -201,6 +201,12 @@ pub mod cxx_qt_ffi {
201201
pub fn create_rs_my_object_rust() -> std::boxed::Box<MyObjectRust> {
202202
core::default::Default::default()
203203
}
204+
impl core::ops::Deref for MyObject {
205+
type Target = MyObjectRust;
206+
fn deref(&self) -> &Self::Target {
207+
self.cxx_qt_ffi_rust()
208+
}
209+
}
204210
impl cxx_qt::CxxQtType for MyObject {
205211
type Rust = MyObjectRust;
206212
fn rust(&self) -> &Self::Rust {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ pub mod cxx_qt_ffi {
199199
pub fn create_rs_my_object_rust() -> std::boxed::Box<MyObjectRust> {
200200
core::default::Default::default()
201201
}
202+
impl core::ops::Deref for MyObject {
203+
type Target = MyObjectRust;
204+
fn deref(&self) -> &Self::Target {
205+
self.cxx_qt_ffi_rust()
206+
}
207+
}
202208
impl cxx_qt::CxxQtType for MyObject {
203209
type Rust = MyObjectRust;
204210
fn rust(&self) -> &Self::Rust {

examples/demo_threading/rust/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ impl cxx_qt::Constructor<()> for qobject::EnergyUsage {
131131
EnergyUsageRust::default()
132132
}
133133

134-
/// A Q_INVOKABLE which starts the TCP server
135134
fn initialize(mut self: core::pin::Pin<&mut Self>, _arguments: Self::InitializeArguments) {
136-
if self.rust().join_handles.is_some() {
135+
if self.join_handles.is_some() {
137136
println!("Already running a server!");
138137
return;
139138
}

examples/qml_features/rust/src/custom_base_class.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ impl qobject::CustomBaseClass {
209209
}
210210

211211
fn add_cpp_context(mut self: Pin<&mut Self>) {
212-
let count = self.as_ref().rust().vector.len();
212+
let count = self.vector.len();
213213
unsafe {
214214
self.as_mut()
215215
.begin_insert_rows(&QModelIndex::default(), count as i32, count as i32);
216-
let id = self.as_ref().rust().id;
216+
let id = self.id;
217217
self.as_mut().rust_mut().id = id + 1;
218218
self.as_mut()
219219
.rust_mut()
@@ -255,7 +255,7 @@ impl qobject::CustomBaseClass {
255255

256256
/// Remove the row with the given index
257257
pub fn remove(mut self: Pin<&mut Self>, index: i32) {
258-
if index < 0 || (index as usize) >= self.as_ref().rust().vector.len() {
258+
if index < 0 || (index as usize) >= self.vector.len() {
259259
return;
260260
}
261261

@@ -278,7 +278,7 @@ impl qobject::CustomBaseClass {
278278
pub const VALUE_ROLE: i32 = 1;
279279

280280
fn data(&self, index: &QModelIndex, role: i32) -> QVariant {
281-
if let Some((id, value)) = self.rust().vector.get(index.row() as usize) {
281+
if let Some((id, value)) = self.vector.get(index.row() as usize) {
282282
return match role {
283283
Self::ID_ROLE => QVariant::from(id),
284284
Self::VALUE_ROLE => QVariant::from(value),
@@ -312,7 +312,7 @@ impl qobject::CustomBaseClass {
312312

313313
/// Return the row count for the QAbstractListModel
314314
pub fn row_count(&self, _parent: &QModelIndex) -> i32 {
315-
self.rust().vector.len() as i32
315+
self.vector.len() as i32
316316
}
317317
}
318318
// ANCHOR_END: book_macro_code

examples/qml_features/rust/src/invokables.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Default for RustInvokablesRust {
6262
impl qobject::RustInvokables {
6363
/// Immutable invokable method that returns the QColor
6464
fn load_color(&self) -> QColor {
65-
self.rust().as_qcolor()
65+
self.as_qcolor()
6666
}
6767

6868
/// Mutable invokable method that stores a color

examples/qml_features/rust/src/signals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl cxx_qt::Constructor<()> for qobject::RustSignals {
113113
// Determine if logging is enabled
114114
if *qobject.as_ref().logging_enabled() {
115115
// If no connections have been made, then create them
116-
if qobject.as_ref().rust().connections.is_none() {
116+
if qobject.as_ref().connections.is_none() {
117117
// ANCHOR: book_signals_connect
118118
let connections = [
119119
qobject.as_mut().on_connected(|_, url| {

tests/basic_cxx_qt/rust/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ impl qobject::MyObject {
122122
}
123123

124124
fn fetch_update_call_count(&self) -> i32 {
125-
self.rust().update_call_count
125+
self.update_call_count
126126
}
127127
}

0 commit comments

Comments
 (0)