Skip to content
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

Signal macro #1643

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glib-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0.96", features = ["full"] }
proc-macro-crate = "3.1"
bitflags.workspace = true

[lib]
proc-macro = true
Expand Down
53 changes: 53 additions & 0 deletions glib-macros/src/derived_signals_attribute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use proc_macro2::{Span, TokenStream};
use quote::quote;

pub const WRONG_PLACE_MSG: &str =
"This macro should be used on `impl` block for `glib::ObjectImpl` trait";

pub fn impl_derived_signals(input: &syn::ItemImpl) -> syn::Result<TokenStream> {
let syn::ItemImpl {
attrs,
generics,
trait_,
self_ty,
items,
..
} = input;

let trait_path = &trait_
.as_ref()
.ok_or_else(|| syn::Error::new(Span::call_site(), WRONG_PLACE_MSG))?
.1;

let mut has_signals = false;

for item in items {
if let syn::ImplItem::Fn(method) = item {
let ident = &method.sig.ident;

if ident == "signals" {
has_signals = true;
}
}
}

let glib = crate::utils::crate_ident_new();

let signals = quote!(
fn signals() -> &'static [#glib::subclass::signal::Signal] {
Self::derived_signals()
}
);

let generated = [(!has_signals).then_some(signals)];

Ok(quote!(
#(#attrs)*
impl #generics #trait_path for #self_ty {
#(#items)*
#(#generated)*
}
))
}
28 changes: 28 additions & 0 deletions glib-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ mod boxed_derive;
mod clone;
mod closure;
mod derived_properties_attribute;
mod derived_signals_attribute;
mod downgrade_derive;
mod enum_derive;
mod error_domain_derive;
mod flags_attribute;
mod object_impl_attributes;
mod properties;
mod shared_boxed_derive;
mod signals_attribute;
mod value_delegate_derive;
mod variant_derive;

Expand Down Expand Up @@ -1501,6 +1503,32 @@ pub fn derived_properties(_attr: TokenStream, item: TokenStream) -> TokenStream
.into()
}

/// This macro enables you to implement object signals in a quick way.
#[proc_macro_attribute]
pub fn signals(attr: TokenStream, item: TokenStream) -> TokenStream {
let attr_input = syn::parse_macro_input!(attr as signals_attribute::Args);

syn::parse::<syn::ItemImpl>(item)
.map_err(|_| syn::Error::new(Span::call_site(), signals_attribute::WRONG_PLACE_MSG))
.and_then(|item_input| signals_attribute::impl_signals(attr_input, item_input))
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}

#[proc_macro_attribute]
pub fn derived_signals(_attr: TokenStream, item: TokenStream) -> TokenStream {
syn::parse::<syn::ItemImpl>(item)
.map_err(|_| {
syn::Error::new(
Span::call_site(),
derived_signals_attribute::WRONG_PLACE_MSG,
)
})
.and_then(|input| derived_signals_attribute::impl_derived_signals(&input))
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}

/// # Example
/// ```
/// use glib::prelude::*;
Expand Down
Loading
Loading