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 {
87
87
}
88
88
89
89
pub mod imp {
90
+ use glib:: thread_guard:: ThreadGuard ;
90
91
use glib:: { ParamSpec , Value } ;
91
92
use std:: rc:: Rc ;
92
93
@@ -145,6 +146,8 @@ mod foo {
145
146
cell : Cell < u8 > ,
146
147
#[ property( get = Self :: overridden, override_class = Base ) ]
147
148
overridden : PhantomData < u32 > ,
149
+ #[ property( name = "thread-guard" , get, set) ]
150
+ thread_guard : ThreadGuard < Mutex < String > > ,
148
151
}
149
152
150
153
impl ObjectImpl for Foo {
@@ -195,6 +198,12 @@ fn props() {
195
198
let bar: String = myfoo. property ( "bar" ) ;
196
199
assert_eq ! ( bar, "" . to_string( ) ) ;
197
200
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
+
198
207
// Set bar
199
208
myfoo. set_property ( "bar" , "epic" . to_value ( ) ) ;
200
209
let bar: String = myfoo. property ( "bar" ) ;
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ use std::sync::Arc;
9
9
use std:: sync:: Mutex ;
10
10
use std:: sync:: RwLock ;
11
11
12
+ use crate :: thread_guard:: ThreadGuard ;
12
13
use crate :: HasParamSpec ;
13
14
14
15
// rustdoc-stripper-ignore-next
@@ -37,6 +38,9 @@ impl<T: Property> Property for Mutex<T> {
37
38
impl < T : Property > Property for RwLock < T > {
38
39
type Value = T :: Value ;
39
40
}
41
+ impl < T : Property > Property for ThreadGuard < T > {
42
+ type Value = T :: Value ;
43
+ }
40
44
impl < T : Property > Property for once_cell:: sync:: OnceCell < T > {
41
45
type Value = T :: Value ;
42
46
}
@@ -131,6 +135,19 @@ impl<T> PropertySetNested for RwLock<T> {
131
135
}
132
136
}
133
137
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
+
134
151
impl < T > PropertyGet for once_cell:: sync:: OnceCell < T > {
135
152
type Value = T ;
136
153
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::{
4
4
mem, ptr,
5
5
sync:: atomic:: { AtomicUsize , Ordering } ,
6
6
} ;
7
+
7
8
fn next_thread_id ( ) -> usize {
8
9
static COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
9
10
COUNTER . fetch_add ( 1 , Ordering :: SeqCst )
@@ -111,4 +112,10 @@ impl<T> Drop for ThreadGuard<T> {
111
112
}
112
113
}
113
114
115
+ impl < T : Default > Default for ThreadGuard < T > {
116
+ fn default ( ) -> Self {
117
+ Self :: new ( T :: default ( ) )
118
+ }
119
+ }
120
+
114
121
unsafe impl < T > Send for ThreadGuard < T > { }
You can’t perform that action at this time.
0 commit comments