Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for updating state in serial message callback #375

Merged

Conversation

paulyoung
Copy link
Contributor

@paulyoung paulyoung commented Jun 8, 2024

I wanted to share this example since I think it's a fairly reasonable thing to want to do.

This currently fails with:

error: error[E0501]: cannot borrow `*self` as mutable because previous closure requires unique access
  --> api/system/examples/update-state-in-serial-message-callback.rs:32:5
   |
28 |                   System::Default().set_serial_message_callback(Some(|msg| {
   |                   -                                                  ----- closure construction occurs here
   |  _________________|
   | |
29 | |                                      self.latest_message = Some(msg);
   | |                                      ------------------- first borrow occurs due to use of `self.latest_message` in closure
30 | |                                  }));
   | |____________________________________- argument requires that `self.latest_message` is borrowed for `'static`
31 |
32 |                   self.set_update_handler();
   |                   ^^^^ second borrow occurs here

The workarounds that I came up with so far weren't very satisfying and basically bypassed State altogether.

@github-actions github-actions bot added docs Improvements or additions to documentation api About Playdate API, bindings and all over that. labels Jun 8, 2024
@paulyoung paulyoung changed the title [WIP] Update state in serial message callback Add example for updating state in serial message callback Jun 8, 2024
@paulyoung
Copy link
Contributor Author

I think I have a version of this that seems reasonable. I’ll push that and then perhaps we can discuss alternatives.

@paulyoung paulyoung marked this pull request as ready for review June 9, 2024 02:24
@paulyoung
Copy link
Contributor Author

paulyoung commented Jun 9, 2024

I suspect that trying to update other parts of the state within event (or otherwise access self) will still be problematic.

I’ll add that and see what comes up.

@paulyoung paulyoung marked this pull request as draft June 9, 2024 03:35
@paulyoung paulyoung marked this pull request as ready for review June 9, 2024 22:24
@boozook boozook enabled auto-merge (squash) June 15, 2024 15:38
@boozook boozook merged commit 9997e61 into boozook:main Jun 15, 2024
36 checks passed
boozook added a commit that referenced this pull request Jun 15, 2024
@boozook boozook linked an issue Jun 15, 2024 that may be closed by this pull request
@paulyoung paulyoung deleted the update-state-in-serial-message-callback branch June 17, 2024 04:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api About Playdate API, bindings and all over that. docs Improvements or additions to documentation
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add setSerialMessageCallback
2 participants