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

Modernize Rust idioms #381

Open
2 of 3 tasks
Ralith opened this issue Apr 22, 2024 · 7 comments
Open
2 of 3 tasks

Modernize Rust idioms #381

Ralith opened this issue Apr 22, 2024 · 7 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Ralith
Copy link
Owner

Ralith commented Apr 22, 2024

Tracking minor cosmetic improvements enabled by recent changes to Rust:

  • Replace .iter().copied() on arrays with .into_iter()
  • Replace lazy_static with const logic where possible, and otherwise OnceLock (or eventually LazyLock, once stabilized)
  • Replace default-then-linearly-overwrite array construction patterns with array::from_fn
@Ralith Ralith added enhancement New feature or request good first issue Good for newcomers labels Apr 22, 2024
@inthar-raven
Copy link
Contributor

inthar-raven commented Apr 22, 2024

What's the right way to make a lazy OnceLock as of now? Lazy<OnceLock<_>> or OnceLock<Lazy<_>>?

@Ralith
Copy link
Owner Author

Ralith commented Apr 22, 2024

I don't know what the Lazy you're referring to is.

@inthar-raven
Copy link
Contributor

inthar-raven commented Apr 22, 2024

once_cell::sync::Lazy
I tried to use a OnceCell for the static values and was suggested by rustc to wrap the whole thing in Lazy::new(||). Since I don't think OnceLock can be initialized statically, I think Lazy<OnceLock<_>> is the right option. Though now every time I access the static values I have to uset .get().unwrap() first...

@inthar-raven
Copy link
Contributor

inthar-raven commented Apr 22, 2024

Here's a bigger problem with Lazy<OnceLock<_>>...
image

cannot return value referencing temporary value
returns a value referencing data owned by the current function

Now I have to lazify everything, I guess...

@Ralith
Copy link
Owner Author

Ralith commented Apr 22, 2024

We shouldn't introduce a dependency on once_cell. OnceLock is part of std. The documentation illustrates how to use it in a static.

@patowen
Copy link
Collaborator

patowen commented Apr 23, 2024

I think using const logic would still be a bit painful due to being unable to use iterators (so we should probably avoid it for now to avoid potential burnout). OnceLock seems promising, though.

@Ralith
Copy link
Owner Author

Ralith commented Apr 23, 2024

I think there's at least a handful of low hanging fruit that don't use iterators. Definitely no need to force the issue, though. A more serious limitation might be the missing transcendental operations on floats.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants