-
Notifications
You must be signed in to change notification settings - Fork 20
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
Replace lazy_static! with OnceLock #383
Conversation
0e3690a
to
99bf660
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to remove the lazy_static
dependency from Cargo.toml.
e78df95
to
f5da84a
Compare
I'm wondering if this can be refactored even further. Now that we don't have to put everything in one big (If it does need to be separate, putting these functions in a private module within |
Regarding my previous comment, Initial experimentation on my end suggests that it is indeed possible, although it would be trickier due to not as easily being able to directly convert one array to another. An example implementation for /// Outward normal vector of this side
#[inline]
pub fn normal_f64(self) -> &'static na::Vector4<f64> {
static SIDE_NORMALS_64: OnceLock<[na::Vector4<f64>; SIDE_COUNT]> = OnceLock::new();
&SIDE_NORMALS_64.get_or_init(|| {
let phi = libm::sqrt(1.25) + 0.5; // golden ratio
let f = math::lorentz_normalize(&na::Vector4::new(1.0, phi, 0.0, libm::sqrt(phi)));
let mut result: [na::Vector4<f64>; SIDE_COUNT] = [na::zero(); SIDE_COUNT];
let mut i = 0;
for (x, y, z, w) in [
(f.x, f.y, f.z, f.w),
(-f.x, f.y, -f.z, f.w),
(f.x, -f.y, -f.z, f.w),
(-f.x, -f.y, f.z, f.w),
] {
for (x, y, z, w) in [(x, y, z, w), (y, z, x, w), (z, x, y, w)] {
result[i] = na::Vector4::new(x, y, z, w);
i += 1;
}
}
result
})[self as usize]
} However, the implementation for /// Outward normal vector of this side
#[inline]
pub fn normal(self) -> &'static na::Vector4<f32> {
static SIDE_NORMALS_F32: OnceLock<[na::Vector4<f32>; SIDE_COUNT]> = OnceLock::new();
&SIDE_NORMALS_F32.get_or_init(|| {
Side::iter()
.map(|side| side.normal_f64().cast())
.collect::<Vec<_>>()
.try_into()
.unwrap()
})[self as usize]
} Similar changes would have to be made to any field that depends on another field (which is most of them). @Ralith may have ideas on whether this extra complexity is worth this consolidation, but my current thought is that it probably isn't (although it could be in the future if paired with some other refactors that would make these implementations easier). I think my suggestion for now to avoid making this PR too involved would be to disregard my previous suggestion in removing the helper functions and instead just dump all the things that used to be in |
It's rarely a bad idea to keep things simple and narrowly scoped. |
407e469
to
79af199
Compare
Looks like the CI failure in EDIT: It worked this time. |
3966225
to
c48f136
Compare
No idea why Clippy didn't catch formatting errors locally... |
The errors caught by Clippy are different from the formatting issues caught by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, pretty close now! It's delightful to see rustfmt start working for all that formerly macro-wrapped code.
Done. Changes should just be on Cargo.toml, cursor.rs and dodeca.rs now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
(ref #381)