Ralith / hypermine

A hyperbolic voxel game
Apache License 2.0
160 stars 20 forks source link

Modernize Rust idioms #381

Open Ralith opened 6 months ago

Ralith commented 6 months ago

Tracking minor cosmetic improvements enabled by recent changes to Rust:

inthar-raven commented 6 months ago

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

Ralith commented 6 months ago

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

inthar-raven commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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.