Skip to content

Commit e32fbaa

Browse files
committed
render_replay: Add unset for optional parameters
1 parent 689c33b commit e32fbaa

File tree

1 file changed

+86
-62
lines changed

1 file changed

+86
-62
lines changed

gsk4/src/render_replay.rs

Lines changed: 86 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,34 @@ impl RenderReplay {
6565
}
6666
}
6767

68-
#[allow(clippy::type_complexity)]
6968
#[doc(alias = "gsk_render_replay_set_font_filter")]
70-
pub fn set_font_filter(
69+
pub fn set_font_filter<P: Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>(
7170
&mut self,
72-
filter: Option<Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>>,
71+
filter: P,
7372
) {
74-
let filter_data: Box_<
75-
Option<Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>>,
76-
> = Box_::new(filter);
77-
unsafe extern "C" fn filter_func(
73+
let filter_data: Box_<P> = Box_::new(filter);
74+
unsafe extern "C" fn filter_func<
75+
P: Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static,
76+
>(
7877
replay: *mut ffi::GskRenderReplay,
7978
font: *mut pango::ffi::PangoFont,
8079
user_data: glib::ffi::gpointer,
8180
) -> *mut pango::ffi::PangoFont {
8281
let replay = RenderReplay(std::ptr::NonNull::new_unchecked(replay));
8382
let font = from_glib_borrow(font);
84-
let callback = &*(user_data
85-
as *mut Option<Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>>);
86-
if let Some(ref callback) = *callback {
87-
callback(&replay, &font)
88-
} else {
89-
panic!("cannot get closure...")
90-
}
91-
.into_glib_ptr()
83+
let callback = &*(user_data as *mut P);
84+
(*callback)(&replay, &font).into_glib_ptr()
9285
}
93-
let filter = if filter_data.is_some() {
94-
Some(filter_func as _)
95-
} else {
96-
None
97-
};
98-
unsafe extern "C" fn user_destroy_func(data: glib::ffi::gpointer) {
99-
let _callback = Box_::from_raw(
100-
data as *mut Option<
101-
Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>,
102-
>,
103-
);
86+
let filter = Some(filter_func::<P> as _);
87+
unsafe extern "C" fn user_destroy_func<
88+
P: Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static,
89+
>(
90+
data: glib::ffi::gpointer,
91+
) {
92+
let _callback = Box_::from_raw(data as *mut P);
10493
}
105-
let destroy_call3 = Some(user_destroy_func as _);
106-
let super_callback0: Box_<
107-
Option<Box_<dyn Fn(&RenderReplay, &pango::Font) -> pango::Font + 'static>>,
108-
> = filter_data;
94+
let destroy_call3 = Some(user_destroy_func::<P> as _);
95+
let super_callback0: Box_<P> = filter_data;
10996
unsafe {
11097
ffi::gsk_render_replay_set_font_filter(
11198
self.0.as_mut(),
@@ -116,6 +103,19 @@ impl RenderReplay {
116103
}
117104
}
118105

106+
#[doc(alias = "gsk_render_replay_set_font_filter")]
107+
#[doc(alias = "set_font_filter")]
108+
pub fn unset_font_filter(&mut self) {
109+
unsafe {
110+
ffi::gsk_render_replay_set_font_filter(
111+
self.0.as_mut(),
112+
None,
113+
std::ptr::null_mut(),
114+
None,
115+
)
116+
}
117+
}
118+
119119
#[doc(alias = "gsk_render_replay_set_node_filter")]
120120
pub fn set_node_filter<P: Fn(&RenderReplay, &RenderNode) -> Option<RenderNode> + 'static>(
121121
&mut self,
@@ -154,6 +154,19 @@ impl RenderReplay {
154154
}
155155
}
156156

157+
#[doc(alias = "gsk_render_replay_set_node_filter")]
158+
#[doc(alias = "set_node_filter")]
159+
pub fn unset_node_filter(&mut self) {
160+
unsafe {
161+
ffi::gsk_render_replay_set_node_filter(
162+
self.0.as_mut(),
163+
None,
164+
std::ptr::null_mut(),
165+
None,
166+
)
167+
}
168+
}
169+
157170
#[doc(alias = "gsk_render_replay_set_node_foreach")]
158171
pub fn set_node_foreach<P: Fn(&RenderReplay, &RenderNode) -> glib::ControlFlow + 'static>(
159172
&mut self,
@@ -192,49 +205,47 @@ impl RenderReplay {
192205
}
193206
}
194207

195-
#[allow(clippy::type_complexity)]
208+
#[doc(alias = "gsk_render_replay_set_node_foreach")]
209+
#[doc(alias = "set_node_foreach")]
210+
pub fn unset_foreach_node(&mut self) {
211+
unsafe {
212+
ffi::gsk_render_replay_set_node_foreach(
213+
self.0.as_mut(),
214+
None,
215+
std::ptr::null_mut(),
216+
None,
217+
)
218+
}
219+
}
220+
196221
#[doc(alias = "gsk_render_replay_set_texture_filter")]
197-
pub fn set_texture_filter(
222+
pub fn set_texture_filter<P: Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>(
198223
&mut self,
199-
filter: Option<Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>>,
224+
filter: P,
200225
) {
201-
let filter_data: Box_<
202-
Option<Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>>,
203-
> = Box_::new(filter);
204-
unsafe extern "C" fn filter_func(
226+
let filter_data: Box_<P> = Box_::new(filter);
227+
unsafe extern "C" fn filter_func<
228+
P: Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static,
229+
>(
205230
replay: *mut ffi::GskRenderReplay,
206231
texture: *mut gdk::ffi::GdkTexture,
207232
user_data: glib::ffi::gpointer,
208233
) -> *mut gdk::ffi::GdkTexture {
209234
let replay = RenderReplay(std::ptr::NonNull::new_unchecked(replay));
210235
let texture = from_glib_borrow(texture);
211-
let callback = &*(user_data
212-
as *mut Option<
213-
Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>,
214-
>);
215-
if let Some(ref callback) = *callback {
216-
callback(&replay, &texture)
217-
} else {
218-
panic!("cannot get closure...")
219-
}
220-
.into_glib_ptr()
236+
let callback = &*(user_data as *mut P);
237+
(*callback)(&replay, &texture).into_glib_ptr()
221238
}
222-
let filter = if filter_data.is_some() {
223-
Some(filter_func as _)
224-
} else {
225-
None
226-
};
227-
unsafe extern "C" fn user_destroy_func(data: glib::ffi::gpointer) {
228-
let _callback = Box_::from_raw(
229-
data as *mut Option<
230-
Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>,
231-
>,
232-
);
239+
let filter = Some(filter_func::<P> as _);
240+
unsafe extern "C" fn user_destroy_func<
241+
P: Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static,
242+
>(
243+
data: glib::ffi::gpointer,
244+
) {
245+
let _callback = Box_::from_raw(data as *mut P);
233246
}
234-
let destroy_call3 = Some(user_destroy_func as _);
235-
let super_callback0: Box_<
236-
Option<Box_<dyn Fn(&RenderReplay, &gdk::Texture) -> gdk::Texture + 'static>>,
237-
> = filter_data;
247+
let destroy_call3 = Some(user_destroy_func::<P> as _);
248+
let super_callback0: Box_<P> = filter_data;
238249
unsafe {
239250
ffi::gsk_render_replay_set_texture_filter(
240251
self.0.as_mut(),
@@ -245,6 +256,19 @@ impl RenderReplay {
245256
}
246257
}
247258

259+
#[doc(alias = "gsk_render_replay_set_texture_filter")]
260+
#[doc(alias = "set_texture_filter")]
261+
pub fn unset_texture_filter(&mut self) {
262+
unsafe {
263+
ffi::gsk_render_replay_set_texture_filter(
264+
self.0.as_mut(),
265+
None,
266+
std::ptr::null_mut(),
267+
None,
268+
)
269+
}
270+
}
271+
248272
#[doc(alias = "gsk_render_replay_new")]
249273
pub fn new() -> RenderReplay {
250274
assert_initialized_main_thread!();

0 commit comments

Comments
 (0)