@@ -9,6 +9,7 @@ mod error_domain_derive;
9
9
mod flags_attribute;
10
10
mod object_interface_attribute;
11
11
mod object_subclass_attribute;
12
+ mod properties;
12
13
mod shared_boxed_derive;
13
14
mod variant_derive;
14
15
@@ -819,7 +820,6 @@ pub fn variant_derive(input: TokenStream) -> TokenStream {
819
820
let input = parse_macro_input ! ( input as DeriveInput ) ;
820
821
variant_derive:: impl_variant ( input)
821
822
}
822
-
823
823
#[ proc_macro]
824
824
pub fn cstr_bytes ( item : TokenStream ) -> TokenStream {
825
825
syn:: parse:: Parser :: parse2 (
@@ -836,3 +836,92 @@ pub fn cstr_bytes(item: TokenStream) -> TokenStream {
836
836
)
837
837
. unwrap_or_else ( |e| e. into_compile_error ( ) . into ( ) )
838
838
}
839
+
840
+ /// # Example
841
+ /// ```
842
+ /// use std::cell::RefCell;
843
+ /// use std::marker::PhantomData;
844
+ /// use std::sync::Mutex;
845
+ /// use glib::prelude::*;
846
+ /// use glib::subclass::prelude::*;
847
+ /// use glib_macros::Properties;
848
+ ///
849
+ /// #[derive(Default, Clone)]
850
+ /// struct Author {
851
+ /// name: String,
852
+ /// nick: String,
853
+ /// }
854
+ ///
855
+ /// pub mod imp {
856
+ /// use glib::{ParamSpec, Value};
857
+ /// use std::rc::Rc;
858
+ ///
859
+ /// use super::*;
860
+ ///
861
+ /// #[derive(Properties, Default)]
862
+ /// #[properties(wrapper_type = super::Foo)]
863
+ /// pub struct Foo {
864
+ /// #[property(get, set = Self::set_fizz)]
865
+ /// fizz: RefCell<String>,
866
+ /// #[property(name = "author-name", get, set, type = String, member = name)]
867
+ /// #[property(name = "author-nick", get, set, type = String, member = nick)]
868
+ /// author: RefCell<Author>,
869
+ /// #[property(get, set, user_1, user_2, lax_validation)]
870
+ /// custom_flags: RefCell<String>,
871
+ /// #[property(get, set, builder().minimum(0).maximum(5))]
872
+ /// numeric_builder: RefCell<u32>,
873
+ /// #[property(get, set, builder('c'))]
874
+ /// builder_with_required_param: RefCell<char>,
875
+ /// #[property(get, set)]
876
+ /// optional: RefCell<Option<String>>,
877
+ /// #[property(get, set)]
878
+ /// smart_pointer: Rc<RefCell<String>>,
879
+ /// }
880
+ ///
881
+ /// impl ObjectImpl for Foo {
882
+ /// fn properties() -> &'static [ParamSpec] {
883
+ /// Self::derived_properties()
884
+ /// }
885
+ /// fn set_property(
886
+ /// &self,
887
+ /// _id: usize,
888
+ /// _value: &Value,
889
+ /// _pspec: &ParamSpec,
890
+ /// ) {
891
+ /// Self::derived_set_property(self, _id, _value, _pspec).unwrap()
892
+ /// }
893
+ /// fn property(&self, _id: usize, _pspec: &ParamSpec) -> Value {
894
+ /// Self::derived_property(self, _id, _pspec).unwrap()
895
+ /// }
896
+ /// }
897
+ ///
898
+ /// #[glib::object_subclass]
899
+ /// impl ObjectSubclass for Foo {
900
+ /// const NAME: &'static str = "MyFoo";
901
+ /// type Type = super::Foo;
902
+ /// }
903
+ ///
904
+ /// impl Foo {
905
+ /// fn set_fizz(&self, value: String) {
906
+ /// *self.fizz.borrow_mut() = format!("custom set: {}", value);
907
+ /// }
908
+ /// }
909
+ /// }
910
+ ///
911
+ /// glib::wrapper! {
912
+ /// pub struct Foo(ObjectSubclass<imp::Foo>);
913
+ /// }
914
+ ///
915
+ /// fn main() {
916
+ /// let myfoo: Foo = glib::object::Object::new(&[]);
917
+ ///
918
+ /// myfoo.set_fizz("test value");
919
+ /// assert_eq!(myfoo.fizz(), "custom set: test value".to_string());
920
+ /// }
921
+ /// ```
922
+ #[ allow( clippy:: needless_doctest_main) ]
923
+ #[ proc_macro_derive( Properties , attributes( properties, property) ) ]
924
+ pub fn derive_props ( input : TokenStream ) -> TokenStream {
925
+ let input = parse_macro_input ! ( input as properties:: PropsMacroInput ) ;
926
+ properties:: impl_derive_props ( input)
927
+ }
0 commit comments