Skip to content

Commit 1a7d6de

Browse files
authored
add dyn for Fn (#2212)
* add dyn for Fn * revert invalid-imports changes * add one more dyn in doc comments
1 parent 810e6a8 commit 1a7d6de

File tree

7 files changed

+88
-87
lines changed

7 files changed

+88
-87
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ yarn.lock
1010
/publish
1111
/publish.exe
1212
.vscode
13+
webdriver.json

guide/src/contributing/design/describe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ In addition to the shims we talked about above which JS generates the macro
4040
```
4141
#[no_mangle]
4242
pub extern "C" fn __wbindgen_describe_greet() {
43-
<Fn(&str)>::describe();
43+
<dyn Fn(&str)>::describe();
4444
}
4545
```
4646

guide/src/contributing/design/rust-type-conversions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ functions imported into Rust.
8686
Mentioned above not all Rust types will fit within 32 bits. While we can
8787
communicate an `f64` we don't necessarily have the ability to use all the bits.
8888
Types like `&str` need to communicate two items, a pointer and a length (64
89-
bits). Other types like `&Closure<Fn()>` have even more information to
89+
bits). Other types like `&Closure<dyn Fn()>` have even more information to
9090
transmit.
9191

9292
As a result we need a method of communicating more data through the signatures

guide/src/reference/passing-rust-closures-to-js.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ JavaScript in two variants:
1111

1212
## Stack-Lifetime Closures
1313

14-
Closures with a stack lifetime are passed to JavaScript as either `&Fn` or `&mut
15-
FnMut` trait objects:
14+
Closures with a stack lifetime are passed to JavaScript as either `&dyn Fn` or `&mut
15+
dyn FnMut` trait objects:
1616

1717
```rust
1818
// Import JS functions that take closures
1919

2020
#[wasm_bindgen]
2121
extern "C" {
22-
fn takes_immutable_closure(f: &Fn());
22+
fn takes_immutable_closure(f: &dyn Fn());
2323

24-
fn takes_mutable_closure(f: &mut FnMut());
24+
fn takes_mutable_closure(f: &mut dyn FnMut());
2525
}
2626

2727
// Usage
@@ -45,7 +45,7 @@ Closures also support arguments and return values like exports do, for example:
4545
```rust
4646
#[wasm_bindgen]
4747
extern "C" {
48-
fn takes_closure_that_takes_int_and_returns_string(x: &Fn(u32) -> String);
48+
fn takes_closure_that_takes_int_and_returns_string(x: &dyn Fn(u32) -> String);
4949
}
5050

5151
takes_closure_that_takes_int_and_returns_string(&|x: u32| -> String {
@@ -81,7 +81,7 @@ as arguments and returns.
8181
```rust
8282
#[wasm_bindgen]
8383
extern "C" {
84-
fn setInterval(closure: &Closure<FnMut()>, millis: u32) -> f64;
84+
fn setInterval(closure: &Closure<dyn FnMut()>, millis: u32) -> f64;
8585
fn cancelInterval(token: f64);
8686

8787
#[wasm_bindgen(js_namespace = console)]
@@ -90,7 +90,7 @@ extern "C" {
9090

9191
#[wasm_bindgen]
9292
pub struct Interval {
93-
closure: Closure<FnMut()>,
93+
closure: Closure<dyn FnMut()>,
9494
token: f64,
9595
}
9696

guide/src/web-sys/type-translations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ all Web APIs)][webidl] have relatively straightforward translations into
1616
* Callbacks are all represented as `js_sys::Function`. This means that all
1717
callbacks going through `web-sys` are a raw JS value. You can work with this
1818
by either juggling actual `js_sys::Function` instances or you can create a
19-
`Closure<FnMut(...)>`, extract the underlying `JsValue` with `as_ref`, and
19+
`Closure<dyn FnMut(...)>`, extract the underlying `JsValue` with `as_ref`, and
2020
then use `JsCast::unchecked_ref` to convert it to a `js_sys::Function`.
2121

2222
[webidl]: https://heycam.github.io/webidl/

src/closure.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use crate::UnwrapThrowExt;
4848
///
4949
/// #[wasm_bindgen]
5050
/// extern "C" {
51-
/// fn setInterval(closure: &Closure<FnMut()>, time: u32) -> i32;
51+
/// fn setInterval(closure: &Closure<dyn FnMut()>, time: u32) -> i32;
5252
/// fn clearInterval(id: i32);
5353
///
5454
/// #[wasm_bindgen(js_namespace = console)]
@@ -58,7 +58,7 @@ use crate::UnwrapThrowExt;
5858
/// #[wasm_bindgen]
5959
/// pub struct IntervalHandle {
6060
/// interval_id: i32,
61-
/// _closure: Closure<FnMut()>,
61+
/// _closure: Closure<dyn FnMut()>,
6262
/// }
6363
///
6464
/// impl Drop for IntervalHandle {
@@ -73,7 +73,7 @@ use crate::UnwrapThrowExt;
7373
/// // a JS closure.
7474
/// let cb = Closure::wrap(Box::new(|| {
7575
/// log("interval elapsed!");
76-
/// }) as Box<FnMut()>);
76+
/// }) as Box<dyn FnMut()>);
7777
///
7878
/// // Next we pass this via reference to the `setInterval` function, and
7979
/// // `setInterval` gets a handle to the corresponding JS closure.
@@ -101,7 +101,7 @@ use crate::UnwrapThrowExt;
101101
/// #[wasm_bindgen]
102102
/// pub struct IntervalHandle {
103103
/// interval_id: i32,
104-
/// _closure: Closure<FnMut()>,
104+
/// _closure: Closure<dyn FnMut()>,
105105
/// }
106106
///
107107
/// impl Drop for IntervalHandle {
@@ -115,7 +115,7 @@ use crate::UnwrapThrowExt;
115115
/// pub fn run() -> Result<IntervalHandle, JsValue> {
116116
/// let cb = Closure::wrap(Box::new(|| {
117117
/// web_sys::console::log_1(&"inverval elapsed!".into());
118-
/// }) as Box<FnMut()>);
118+
/// }) as Box<dyn FnMut()>);
119119
///
120120
/// let window = web_sys::window().unwrap();
121121
/// let interval_id = window.set_interval_with_callback_and_timeout_and_arguments_0(
@@ -144,7 +144,7 @@ use crate::UnwrapThrowExt;
144144
///
145145
/// #[wasm_bindgen]
146146
/// extern "C" {
147-
/// fn requestAnimationFrame(closure: &Closure<FnMut()>) -> u32;
147+
/// fn requestAnimationFrame(closure: &Closure<dyn FnMut()>) -> u32;
148148
/// fn cancelAnimationFrame(id: u32);
149149
///
150150
/// #[wasm_bindgen(js_namespace = console)]
@@ -154,7 +154,7 @@ use crate::UnwrapThrowExt;
154154
/// #[wasm_bindgen]
155155
/// pub struct AnimationFrameHandle {
156156
/// animation_id: u32,
157-
/// _closure: Closure<FnMut()>,
157+
/// _closure: Closure<dyn FnMut()>,
158158
/// }
159159
///
160160
/// impl Drop for AnimationFrameHandle {
@@ -377,12 +377,12 @@ where
377377

378378
// NB: we use a specific `T` for this `Closure<T>` impl block to avoid every
379379
// call site having to provide an explicit, turbo-fished type like
380-
// `Closure::<FnOnce()>::once(...)`.
380+
// `Closure::<dyn FnOnce()>::once(...)`.
381381
impl Closure<dyn FnOnce()> {
382382
/// Create a `Closure` from a function that can only be called once.
383383
///
384384
/// Since we have no way of enforcing that JS cannot attempt to call this
385-
/// `FnOne(A...) -> R` more than once, this produces a `Closure<FnMut(A...)
385+
/// `FnOne(A...) -> R` more than once, this produces a `Closure<dyn FnMut(A...)
386386
/// -> R>` that will dynamically throw a JavaScript error if called more
387387
/// than once.
388388
///
@@ -404,7 +404,7 @@ impl Closure<dyn FnOnce()> {
404404
///
405405
/// // Create a `Closure` from `f`. Note that the `Closure`'s type parameter
406406
/// // is `FnMut`, even though `f` is `FnOnce`.
407-
/// let closure: Closure<FnMut() -> String> = Closure::once(f);
407+
/// let closure: Closure<dyn FnMut() -> String> = Closure::once(f);
408408
/// ```
409409
pub fn once<F, A, R>(fn_once: F) -> Closure<F::FnMut>
410410
where

0 commit comments

Comments
 (0)