File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ mod foo {
8787 }
8888
8989 pub mod imp {
90+ use glib:: thread_guard:: ThreadGuard ;
9091 use glib:: { ParamSpec , Value } ;
9192 use std:: rc:: Rc ;
9293
@@ -145,6 +146,8 @@ mod foo {
145146 cell : Cell < u8 > ,
146147 #[ property( get = Self :: overridden, override_class = Base ) ]
147148 overridden : PhantomData < u32 > ,
149+ #[ property( get, set) ]
150+ thread_guard : ThreadGuard < Mutex < String > > ,
148151 }
149152
150153 impl ObjectImpl for Foo {
@@ -195,6 +198,12 @@ fn props() {
195198 let bar: String = myfoo. property ( "bar" ) ;
196199 assert_eq ! ( bar, "" . to_string( ) ) ;
197200
201+ // Set the thread guard
202+ myfoo. set_property ( "thread-guard" , "foobar" . to_value ( ) ) ;
203+ // And grab directly the string from the guard after
204+ let bar: String = myfoo. property ( "thread-guard" ) ;
205+ assert_eq ! ( bar, "foobar" . to_string( ) ) ;
206+
198207 // Set bar
199208 myfoo. set_property ( "bar" , "epic" . to_value ( ) ) ;
200209 let bar: String = myfoo. property ( "bar" ) ;
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ use std::sync::Arc;
99use std:: sync:: Mutex ;
1010use std:: sync:: RwLock ;
1111
12+ use crate :: thread_guard:: ThreadGuard ;
1213use crate :: HasParamSpec ;
1314
1415// rustdoc-stripper-ignore-next
@@ -37,6 +38,9 @@ impl<T: Property> Property for Mutex<T> {
3738impl < T : Property > Property for RwLock < T > {
3839 type Value = T :: Value ;
3940}
41+ impl < T : Property > Property for ThreadGuard < T > {
42+ type Value = T :: Value ;
43+ }
4044impl < T : Property > Property for once_cell:: sync:: OnceCell < T > {
4145 type Value = T :: Value ;
4246}
@@ -131,6 +135,19 @@ impl<T> PropertySetNested for RwLock<T> {
131135 }
132136}
133137
138+ impl < T : PropertyGet > PropertyGet for ThreadGuard < T > {
139+ type Value = T :: Value ;
140+ fn get < R , F : Fn ( & Self :: Value ) -> R > ( & self , f : F ) -> R {
141+ self . get_ref ( ) . get ( f)
142+ }
143+ }
144+ impl < T : PropertySetNested > PropertySetNested for ThreadGuard < T > {
145+ type SetNestedValue = T :: SetNestedValue ;
146+ fn set_nested < F : FnOnce ( & mut Self :: SetNestedValue ) > ( & self , f : F ) {
147+ self . get_ref ( ) . set_nested ( f)
148+ }
149+ }
150+
134151impl < T > PropertyGet for once_cell:: sync:: OnceCell < T > {
135152 type Value = T ;
136153 fn get < R , F : Fn ( & Self :: Value ) -> R > ( & self , f : F ) -> R {
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ use std::{
44 mem, ptr,
55 sync:: atomic:: { AtomicUsize , Ordering } ,
66} ;
7+
78fn next_thread_id ( ) -> usize {
89 static COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
910 COUNTER . fetch_add ( 1 , Ordering :: SeqCst )
@@ -111,4 +112,10 @@ impl<T> Drop for ThreadGuard<T> {
111112 }
112113}
113114
115+ impl < T : Default > Default for ThreadGuard < T > {
116+ fn default ( ) -> Self {
117+ Self :: new ( T :: default ( ) )
118+ }
119+ }
120+
114121unsafe impl < T > Send for ThreadGuard < T > { }
You can’t perform that action at this time.
0 commit comments