Skip to content

Commit 267f08c

Browse files
authored
Merge pull request #985 from RealKC/weakref-property
Make `WeakRef` and `SendWeakRef` useable with the `Properties` derive macro
2 parents 591409c + 2154a52 commit 267f08c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

glib-macros/tests/properties.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ 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>,
152+
#[property(get, set)]
153+
send_weak_ref_prop: glib::SendWeakRef<glib::Object>,
150154
}
151155

152156
impl ObjectImpl for Foo {

glib/src/property.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ 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::SendWeakRef;
16+
use crate::WeakRef;
1317

1418
// rustdoc-stripper-ignore-next
1519
/// A type that can be used as a property. It covers every type which have an associated `ParamSpec`
@@ -50,6 +54,12 @@ impl<T: Property> Property for Rc<T> {
5054
impl<T: Property> Property for Arc<T> {
5155
type Value = T::Value;
5256
}
57+
impl<T: IsA<Object> + HasParamSpec> Property for WeakRef<T> {
58+
type Value = Option<T>;
59+
}
60+
impl<T: IsA<Object> + HasParamSpec> Property for SendWeakRef<T> {
61+
type Value = Option<T>;
62+
}
5363

5464
// rustdoc-stripper-ignore-next
5565
/// A container type implementing this trait can be read by the default getter generated by the `Props` macro.
@@ -169,6 +179,35 @@ impl<T> PropertySet for once_cell::unsync::OnceCell<T> {
169179
}
170180
}
171181

182+
impl<T: IsA<Object>> PropertyGet for WeakRef<T> {
183+
type Value = Option<T>;
184+
185+
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
186+
f(&self.upgrade())
187+
}
188+
}
189+
impl<T: IsA<Object>> PropertySet for WeakRef<T> {
190+
type SetValue = Option<T>;
191+
192+
fn set(&self, v: Self::SetValue) {
193+
self.set(v.as_ref())
194+
}
195+
}
196+
impl<T: IsA<Object>> PropertyGet for SendWeakRef<T> {
197+
type Value = Option<T>;
198+
199+
fn get<R, F: Fn(&Self::Value) -> R>(&self, f: F) -> R {
200+
f(&self.upgrade())
201+
}
202+
}
203+
impl<T: IsA<Object>> PropertySet for SendWeakRef<T> {
204+
type SetValue = Option<T>;
205+
206+
fn set(&self, v: Self::SetValue) {
207+
WeakRef::set(self, v.as_ref());
208+
}
209+
}
210+
172211
// Smart pointers wrapping a `PropertyRead`/`PropertyWrite`
173212
impl<T: PropertyGet> PropertyGet for Rc<T> {
174213
type Value = T::Value;

0 commit comments

Comments
 (0)