Well, I actually made some progress today.
GOAL: Try to get playground VERSION 3 working with actual wasm_bindgen code.
For that to happen, the 4 closures in the swipe module CANNOT have the move
keyword.
Step 1. (swipe closures 1-3)
let closure_r_ts = |e: TouchEvent| { /.../ }
let closure_r_tm = |e: TouchEvent| { /.../ }
let closure_r_tc = || {};
Step 1. The move
keyword was able to be removed from 3 of the 4 closures without issue - even with Atomic references
Step 2. (click closure, separate module)
error[E0501]: cannot borrow `*slides` as immutable because previous closure requires unique access
--> src/lib.rs:241:14
|
231 | let closure_r = |e: Event| {
| ---------- closure construction occurs here
...
234 | slides.get_next(event);
| ------ first borrow occurs due to use of `*slides` in closure
...
237 | let closure_wb = closure_r.to_closure_wb_fnmut();
| ------------------------------- argument requires that `*slides` is borrowed for `'static`
...
241 | let el = slides.get_el(id);
| ^^^^^^^^^^^^^^^^^ second borrow occurs here
Step 2. Cleared this error by reordering the borrows.
I put the borrow without the closure FIRST, then the borrow with the closure AFTER.
Compiler let both borrows through.
Step 3. (click closure, separate module)
error[E0521]: borrowed data escapes outside of function
--> src/lib.rs:245:22
|
229 | fn add_click(slides: &mut SlidesT) {
| ------ - let's call the lifetime of this reference `'1`
| |
| `slides` is a reference that is only valid in the function body
...
245 | let closure_wb = closure_r.to_closure_wb_fnmut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `slides` escapes the function body here
| argument requires that `'1` must outlive `'static`
Step 3. Cleared this error by using the clone + move method.
slides
was cloned outside closure.
Cloned value was used inside closure.
Force ownership of cloned value by using move
keyword.
(OK to use move
keyword here because there aren't any Atomics.)
Step 4. (swipe closure #4)
error[E0501]: cannot borrow `*slides` as immutable because previous closure requires unique access
--> src/swipe.rs:85:17
|
61 | let closure_r_te = || {
| -- closure construction occurs here
...
75 | slides.get_next(event); // left swipe [1]<[2]
| ------ first borrow occurs due to use of `*slides` in closure
...
85 | let el_id = slides.get_el(id);
| ^^^^^^^^^^^^^^^^^ second borrow occurs here
...
95 | let closure_wb_te = closure_r_te.to_closure_wb_fnmut();
| ---------------------------------- argument requires that `*slides` is borrowed for `'static`
(Same as click closure)
Step 4. Cleared this error by reordering the borrows.
I put the borrow without the closure FIRST, then the borrow with the closure (closure +4) AFTER.
Compiler let both borrows through.
Step 5. (swipe closure)
error[E0521]: borrowed data escapes outside of function
--> src/swipe.rs:99:25
|
7 | pub fn add_swipe(slides: &mut SlidesT) {
| ------ - let's call the lifetime of this reference `'1`
| |
| `slides` is a reference that is only valid in the function body
...
99 | let closure_wb_te = closure_r_te.to_closure_wb_fnmut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| `slides` escapes the function body here
| argument requires that `'1` must outlive `'static`
(Same as click closure)
Step 5. Cleared this error by splitting the closure into two parts:
- A closure that uses the clone + move method (see above)
- A closure that contains the Atomics (no
move
keyword).
The closure with the moved slides
value then calls the closure with the Atomics.
So now every closure containing Atomics do not have the move
keyword. So can I now do VERSION 3 with the struct/Atomics?
Not yet: The borrowed touch
is the newly added struct. This is where I currently am.
error[E0373]: closure may outlive the current function, but it borrows `touch.start_x`, which is owned by the current function
--> src/swipe.rs:32:24
|
32 | let closure_r_ts = |e: TouchEvent| {
| ^^^^^^^^^^^^^^^ may outlive borrowed value `touch.start_x`
...
52 | touch.start_x.store(page_x, Ordering::Relaxed);
| ------------- `touch.start_x` is borrowed here
|
note: function requires argument type to outlive `'static`
--> src/swipe.rs:119:25
|
119 | let closure_wb_ts = closure_r_ts.to_closure_wb_fnmut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `touch.start_x` (and any other referenced variables), use the `move` keyword
|
32 | let closure_r_ts = move |e: TouchEvent| {