-
-
Notifications
You must be signed in to change notification settings - Fork 117
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")] | ||
|
@@ -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; | ||
|
@@ -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>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this also already handle a |
||
#[property(get, set = Self::set_fizz, name = "fizz", nick = "fizz-nick", | ||
blurb = "short description stored in the GLib type system" | ||
)] | ||
|
@@ -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(), | ||
|
There was a problem hiding this comment.
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