@@ -726,56 +726,91 @@ pub fn shared_boxed_derive(input: TokenStream) -> TokenStream {
726
726
/// }
727
727
/// ```
728
728
///
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;
742
734
///
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`].
745
742
/// Therefore, whereas an object subclass can be registered only once as a
746
743
/// static type, it can be registered several times as a dynamic type.
747
744
///
748
745
/// 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
750
747
/// [`TypePlugin`] subclass is a [`TypeModule`], the object subclass registered
751
748
/// as a dynamic type is marked as unloaded and must be registered again when
752
749
/// the module is reloaded.
753
750
///
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:
756
753
///
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):
759
759
/// ```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)]
761
765
/// impl ObjectSubclass for MyType { ... }
762
766
/// ```
763
767
///
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 :
767
771
/// ```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
+ /// }
770
791
/// ```
771
792
///
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`]:
776
795
/// ```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
+ /// }
779
814
/// ```
780
815
///
781
816
/// [`ObjectSubclass`]: ../glib/subclass/types/trait.ObjectSubclass.html
@@ -784,23 +819,10 @@ pub fn object_subclass(_attr: TokenStream, item: TokenStream) -> TokenStream {
784
819
/// [`TypePluginExt::unuse`]: ../glib/gobject/type_plugin/trait.TypePluginExt.html#method.unuse
785
820
#[ proc_macro_attribute]
786
821
#[ proc_macro_error]
787
- pub fn dynamic_object_subclass ( attr : TokenStream , item : TokenStream ) -> TokenStream {
822
+ pub fn object_subclass ( _attr : TokenStream , item : TokenStream ) -> TokenStream {
788
823
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
- } ;
802
824
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 ( ) ,
804
826
Err ( _) => abort_call_site ! ( object_subclass_attribute:: WRONG_PLACE_MSG ) ,
805
827
}
806
828
}
0 commit comments