-
-
Notifications
You must be signed in to change notification settings - Fork 115
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
Add glib::signals! macro #1577
Draft
PJungkamp
wants to merge
1
commit into
gtk-rs:main
Choose a base branch
from
PJungkamp:signals-macro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+525
−8
Draft
Add glib::signals! macro #1577
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,13 @@ | |
use glib::prelude::*; | ||
use glib::subclass::prelude::*; | ||
use glib::subclass::Signal; | ||
use glib::Properties; | ||
use glib::subclass::object::DerivedObjectSignals; | ||
use std::cell::RefCell; | ||
use std::sync::OnceLock; | ||
|
||
mod imp { | ||
use super::*; | ||
|
||
#[derive(Properties, Default)] | ||
#[derive(glib::Properties, Default)] | ||
#[properties(wrapper_type = super::Author)] | ||
pub struct Author { | ||
#[property(get, set)] | ||
|
@@ -20,11 +19,17 @@ mod imp { | |
surname: RefCell<String>, | ||
} | ||
|
||
#[glib::signals(wrapper_type = super::Author)] | ||
impl Author { | ||
|
||
#[signal] | ||
fn awarded(&self) {} | ||
} | ||
|
||
#[glib::derived_properties] | ||
impl ObjectImpl for Author { | ||
fn signals() -> &'static [Signal] { | ||
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new(); | ||
SIGNALS.get_or_init(|| vec![Signal::builder("awarded").build()]) | ||
Self::derived_signals() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to add a derived_signals macro as well, not much work and would make the migration to use the future signals macro more appealing |
||
} | ||
} | ||
|
||
|
@@ -38,6 +43,7 @@ mod imp { | |
glib::wrapper! { | ||
pub struct Author(ObjectSubclass<imp::Author>); | ||
} | ||
|
||
impl Author { | ||
pub fn new(name: &str, surname: &str) -> Self { | ||
glib::Object::builder() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wonder if it would be possible to make
and that means that the signal has no class handler, and
which means that it has a class handler with the body of the function.
In the first case, the macro would have to remove the whole function after expansion.
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.
I almost never use class handlers in my signals so I would appreciate something like that too.
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.
A function without a block isn't valid rust syntax outside of an
extern
block. I don't want to add some invalid Syntax that would be removed by the macro.It's also way more complicated to parse this. I would have to write a custom parser based on
syn::ItemImpl
that accepts these "invalid" functions. And it confuses other parsing code for other macros or rustfmt.I can add and parse a toplevel pseudo-macro, meaning something like this would be ok:
This pseudo-macro is valid Rust Syntax and should work with rustfmt and other macros.
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.
Maybe instead of using an
impl Object
, the usage could betrait ObjectSignals
making it possible to provide a default impl?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.
Default implementations can only use methods provided by the trait's required subtraits and itself.
The class handler implementations would have to reside in an
impl ObjectSignals for FooObject
block.I don't really see how I'd map a trait to a definition of signals, as I don't want the macro to modify the trait in ways that make it hard for a developer to understand what the trait will actually look like.
The problem I see is that a semicolon instead of a block in a trait definition means that you have to provide the class handler in your implementation of that trait, while you want it to mean that there is no class handler.
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.
The trait itself wouldn't be used. You can then
impl T for SomeObject
and forward the calls when the trait has an impl, no?Anyways, zbus is using something similar for it zbus::proxy! macro.
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.
There's an open issue around
#[zbus::proxy]
eating the trait definition.I don't want to introduce these weird macro-related edge cases into
glib
.What do you think about this?
I would make the macro produce something like:
A
#[glib::derived_signals]
macro could then be used to insert the__derived_signals
function call into theObjectImpl::signals
function.