diff --git a/glib-macros/tests/properties.rs b/glib-macros/tests/properties.rs index 3bb611c1c1cb..40c9368bf223 100644 --- a/glib-macros/tests/properties.rs +++ b/glib-macros/tests/properties.rs @@ -87,6 +87,7 @@ mod foo { } pub mod imp { + use glib::thread_guard::ThreadGuard; use glib::{ParamSpec, Value}; use std::rc::Rc; @@ -145,6 +146,8 @@ mod foo { cell: Cell, #[property(get = Self::overridden, override_class = Base)] overridden: PhantomData, + #[property(get, set)] + thread_guard: ThreadGuard>, } impl ObjectImpl for Foo { @@ -195,6 +198,12 @@ fn props() { let bar: String = myfoo.property("bar"); assert_eq!(bar, "".to_string()); + // Set the thread guard + myfoo.set_property("thread-guard", "foobar".to_value()); + // And grab directly the string from the guard after + let bar: String = myfoo.property("thread-guard"); + assert_eq!(bar, "foobar".to_string()); + // Set bar myfoo.set_property("bar", "epic".to_value()); let bar: String = myfoo.property("bar"); diff --git a/glib/src/property.rs b/glib/src/property.rs index 73a0471b8297..7a097a7c81f0 100644 --- a/glib/src/property.rs +++ b/glib/src/property.rs @@ -9,6 +9,7 @@ use std::sync::Arc; use std::sync::Mutex; use std::sync::RwLock; +use crate::thread_guard::ThreadGuard; use crate::HasParamSpec; // rustdoc-stripper-ignore-next @@ -37,6 +38,9 @@ impl Property for Mutex { impl Property for RwLock { type Value = T::Value; } +impl Property for ThreadGuard { + type Value = T::Value; +} impl Property for once_cell::sync::OnceCell { type Value = T::Value; } @@ -131,6 +135,19 @@ impl PropertySetNested for RwLock { } } +impl PropertyGet for ThreadGuard { + type Value = T::Value; + fn get R>(&self, f: F) -> R { + self.get_ref().get(f) + } +} +impl PropertySetNested for ThreadGuard { + type SetNestedValue = T::SetNestedValue; + fn set_nested(&self, f: F) { + self.get_ref().set_nested(f) + } +} + impl PropertyGet for once_cell::sync::OnceCell { type Value = T; fn get R>(&self, f: F) -> R { diff --git a/glib/src/thread_guard.rs b/glib/src/thread_guard.rs index f3b4c6175076..a813d50eaabe 100644 --- a/glib/src/thread_guard.rs +++ b/glib/src/thread_guard.rs @@ -4,6 +4,7 @@ use std::{ mem, ptr, sync::atomic::{AtomicUsize, Ordering}, }; + fn next_thread_id() -> usize { static COUNTER: AtomicUsize = AtomicUsize::new(0); COUNTER.fetch_add(1, Ordering::SeqCst) @@ -111,4 +112,10 @@ impl Drop for ThreadGuard { } } +impl Default for ThreadGuard { + fn default() -> Self { + Self::new(T::default()) + } +} + unsafe impl Send for ThreadGuard {}