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

glib: Rework signal class handlers and accumulators #611

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
58 changes: 58 additions & 0 deletions glib-macros/src/class_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::utils::crate_ident_new;
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use quote::quote_spanned;
use syn::spanned::Spanned;

pub(crate) fn class_handler_inner(input: TokenStream, has_token: bool) -> TokenStream {
let crate_ident = crate_ident_new();
let closure = syn::parse_macro_input!(input as syn::ExprClosure);
let closure_ident = Ident::new("____closure", Span::mixed_site());
let token_ident = has_token.then(|| Ident::new("____token", Span::mixed_site()));
let values_ident = Ident::new("____values", Span::mixed_site());
let offset = if has_token { 1 } else { 0 };
let arg_names = closure
.inputs
.iter()
.skip(offset)
.enumerate()
.map(|(index, _)| Ident::new(&format!("____arg{}", index), Span::mixed_site()));
let arg_names = if let Some(token) = token_ident.as_ref().cloned() {
std::iter::once(token).chain(arg_names).collect::<Vec<_>>()
} else {
arg_names.collect::<Vec<_>>()
};
let arg_values = closure
.inputs
.iter()
.skip(offset)
.enumerate()
.map(|(index, pat)| {
let err_msg = format!("Wrong type for argument {}: {{:?}}", index);
let name = &arg_names[index + offset];
quote_spanned! { pat.span() =>
let #name = #crate_ident::Value::get(&#values_ident[#index])
.unwrap_or_else(|e| ::std::panic!(#err_msg, e));
}
});
let args_len = closure.inputs.len().saturating_sub(offset);
let token_arg = token_ident.map(|t| {
quote_spanned! { t.span() =>
#t: #crate_ident::subclass::SignalClassOverrideToken,
}
});
let output = quote_spanned! { closure.span() => {
let #closure_ident = #closure;
move
|#token_arg #values_ident: &[#crate_ident::Value]| -> ::std::option::Option<#crate_ident::Value> {
assert_eq!(#values_ident.len(), #args_len);
#(#arg_values)*
#crate_ident::closure::ToClosureReturnValue::to_closure_return_value(
&#closure_ident(#(#arg_names),*)
)
}
} };
output.into()
}
185 changes: 185 additions & 0 deletions glib-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.

mod boxed_derive;
mod class_handler;
mod clone;
mod closure;
mod downgrade_derive;
Expand Down Expand Up @@ -418,6 +419,190 @@ pub fn closure_local(item: TokenStream) -> TokenStream {
closure::closure_inner(item, "new_local")
}

/// Macro for creating signal class handlers.
///
/// This is only meant to be used with [`SignalBuilder::class_handler`] for automatic conversion of
/// its types to and from [`Value`]s. This macro takes a closure expression with any number of
/// arguments, and returns a closure that returns `Option<Value>` and takes a single `&[Value]`
/// argument.
///
/// Similar to [`closure!`], the returned function takes [`Value`] objects as inputs and output and
/// automatically converts the inputs to Rust types when invoking its callback, and then will
/// convert the output back to a `Value`. All inputs must implement the [`FromValue`] trait, and
/// outputs must either implement the [`ToValue`] trait or be the unit type `()`. Type-checking of
/// inputs is done at run-time; if the argument types do not match the type of the signal handler
/// then the closure will panic. Note that when passing input types derived from [`Object`] or
/// [`Interface`], you must take care to upcast to the exact object or interface type that is being
/// received.
///
/// [`SignalBuilder::class_handler`]: ../glib/subclass/signal/struct.SignalBuilder.html#method.class_handler
/// [`Value`]: ../glib/value/struct.Value.html
/// [`FromValue`]: ../glib/value/trait.FromValue.html
/// [`ToValue`]: ../glib/value/trait.ToValue.html
/// [`Interface`]: ../glib/object/struct.Interface.html
/// [`Object`]: ../glib/object/struct.Object.html
/// ```no_run
/// # use glib;
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
/// use glib::class_handler;
/// # use glib::once_cell;
/// #
/// # glib::wrapper! {
/// # pub struct MyObject(ObjectSubclass<imp::MyObject>);
/// # }
/// # mod imp {
/// # use super::*;
/// #
/// # #[derive(Default)]
/// # pub struct MyObject;
/// #
/// # #[glib::object_subclass]
/// # impl ObjectSubclass for MyObject {
/// # const NAME: &'static str = "MyObject";
/// # type Type = super::MyObject;
/// # }
///
/// impl ObjectImpl for MyObject {
/// fn signals() -> &'static [glib::subclass::Signal] {
/// use once_cell::sync::Lazy;
/// use glib::subclass::Signal;
/// static SIGNALS: Lazy<Vec<glib::subclass::Signal>> = Lazy::new(|| {
/// vec![
/// Signal::builder(
/// "my-signal",
/// &[u32::static_type().into()],
/// String::static_type().into(),
/// )
/// .class_handler(class_handler!(
/// |_this: &super::MyObject, num: u32| -> String {
/// format!("{}", num)
/// }))
/// .build(),
/// ]
/// });
/// SIGNALS.as_ref()
/// }
/// }
/// # }
/// # fn main() {}
/// ```
#[proc_macro]
#[proc_macro_error]
pub fn class_handler(item: TokenStream) -> TokenStream {
class_handler::class_handler_inner(item, false)
}

/// Macro for creating overridden signal class handlers.
///
/// This is only meant to be used with [`ObjectClassSubclassExt::override_signal_class_handler`]
/// for automatic conversion of its types to and from [`Value`]s. This macro takes a closure
/// expression with any number of arguments and returns a closure that returns `Option<Value>` and
/// takes two arguments: a [`SignalClassOverrideToken`] as the first and `&[Value]` as the second.
/// If any arguments are provided to the input closure, the `SignalClassOverrideToken` will always
/// be passed as the first argument.
///
/// Similar to [`closure!`], the returned function takes [`Value`] objects as inputs and output and
/// automatically converts the inputs to Rust types when invoking its callback, and then will
/// convert the output back to a `Value`. All inputs must implement the [`FromValue`] trait, and
/// outputs must either implement the [`ToValue`] trait or be the unit type `()`. Type-checking of
/// inputs is done at run-time; if the argument types do not match the type of the signal handler
/// then the closure will panic. Note that when passing input types derived from [`Object`] or
/// [`Interface`], you must take care to upcast to the exact object or interface type that is being
/// received.
///
/// [`ObjectClassSubclassExt::override_signal_class_handler`]: ../glib/subclass/object/trait.ObjectClassSubclassExt.html#method.override_signal_class_handler
/// [`SignalClassOverrideToken`]: ../glib/subclass/object/struct.SignalClassOverrideToken.html
/// [`Value`]: ../glib/value/struct.Value.html
/// [`FromValue`]: ../glib/value/trait.FromValue.html
/// [`ToValue`]: ../glib/value/trait.ToValue.html
/// [`Interface`]: ../glib/object/struct.Interface.html
/// [`Object`]: ../glib/object/struct.Object.html
/// ```no_run
/// # use glib;
/// use glib::prelude::*;
/// use glib::subclass::prelude::*;
/// use glib::subclass::SignalClassOverrideToken;
/// use glib::{class_handler, override_handler};
/// # use glib::once_cell;
/// #
/// # glib::wrapper! {
/// # pub struct MyBase(ObjectSubclass<imp::MyBase>);
/// # }
/// # unsafe impl<T: imp::MyBaseImpl> IsSubclassable<T> for MyBase {}
/// # glib::wrapper! {
/// # pub struct MyDerived(ObjectSubclass<imp::MyDerived>) @extends MyBase;
/// # }
/// # mod imp {
/// # use super::*;
/// #
/// # #[derive(Default)]
/// # pub struct MyBase;
/// #
/// # #[glib::object_subclass]
/// # impl ObjectSubclass for MyBase {
/// # const NAME: &'static str = "MyBase";
/// # type Type = super::MyBase;
/// # }
///
/// impl ObjectImpl for MyBase {
/// fn signals() -> &'static [glib::subclass::Signal] {
/// use once_cell::sync::Lazy;
/// use glib::subclass::Signal;
/// static SIGNALS: Lazy<Vec<glib::subclass::Signal>> = Lazy::new(|| {
/// vec![
/// Signal::builder(
/// "my-signal",
/// &[String::static_type().into(), u32::static_type().into()],
/// String::static_type().into(),
/// )
/// .class_handler(class_handler!(
/// |this: &super::MyBase, myarg1: Option<&str>, myarg2: u32| -> String {
/// format!("len: {}", myarg1.unwrap_or_default().len() + myarg2 as usize)
/// }))
/// .build(),
/// ]
/// });
/// SIGNALS.as_ref()
/// }
/// }
/// #
/// # pub trait MyBaseImpl: ObjectImpl {}
/// #
/// # #[derive(Default)]
/// # pub struct MyDerived;
///
/// #[glib::object_subclass]
/// impl ObjectSubclass for MyDerived {
/// const NAME: &'static str = "MyDerived";
/// type Type = super::MyDerived;
/// type ParentType = super::MyBase;
/// fn class_init(class: &mut Self::Class) {
/// class.override_signal_class_handler(
/// "my-signal",
/// override_handler!(|token: SignalClassOverrideToken,
/// this: &super::MyDerived,
/// myarg1: Option<&str>,
/// myarg2: u32|
/// -> String {
/// let s = format!("str: {} num: {}", myarg1.unwrap_or("nothing"), myarg2);
/// let ret: String = token.chain(&[&this, &Some(&s), &myarg2]);
/// format!("ret: {}", ret)
/// })
/// );
/// }
/// }
/// # impl ObjectImpl for MyDerived {}
/// # impl MyBaseImpl for MyDerived {}
/// # }
/// # fn main() {}
/// ```
#[proc_macro]
#[proc_macro_error]
pub fn override_handler(item: TokenStream) -> TokenStream {
class_handler::class_handler_inner(item, true)
}

/// Derive macro for register a rust enum in the glib type system and derive the
/// the [`glib::Value`] traits.
///
Expand Down
4 changes: 2 additions & 2 deletions glib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub use bitflags;
pub use once_cell;

pub use glib_macros::{
clone, closure, closure_local, flags, object_interface, object_subclass, Boxed, Downgrade,
Enum, ErrorDomain, SharedBoxed, Variant,
class_handler, clone, closure, closure_local, flags, object_interface, object_subclass,
override_handler, Boxed, Downgrade, Enum, ErrorDomain, SharedBoxed, Variant,
};

#[doc(hidden)]
Expand Down
5 changes: 2 additions & 3 deletions glib/src/subclass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ pub mod prelude {

pub use self::boxed::register_boxed_type;
pub use self::interface::register_interface;
pub use self::signal::{
Signal, SignalClassHandlerToken, SignalId, SignalInvocationHint, SignalQuery, SignalType,
};
pub use self::object::SignalClassOverrideToken;
pub use self::signal::{Signal, SignalId, SignalInvocationHint, SignalQuery, SignalType};
pub use self::types::{register_type, InitializingObject, InitializingType, TypeData};
Loading