Skip to content

ThreadGuard: Implement ToValue, Fromvalue and Default traits #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions glib-macros/tests/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mod foo {
}

pub mod imp {
use glib::thread_guard::ThreadGuard;
use glib::{ParamSpec, Value};
use std::rc::Rc;

Expand Down Expand Up @@ -145,6 +146,8 @@ mod foo {
cell: Cell<u8>,
#[property(get = Self::overridden, override_class = Base)]
overridden: PhantomData<u32>,
#[property(get, set)]
thread_guard: ThreadGuard<Mutex<String>>,
}

impl ObjectImpl for Foo {
Expand Down Expand Up @@ -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");
Expand Down
17 changes: 17 additions & 0 deletions glib/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,6 +38,9 @@ impl<T: Property> Property for Mutex<T> {
impl<T: Property> Property for RwLock<T> {
type Value = T::Value;
}
impl<T: Property> Property for ThreadGuard<T> {
type Value = T::Value;
}
impl<T: Property> Property for once_cell::sync::OnceCell<T> {
type Value = T::Value;
}
Expand Down Expand Up @@ -131,6 +135,19 @@ impl<T> PropertySetNested for RwLock<T> {
}
}

impl<T: PropertyGet> PropertyGet for ThreadGuard<T> {
type Value = T::Value;
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
self.get_ref().get(f)
}
}
impl<T: PropertySetNested> PropertySetNested for ThreadGuard<T> {
type SetNestedValue = T::SetNestedValue;
fn set_nested<F: FnOnce(&mut Self::SetNestedValue)>(&self, f: F) {
self.get_ref().set_nested(f)
}
}

impl<T> PropertyGet for once_cell::sync::OnceCell<T> {
type Value = T;
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
Expand Down
7 changes: 7 additions & 0 deletions glib/src/thread_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -111,4 +112,10 @@ impl<T> Drop for ThreadGuard<T> {
}
}

impl<T: Default> Default for ThreadGuard<T> {
fn default() -> Self {
Self::new(T::default())
}
}

unsafe impl<T> Send for ThreadGuard<T> {}