javascriptcore/auto/
weak_value.rs1use crate::Value;
6use glib::{
7 prelude::*,
8 signal::{connect_raw, SignalHandlerId},
9 translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14 #[doc(alias = "JSCWeakValue")]
15 pub struct WeakValue(Object<ffi::JSCWeakValue, ffi::JSCWeakValueClass>);
16
17 match fn {
18 type_ => || ffi::jsc_weak_value_get_type(),
19 }
20}
21
22impl WeakValue {
23 pub const NONE: Option<&'static WeakValue> = None;
24
25 #[doc(alias = "jsc_weak_value_new")]
26 pub fn new(value: &impl IsA<Value>) -> WeakValue {
27 unsafe { from_glib_full(ffi::jsc_weak_value_new(value.as_ref().to_glib_none().0)) }
28 }
29
30 pub fn builder() -> WeakValueBuilder {
35 WeakValueBuilder::new()
36 }
37}
38
39impl Default for WeakValue {
40 fn default() -> Self {
41 glib::object::Object::new::<Self>()
42 }
43}
44
45#[must_use = "The builder must be built to be used"]
50pub struct WeakValueBuilder {
51 builder: glib::object::ObjectBuilder<'static, WeakValue>,
52}
53
54impl WeakValueBuilder {
55 fn new() -> Self {
56 Self {
57 builder: glib::object::Object::builder(),
58 }
59 }
60
61 pub fn value(self, value: &impl IsA<Value>) -> Self {
62 Self {
63 builder: self.builder.property("value", value.clone().upcast()),
64 }
65 }
66
67 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
70 pub fn build(self) -> WeakValue {
71 self.builder.build()
72 }
73}
74
75mod sealed {
76 pub trait Sealed {}
77 impl<T: super::IsA<super::WeakValue>> Sealed for T {}
78}
79
80pub trait WeakValueExt: IsA<WeakValue> + sealed::Sealed + 'static {
81 #[doc(alias = "jsc_weak_value_get_value")]
82 #[doc(alias = "get_value")]
83 fn value(&self) -> Option<Value> {
84 unsafe {
85 from_glib_full(ffi::jsc_weak_value_get_value(
86 self.as_ref().to_glib_none().0,
87 ))
88 }
89 }
90
91 #[doc(alias = "cleared")]
92 fn connect_cleared<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
93 unsafe extern "C" fn cleared_trampoline<P: IsA<WeakValue>, F: Fn(&P) + 'static>(
94 this: *mut ffi::JSCWeakValue,
95 f: glib::ffi::gpointer,
96 ) {
97 let f: &F = &*(f as *const F);
98 f(WeakValue::from_glib_borrow(this).unsafe_cast_ref())
99 }
100 unsafe {
101 let f: Box_<F> = Box_::new(f);
102 connect_raw(
103 self.as_ptr() as *mut _,
104 b"cleared\0".as_ptr() as *const _,
105 Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
106 cleared_trampoline::<Self, F> as *const (),
107 )),
108 Box_::into_raw(f),
109 )
110 }
111 }
112}
113
114impl<O: IsA<WeakValue>> WeakValueExt for O {}