Skip to content
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

Partially nested types in properties: newtypes, ThreadGuard #984

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 24 additions & 6 deletions glib-macros/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,24 @@ fn expand_property_fn(props: &[PropDesc]) -> TokenStream2 {
),
(None, MaybeCustomFn::Default) => quote!(
DerivedPropertiesEnum::#enum_ident =>
#crate_ident::PropertyGet::get(&self.#field_ident, |v| ::std::convert::From::from(v))
#crate_ident::PropertyGet::get(
&self.#field_ident,
|v| #crate_ident::PropertyGet::get(
v,
|v| ::std::convert::From::from(v)
)
)

),
(Some(member), MaybeCustomFn::Default) => quote!(
DerivedPropertiesEnum::#enum_ident =>
#crate_ident::PropertyGet::get(&self.#field_ident, |v| ::std::convert::From::from(&v.#member))

#crate_ident::PropertyGet::get(
&self.#field_ident,
|v| #crate_ident::PropertyGet::get(
&v.#member,
|v| ::std::convert::From::from(v)
)
)
),
};
quote_spanned!(span=> #body)
Expand Down Expand Up @@ -415,6 +426,7 @@ fn expand_set_property_fn(props: &[PropDesc]) -> TokenStream2 {
field_ident,
member,
set,
ty,
..
} = p;

Expand All @@ -426,22 +438,28 @@ fn expand_set_property_fn(props: &[PropDesc]) -> TokenStream2 {
let body = match (member, set) {
(_, MaybeCustomFn::Custom(expr)) => quote!(
DerivedPropertiesEnum::#enum_ident => {
(#expr)(&self, #crate_ident::Value::get(value)#expect);
let value: <#ty as #crate_ident::Property>::Value =
#crate_ident::Value::get(value)#expect;
(#expr)(&self, ::std::convert::From::from(value));
}
),
(None, MaybeCustomFn::Default) => quote!(
DerivedPropertiesEnum::#enum_ident => {
let value: <#ty as #crate_ident::Property>::Value =
#crate_ident::Value::get(value)#expect;
#crate_ident::PropertySet::set(
&self.#field_ident,
#crate_ident::Value::get(value)#expect
::std::convert::From::from(value)
);
}
),
(Some(member), MaybeCustomFn::Default) => quote!(
DerivedPropertiesEnum::#enum_ident => {
let value: <#ty as #crate_ident::Property>::Value =
#crate_ident::Value::get(value)#expect;
#crate_ident::PropertySetNested::set_nested(
&self.#field_ident,
move |v| v.#member = #crate_ident::Value::get(value)#expect
move |v| v.#member = ::std::convert::From::from(value)
);
}
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a discussion item for this somewhere so we don't forget

I guess there's still one problem: the setter on the wrapper type accepts the inner type, that is, i32, not MyInt(i32).

Expand Down
34 changes: 34 additions & 0 deletions glib-macros/tests/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ mod foo {
use std::sync::Mutex;

use super::base::Base;
use glib::thread_guard::ThreadGuard;

#[derive(Clone, Default, Debug, PartialEq, Eq, glib::Boxed)]
#[boxed_type(name = "SimpleBoxedString")]
Expand All @@ -86,6 +87,24 @@ mod foo {
nick: String,
}

// Custom type, behaving as the inner type
#[derive(Default)]
pub struct MyInt(i32);
impl glib::Property for MyInt {
type Value = i32;
}
impl glib::PropertyGet for MyInt {
type Value = i32;
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
f(&self.0)
}
}
impl From<i32> for MyInt {
fn from(v: i32) -> Self {
MyInt(v)
}
}

pub mod imp {
use glib::{ParamSpec, Value};
use std::rc::Rc;
Expand All @@ -101,10 +120,16 @@ mod foo {
double: RefCell<f64>,
#[property(get = |_| 42.0, set)]
infer_inline_type: RefCell<f64>,
#[property(get, set)]
custom_type: RefCell<MyInt>,
// The following property doesn't store any data. The value of the property is calculated
// when the value is accessed.
#[property(get = Self::hello_world)]
_buzz: PhantomData<String>,
#[property(get, set)]
thread_guard_wrapped: Mutex<ThreadGuard<u32>>,
#[property(get, set)]
thread_guard_wrapping: ThreadGuard<Mutex<u32>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this also already handle a Mutex<Option<ThreadGuard<Paintable>>>? Asking for the Option there. It's a write-only property so I think that potentially works because there's From<T> for Option<T> but I think it doesn't work in practice because you'd need to call from() twice?

#[property(get, set = Self::set_fizz, name = "fizz", nick = "fizz-nick",
blurb = "short description stored in the GLib type system"
)]
Expand Down Expand Up @@ -220,6 +245,15 @@ fn props() {
let author_name: String = myfoo.property("author-nick");
assert_eq!(author_name, "freddy-nick".to_string());

// Complex wrapping
myfoo.set_property("thread-guard-wrapped", 2u32.to_value());
let v: u32 = myfoo.property("thread-guard-wrapped");
assert_eq!(v, 2);

myfoo.set_property("thread-guard-wrapping", 3u32.to_value());
let v: u32 = myfoo.property("thread-guard-wrapping");
assert_eq!(v, 3);

// read_only
assert_eq!(
myfoo.find_property("read_only_text").unwrap().flags(),
Expand Down
17 changes: 17 additions & 0 deletions glib/src/property.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::thread_guard::ThreadGuard;
use std::cell::Cell;
use std::cell::RefCell;
use std::marker::PhantomData;
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 @@ -138,6 +142,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
12 changes: 12 additions & 0 deletions glib/src/thread_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,15 @@ impl<T> Drop for ThreadGuard<T> {
}

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

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

impl<T> From<T> for ThreadGuard<T> {
fn from(value: T) -> Self {
ThreadGuard::new(value)
}
}