hypermine icon indicating copy to clipboard operation
hypermine copied to clipboard

Modernize Rust idioms

Open Ralith opened this issue 1 year ago • 7 comments

Tracking minor cosmetic improvements enabled by recent changes to Rust:

  • [x] Replace .iter().copied() on arrays with .into_iter()
  • [x] 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 avatar Apr 22 '24 04:04 Ralith

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

inthar-raven avatar Apr 22 '24 16:04 inthar-raven

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

Ralith avatar Apr 22 '24 16:04 Ralith

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 avatar Apr 22 '24 17:04 inthar-raven

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...

inthar-raven avatar Apr 22 '24 17:04 inthar-raven

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

Ralith avatar Apr 22 '24 18:04 Ralith

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.

patowen avatar Apr 23 '24 00:04 patowen

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.

Ralith avatar Apr 23 '24 00:04 Ralith