Skip to content

Commit ca45935

Browse files
committed
ThreadGuard: Implement ToValue, Fromvalue and Default traits
This will also allow ThreadGuard to be used with the new property macro.
1 parent dcf23b2 commit ca45935

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

glib-macros/tests/properties.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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(name = "thread-guard", 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");

glib/src/property.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::sync::Arc;
99
use std::sync::Mutex;
1010
use std::sync::RwLock;
1111

12+
use crate::thread_guard::ThreadGuard;
1213
use crate::HasParamSpec;
1314

1415
// rustdoc-stripper-ignore-next
@@ -37,6 +38,9 @@ impl<T: Property> Property for Mutex<T> {
3738
impl<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+
}
4044
impl<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+
134151
impl<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 {

glib/src/thread_guard.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
mem, ptr,
55
sync::atomic::{AtomicUsize, Ordering},
66
};
7+
78
fn 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+
114121
unsafe impl<T> Send for ThreadGuard<T> {}

0 commit comments

Comments
 (0)