Skip to content

Commit f78cdaa

Browse files
committed
replace macro 'dynamic_object_subclass' by the macro helper attribute 'object_subclass_dynamic' for macro 'object_subclass'
Signed-off-by: fbrouille <[email protected]>
1 parent 7400c0b commit f78cdaa

File tree

8 files changed

+265
-256
lines changed

8 files changed

+265
-256
lines changed

glib-macros/src/lib.rs

Lines changed: 69 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -726,56 +726,91 @@ pub fn shared_boxed_derive(input: TokenStream) -> TokenStream {
726726
/// }
727727
/// ```
728728
///
729-
/// [`ObjectSubclass`]: ../glib/subclass/types/trait.ObjectSubclass.html
730-
#[proc_macro_attribute]
731-
#[proc_macro_error]
732-
pub fn object_subclass(_attr: TokenStream, item: TokenStream) -> TokenStream {
733-
use proc_macro_error::abort_call_site;
734-
match syn::parse::<syn::ItemImpl>(item) {
735-
Ok(input) => object_subclass_attribute::impl_object_subclass(&input).into(),
736-
Err(_) => abort_call_site!(object_subclass_attribute::WRONG_PLACE_MSG),
737-
}
738-
}
739-
740-
/// Macro for boilerplate of [`ObjectSubclass`] implementations that are
741-
/// registered as dynamic types.
729+
/// An object subclass can be registered as a dynamic type by setting the macro
730+
/// helper attribute `object_class_dynamic`:
731+
/// ```ignore
732+
/// #[derive(Default)]
733+
/// pub struct MyType;
742734
///
743-
/// An object subclass must be explicitly registered as a dynamic type when the
744-
/// system loads its implementation (see [`TypePlugin`] and [`TypeModule`].
735+
/// #[glib::object_subclass]
736+
/// #[object_subclass_dynamic]
737+
/// impl ObjectSubclass for MyType { ... }
738+
/// ```
739+
///
740+
/// As a dynamic type, an object subclass must be explicitly registered when
741+
/// the system loads the implementation (see [`TypePlugin`] and [`TypeModule`].
745742
/// Therefore, whereas an object subclass can be registered only once as a
746743
/// static type, it can be registered several times as a dynamic type.
747744
///
748745
/// An object subclass registered as a dynamic type is never unregistered. The
749-
/// system calls [`TypePluginExt::unuse`] to unload its implementation. If the
746+
/// system calls [`TypePluginExt::unuse`] to unload the implementation. If the
750747
/// [`TypePlugin`] subclass is a [`TypeModule`], the object subclass registered
751748
/// as a dynamic type is marked as unloaded and must be registered again when
752749
/// the module is reloaded.
753750
///
754-
/// This macro provides two behaviors when registering an object subclass as a
755-
/// dynamic type:
751+
/// The macro helper attribute `object_class_dynamic` provides two behaviors
752+
/// when registering an object subclass as a dynamic type:
756753
///
757-
/// By default an object subclass is registered as a dynamic type when the
758-
/// system loads its implementation (e.g. when the module is loaded):
754+
/// - lazy registration: by default an object subclass is registered as a
755+
/// dynamic type when the system loads the implementation (e.g. when the module
756+
/// is loaded). Optionally setting `lazy_registration` to `true` postpones
757+
/// registration on the first use (when `static_type()` is called for the first
758+
/// time):
759759
/// ```ignore
760-
/// #[glib::dynamic_object_subclass]
760+
/// #[derive(Default)]
761+
/// pub struct MyType;
762+
///
763+
/// #[glib::object_subclass]
764+
/// #[object_subclass_dynamic(lazy_registration = true)]
761765
/// impl ObjectSubclass for MyType { ... }
762766
/// ```
763767
///
764-
/// Optionally setting the macro attribute `lazy_registration` to `true`
765-
/// postpones registration on the first use (when `type_()` is called for the
766-
/// first time), similarly to the [`macro@object_subclass`] macro:
768+
/// - registration within [`TypeModule`] subclass or within [`TypePlugin`]
769+
/// subclass: an object subclass is usually registered as a dynamic type within
770+
/// a [`TypeModule`] subclass:
767771
/// ```ignore
768-
/// #[glib::dynamic_object_subclass(lazy_registration = true)]
769-
/// impl ObjectSubclass for MyType { ... }
772+
/// #[derive(Default)]
773+
/// pub struct MyModuleType;
774+
///
775+
/// #[glib::object_subclass]
776+
/// #[object_subclass_dynamic]
777+
/// impl ObjectSubclass for MyModuleType { ... }
778+
/// ...
779+
/// #[derive(Default)]
780+
/// pub struct MyModule;
781+
/// ...
782+
/// impl TypeModuleImpl for MyModule {
783+
/// fn load(&self) -> bool {
784+
/// // registers object subclasses as dynamic types.
785+
/// let my_module = self.obj();
786+
/// let type_module: &glib::TypeModule = my_module.upcast_ref();
787+
/// MyModuleType::on_implementation_load(type_module)
788+
/// }
789+
/// ...
790+
/// }
770791
/// ```
771792
///
772-
/// By default an object subclass is considered to be registered as a dynamic
773-
/// type within a [`TypeModule`] subclass. Optionally setting the macro
774-
/// attribute `plugin_type` allows to register an object subclass as a dynamic
775-
/// type within a given [`TypePlugin`] subclass:
793+
/// Optionally setting `plugin_type` allows to register an object subclass as a
794+
/// dynamic type within a [`TypePlugin`] subclass that is not a [`TypeModule`]:
776795
/// ```ignore
777-
/// #[glib::dynamic_object_subclass(plugin_type = MyPlugin)]
778-
/// impl ObjectSubclass for MyType { ... }
796+
/// #[derive(Default)]
797+
/// pub struct MyPluginType;
798+
///
799+
/// #[glib::object_subclass]
800+
/// #[object_subclass_dynamic(plugin_type = MyPlugin)]
801+
/// impl ObjectSubclass for MyPluginType { ... }
802+
/// ...
803+
/// #[derive(Default)]
804+
/// pub struct MyPlugin;
805+
/// ...
806+
/// impl TypePluginImpl for MyPlugin {
807+
/// fn use_plugin(&self) {
808+
/// // register object subclasses as dynamic types.
809+
/// let my_plugin = self.obj();
810+
/// MyPluginType::on_implementation_load(my_plugin.as_ref());
811+
/// }
812+
/// ...
813+
/// }
779814
/// ```
780815
///
781816
/// [`ObjectSubclass`]: ../glib/subclass/types/trait.ObjectSubclass.html
@@ -784,23 +819,10 @@ pub fn object_subclass(_attr: TokenStream, item: TokenStream) -> TokenStream {
784819
/// [`TypePluginExt::unuse`]: ../glib/gobject/type_plugin/trait.TypePluginExt.html#method.unuse
785820
#[proc_macro_attribute]
786821
#[proc_macro_error]
787-
pub fn dynamic_object_subclass(attr: TokenStream, item: TokenStream) -> TokenStream {
822+
pub fn object_subclass(_attr: TokenStream, item: TokenStream) -> TokenStream {
788823
use proc_macro_error::abort_call_site;
789-
let attrs = match syn::parse::Parser::parse(
790-
syn::punctuated::Punctuated::<syn::Expr, syn::Token![,]>::parse_terminated,
791-
attr,
792-
) {
793-
Ok(attrs)
794-
if attrs
795-
.iter()
796-
.all(|attr| matches!(attr, syn::Expr::Assign(_))) =>
797-
{
798-
attrs
799-
}
800-
_ => abort_call_site!(object_subclass_attribute::WRONG_EXPRESSION_MSG),
801-
};
802824
match syn::parse::<syn::ItemImpl>(item) {
803-
Ok(input) => object_subclass_attribute::impl_dynamic_object_subclass(&attrs, &input).into(),
825+
Ok(mut input) => object_subclass_attribute::impl_object_subclass(&mut input).into(),
804826
Err(_) => abort_call_site!(object_subclass_attribute::WRONG_PLACE_MSG),
805827
}
806828
}

0 commit comments

Comments
 (0)