Skip to content

Commit b8946e5

Browse files
committed
Make WeakRef useable with the Properties derive macro
1 parent 591409c commit b8946e5

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

glib-macros/tests/properties.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ mod foo {
147147
cell: Cell<u8>,
148148
#[property(get = Self::overridden, override_class = Base)]
149149
overridden: PhantomData<u32>,
150+
#[property(get, set)]
151+
weak_ref_prop: glib::WeakRef<glib::Object>,
150152
}
151153

152154
impl ObjectImpl for Foo {

glib/src/property.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use std::sync::Mutex;
1010
use std::sync::RwLock;
1111

1212
use crate::HasParamSpec;
13+
use crate::IsA;
14+
use crate::Object;
15+
use crate::WeakRef;
1316

1417
// rustdoc-stripper-ignore-next
1518
/// A type that can be used as a property. It covers every type which have an associated `ParamSpec`
@@ -50,6 +53,9 @@ impl<T: Property> Property for Rc<T> {
5053
impl<T: Property> Property for Arc<T> {
5154
type Value = T::Value;
5255
}
56+
impl<T: IsA<Object> + HasParamSpec> Property for WeakRef<T> {
57+
type Value = Option<T>;
58+
}
5359

5460
// rustdoc-stripper-ignore-next
5561
/// A container type implementing this trait can be read by the default getter generated by the `Props` macro.
@@ -169,6 +175,21 @@ impl<T> PropertySet for once_cell::unsync::OnceCell<T> {
169175
}
170176
}
171177

178+
impl<T: IsA<Object>> PropertyGet for WeakRef<T> {
179+
type Value = Option<T>;
180+
181+
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
182+
f(&self.upgrade())
183+
}
184+
}
185+
impl<T: IsA<Object>> PropertySet for WeakRef<T> {
186+
type SetValue = Option<T>;
187+
188+
fn set(&self, v: Self::SetValue) {
189+
self.set(v.as_ref())
190+
}
191+
}
192+
172193
// Smart pointers wrapping a `PropertyRead`/`PropertyWrite`
173194
impl<T: PropertyGet> PropertyGet for Rc<T> {
174195
type Value = T::Value;

0 commit comments

Comments
 (0)