Skip to content

Commit 5638649

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 5638649

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

glib-macros/tests/properties.rs

Lines changed: 3 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(get, set, default = "")]
150+
thread_guard: RefCell<ThreadGuard<String>>,
148151
}
149152

150153
impl ObjectImpl for Foo {

glib/src/param_spec.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,15 @@ pub trait HasParamSpec {
20652065
fn param_spec_builder() -> Self::BuilderFn;
20662066
}
20672067

2068+
impl<T: HasParamSpec> HasParamSpec for crate::thread_guard::ThreadGuard<T> {
2069+
type ParamSpec = T::ParamSpec;
2070+
type SetValue = T::SetValue;
2071+
type BuilderFn = T::BuilderFn;
2072+
2073+
fn param_spec_builder() -> Self::BuilderFn {
2074+
T::param_spec_builder()
2075+
}
2076+
}
20682077
impl<T: crate::value::ToValueOptional + HasParamSpec> HasParamSpec for Option<T> {
20692078
type ParamSpec = T::ParamSpec;
20702079
type SetValue = T::SetValue;

glib/src/thread_guard.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use std::{
44
mem, ptr,
55
sync::atomic::{AtomicUsize, Ordering},
66
};
7+
8+
use crate::value::{FromValue, ValueTypeChecker, ValueTypeMismatchOrNoneError};
9+
use crate::{StaticType, ToValue, Type, Value};
10+
711
fn next_thread_id() -> usize {
812
static COUNTER: AtomicUsize = AtomicUsize::new(0);
913
COUNTER.fetch_add(1, Ordering::SeqCst)
@@ -111,4 +115,39 @@ impl<T> Drop for ThreadGuard<T> {
111115
}
112116
}
113117

118+
impl<T: Default> Default for ThreadGuard<T> {
119+
fn default() -> Self {
120+
Self::new(T::default())
121+
}
122+
}
123+
114124
unsafe impl<T> Send for ThreadGuard<T> {}
125+
126+
impl<T: ToValue + StaticType> ToValue for ThreadGuard<T> {
127+
fn to_value(&self) -> Value {
128+
T::to_value(self.get_ref())
129+
}
130+
131+
fn value_type(&self) -> Type {
132+
T::static_type()
133+
}
134+
}
135+
136+
impl<T: From<Value> + ToValue> From<ThreadGuard<T>> for Value {
137+
fn from(guard: ThreadGuard<T>) -> Self {
138+
Self::from(guard.get_ref())
139+
}
140+
}
141+
142+
unsafe impl<'a, T, C, E> FromValue<'a> for ThreadGuard<T>
143+
where
144+
T: FromValue<'a, Checker = C> + StaticType,
145+
C: ValueTypeChecker<Error = ValueTypeMismatchOrNoneError<E>>,
146+
E: std::error::Error + Send + Sized + 'static,
147+
{
148+
type Checker = T::Checker;
149+
150+
unsafe fn from_value(value: &'a Value) -> Self {
151+
Self::new(T::from_value(value))
152+
}
153+
}

0 commit comments

Comments
 (0)