-
Notifications
You must be signed in to change notification settings - Fork 82
Add AsQobject trait #1262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add AsQobject trait #1262
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1262 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 73 73
Lines 12634 12634
=========================================
Hits 12634 12634 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
pub trait AsObject { | ||
/// Cast self reference into a [QObject] reference | ||
fn as_qobject(&self) -> &QObject; | ||
|
||
/// Cast mutable reference into a mutable [QObject] reference | ||
fn as_qobject_mut(&mut self) -> &mut QObject; | ||
|
||
/// Cast pinned mutable reference into a pinned mutable [QObject] reference | ||
fn as_qobject_pin(self: Pin<&mut Self>) -> Pin<&mut QObject>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be named AsQObject
? Since there should never be a plain &mut
to a QObject
(they should always be pinned), I think it might make more sense to have something like this:
pub trait AsQObject {
/// Cast self reference into a [QObject] reference
fn as_qobject(&self) -> &QObject;
/// Cast pinned mutable reference into a pinned mutable [QObject] reference
fn as_qobject_mut(self: Pin<&mut Self>) -> Pin<&mut QObject>;
}
I think it's good practice to use "mut" in function names wherever mutable borrowing/casting is involved. myobj.as_qobject_mut().do_something()
immediately reads to me as "this is using a mutable QObject method". myobj.as_qobject_pin().do_something()
seems less clear. Pin<&mut QObject>
is the only possible mutable reference to a QObject
, so it makes sense.
A trait which will wrap the QObjectExt methods for types which upcast to QObject. When used in conjunction with #1252 and #1226, this will allow any type that inherits from QObject - even if this is through many levels of inheritance - to have an
as_qobject
method, and if the trait is imported, expose the methods of QObject directly on that child class. Closes #562.