Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5c58976

Browse files
committed
fix: convert callback lifetimes to 'static
Except for ev.on_tick which has a test against use-after-free incompile_tests.rs.
1 parentc415fe3 commit5c58976

File tree

6 files changed

+56
-36
lines changed

6 files changed

+56
-36
lines changed

‎CONTRIBUTING/callbacks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Their intended use is for `to_heap_ptr` to turn a Rust function into a bag of bi
1515
by "generic locking" the user and wrapper functions, like so:
1616

1717
```rust
18-
fnon_whatever<'ctx,F:FnMut(&Whatever)>(&mutself,_ctx:&'ctxUI,callback:F) {
18+
fnon_whatever<'ctx,F:FnMut(&Whatever)+ 'static>(&mutself,_ctx:&'ctxUI,callback:F) {
1919

2020
fnc_callback<G:FnMut(&Whatever)> {/* ... do stuff ... */ }
2121

@@ -26,5 +26,5 @@ fn on_whatever<'ctx, F: FnMut(&Whatever)>(&mut self, _ctx: &'ctx UI, callback: F
2626
This is somewhat verbose but ensures that the types do not deviate, which would be unsafe.
2727

2828
Callbacks should be named`on_event` where`event` is, for instance,`clicked` or
29-
`closing`.
29+
`closing`. The functions taken by callbacks must always have the`'static` bound.
3030

‎iui/src/compile_tests.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
11
//! Examples of unsound code that IUI statically prevents from compiling.
22
//!
3-
//! Here, we attempt toplaceuse-after-free some callbacks.
3+
//! Here, we attempt to use-after-free some callbacks.
44
//!
55
//! ```compile_fail
6-
//! letev = iui::UI::init().unwrap();
6+
//! letui = iui::UI::init().unwrap();
77
//!
88
//! {
99
//! let v = vec![1, 2, 3, 4];
10-
//!ev.queue_main(|| {
10+
//!ui.queue_main(|| {
1111
//! for i in &v {
1212
//! println!("{}", i);
1313
//! }
1414
//! });
1515
//! }
16-
//!
17-
//! ev.quit();
18-
//! ev.main();
19-
//! ```
20-
//!
21-
//! ```compile_fail
22-
//! let ev = iui::UI::init().unwrap();
23-
//!
24-
//! {
25-
//! let v = vec![1, 2, 3, 4];
26-
//! ev.on_should_quit(|| {
27-
//! for i in &v {
28-
//! println!("{}", i);
29-
//! }
30-
//! });
31-
//! }
32-
//!
33-
//! ev.quit();
34-
//! ev.main();
3516
//! ```
3617
//!
18+
//! This one is OK, because it moves the `Vec` into the closure's scope.
3719
//! ```
3820
//! let ev = iui::UI::init().unwrap();
3921
//!
@@ -47,3 +29,41 @@
4729
//! ev.quit();
4830
//! ev.main();
4931
//! ```
32+
//!
33+
//! This one tries to use a reference to a string that is dropped out of scope.
34+
//! ```compile_fail
35+
//! # use iui::prelude::*;
36+
//! # use iui::controls::{Button};
37+
//! let ui = UI::init().unwrap();
38+
//! let mut win = Window::new(&ui, "Test App", 200, 200, WindowType::NoMenubar);
39+
//! let mut button = Button::new(&ui, "Button");
40+
//!
41+
//! {
42+
//! let s = String::from("Whatever!");
43+
//! let callback = |b: &mut Button| { println!("{}", s)};
44+
//! button.on_clicked(&ui, callback);
45+
//! }
46+
//!
47+
//! win.set_child(&ui, button);
48+
//! ```
49+
//!
50+
//! Here we try to use-after-free data in the on-tick callback.
51+
//!
52+
//! ```compile_fail
53+
//! # use iui::prelude::*;
54+
//! # use iui::controls::{Button};
55+
//! let ui = UI::init().unwrap();
56+
//! let mut ev = ui.event_loop();
57+
//! let win = Window::new(&ui, "Test App", 200, 200, WindowType::NoMenubar);
58+
//!
59+
//! {
60+
//! let s = String::from("Whatever!");
61+
//! let callback = || { println!("{}", s) };
62+
//! ev.on_tick(&ui, callback);
63+
//! }
64+
//!
65+
//! ev.next_tick(&ui);
66+
//! ui.quit();
67+
//! ev.next_tick(&ui);
68+
//!
69+
//! ```

‎iui/src/controls/button.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Button {
4646
/// Run the given callback when the button is clicked.
4747
pubfnon_clicked<'ctx,F>(&mutself,_ctx:&'ctxUI,callback:F)
4848
where
49-
F:FnMut(&mutButton) +'ctx,
49+
F:FnMut(&mutButton) +'static,
5050
{
5151
extern"C"fnc_callback<G>(button:*mutuiButton,data:*mutc_void)
5252
where

‎iui/src/controls/entry.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ use str_tools::{from_toolkit_string, to_toolkit_string};
1919
pubtraitNumericEntry{
2020
fnvalue(&self,ctx:&UI) ->i32;
2121
fnset_value(&mutself,ctx:&UI,value:i32);
22-
fnon_changed<'ctx,F:FnMut(i32) +'ctx>(&mutself,ctx:&'ctxUI,callback:F);
22+
fnon_changed<'ctx,F:FnMut(i32) +'static>(&mutself,ctx:&'ctxUI,callback:F);
2323
}
2424

2525
pubtraitTextEntry{
2626
fnvalue(&self,ctx:&UI) ->String;
2727
fnset_value(&mutself,ctx:&UI,value:&str);
28-
fnon_changed<'ctx,F:FnMut(String) +'ctx>(&mutself,ctx:&'ctxUI,callback:F);
28+
fnon_changed<'ctx,F:FnMut(String) +'static>(&mutself,ctx:&'ctxUI,callback:F);
2929
}
3030

3131
define_control!{
@@ -70,7 +70,7 @@ impl NumericEntry for Spinbox {
7070

7171
fnon_changed<'ctx,F>(&mutself,_ctx:&'ctxUI,callback:F)
7272
where
73-
F:FnMut(i32) +'ctx,
73+
F:FnMut(i32) +'static
7474
{
7575
extern"C"fnc_callback<G>(spinbox:*mutuiSpinbox,data:*mutc_void)
7676
where
@@ -103,7 +103,7 @@ impl NumericEntry for Slider {
103103

104104
fnon_changed<'ctx,F>(&mutself,_ctx:&'ctxUI,callback:F)
105105
where
106-
F:FnMut(i32) +'ctx,
106+
F:FnMut(i32) +'static
107107
{
108108
extern"C"fnc_callback<G>(slider:*mutuiSlider,data:*mutc_void)
109109
where
@@ -169,7 +169,7 @@ impl TextEntry for Entry {
169169

170170
fnon_changed<'ctx,F>(&mutself,_ctx:&'ctxUI,callback:F)
171171
where
172-
F:FnMut(String) +'ctx,
172+
F:FnMut(String) +'static,
173173
{
174174
extern"C"fnc_callback<G>(entry:*mutuiEntry,data:*mutc_void)
175175
where
@@ -235,7 +235,7 @@ impl TextEntry for MultilineEntry {
235235

236236
fnon_changed<'ctx,F>(&mutself,_ctx:&'ctxUI,callback:F)
237237
where
238-
F:FnMut(String) +'ctx,
238+
F:FnMut(String) +'static,
239239
{
240240
extern"C"fnc_callback<G>(entry:*mutuiMultilineEntry,data:*mutc_void)
241241
where
@@ -288,7 +288,7 @@ impl Combobox {
288288

289289
pubfnon_selected<'ctx,F>(&mutself,_ctx:&'ctxUI,callback:F)
290290
where
291-
F:FnMut(i32) +'ctx,
291+
F:FnMut(i32) +'static,
292292
{
293293
extern"C"fnc_callback<G>(combobox:*mutuiCombobox,data:*mutc_void)
294294
where
@@ -331,7 +331,7 @@ impl Checkbox {
331331

332332
pubfnon_toggled<'ctx,F>(&mutself,_ctx:&'ctxUI,callback:F)
333333
where
334-
F:FnMut(bool) +'ctx,
334+
F:FnMut(bool) +'static,
335335
{
336336
extern"C"fnc_callback<G>(checkbox:*mutuiCheckbox,data:*mutc_void)
337337
where
@@ -376,7 +376,7 @@ impl RadioButtons {
376376
unsafe{ ui_sys::uiRadioButtonsSetSelected(self.uiRadioButtons, idx);}
377377
}
378378

379-
pubfnon_selected<'ctx,F:FnMut(i32) +'ctx>(&self,_ctx:&'ctxUI,callback:F){
379+
pubfnon_selected<'ctx,F:FnMut(i32) +'static>(&self,_ctx:&'ctxUI,callback:F){
380380
unsafe{
381381
letmut data:Box<Box<dynFnMut(i32)>> =Box::new(Box::new(callback));
382382
ui_sys::uiRadioButtonsOnSelected(

‎iui/src/controls/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl Window {
9090
/// the application when the window is closed.
9191
pubfnon_closing<'ctx,F>(&mutself,_ctx:&'ctxUI,callback:F)
9292
where
93-
F:FnMut(&mutWindow) +'ctx,
93+
F:FnMut(&mutWindow) +'static,
9494
{
9595
extern"C"fnc_callback<G>(window:*mutuiWindow,data:*mutc_void) ->i32
9696
where

‎iui/src/menus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl MenuItem {
4848
/// Sets the function to be executed when the item is clicked/selected.
4949
pubfnon_clicked<'ctx,F>(&self,_ctx:&'ctxUI,callback:F)
5050
where
51-
F:FnMut(&MenuItem,&Window) +'ctx,
51+
F:FnMut(&MenuItem,&Window) +'static,
5252
{
5353
extern"C"fnc_callback<G:FnMut(&MenuItem,&Window)>(
5454
menu_item:*mutuiMenuItem,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp