Skip to content

Commit 913c91b

Browse files
Add QtObject trait with basic as_ methods
1 parent f45893f commit 913c91b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

crates/cxx-qt/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//!
1010
//! See the [book](https://kdab.github.io/cxx-qt/book/) for more information.
1111
12+
use std::pin::Pin;
1213
use std::{fs::File, io::Write, path::Path};
1314

1415
#[doc(hidden)]
@@ -125,6 +126,27 @@ pub use threading::{CxxQtThread, ThreadingQueueError};
125126
#[doc(hidden)]
126127
pub use static_assertions;
127128

129+
/// This trait provides wrappers for objects which are QObjects or can be turned into a [QObject].
130+
/// It is automatically implemented for any types which implement [Upcast] to a [QObject].
131+
pub trait AsObject {
132+
/// Cast self reference into a [QObject] reference
133+
fn as_qobject(&self) -> &QObject;
134+
135+
/// Cast pinned mutable reference into a pinned mutable [QObject] reference
136+
fn as_qobject_mut(self: Pin<&mut Self>) -> Pin<&mut QObject>;
137+
}
138+
139+
impl<T: casting::Upcast<QObject>> AsObject for T {
140+
fn as_qobject(&self) -> &QObject {
141+
self.upcast()
142+
}
143+
144+
145+
fn as_qobject_mut(self: Pin<&mut Self>) -> Pin<&mut QObject> {
146+
self.upcast_pin()
147+
}
148+
}
149+
128150
/// This trait is automatically implemented for all QObject types generated by CXX-Qt.
129151
/// It provides information about the inner Rust struct that is wrapped by the QObject, as well as the methods
130152
/// that Cxx-Qt will generate for the QObject.

0 commit comments

Comments
 (0)