Skip to content

Commit 9ed7e49

Browse files
Add QtObject trait with basic as_ methods
1 parent 0926cf7 commit 9ed7e49

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

crates/cxx-qt/src/lib.rs

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

1414
#[doc(hidden)]
1515
pub mod casting;
@@ -125,6 +125,26 @@ pub use threading::{CxxQtThread, ThreadingQueueError};
125125
#[doc(hidden)]
126126
pub use static_assertions;
127127

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

0 commit comments

Comments
 (0)