Skip to content

Use OnceCell for gloo-history #356

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

Closed
jplatte opened this issue Jul 23, 2023 · 2 comments · Fixed by #358
Closed

Use OnceCell for gloo-history #356

jplatte opened this issue Jul 23, 2023 · 2 comments · Fixed by #358

Comments

@jplatte
Copy link
Contributor

jplatte commented Jul 23, 2023

The BrowserHistory initialization could be optimized / simplified a bit by using OnceCell instead of RefCell<Option<_>>. However, this would raise the MSRV to 1.70. Are you interested in a PR?

@futursolo
Copy link
Collaborator

Thank you for the suggestion.

Unlike a Send + Sync static, RefCell<Option<_>> does not have any racing conditions as it will not be able to execute any other code until the init sequence finished executing.

I think the better solution is to use std::cell::LazyCell in which a lazy initialisation is guaranteed to happen once.

Given I cannot see any downside and LazyCell seems to be a better candidate for this use case, I would prefer to wait until LazyCell is stabilised and switch to LazyCell directly.

I will keep this issue open so we can track the stabilisation process of LazyCell.

@jplatte
Copy link
Contributor Author

jplatte commented Jul 23, 2023

OnceCell also guarantees that it's only initialized once, LazyCell is simply a slightly less general but more convenient type on top of OnceCell. Also no race conditions are possible, we are talking about thread_local variables here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants