From aeace1a56291a1bf8316ce46acccc51db7f805eb Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sun, 21 Sep 2025 22:30:38 -0700 Subject: [PATCH] refactor: macro to infer const array length --- .../src/components/test_assembly_chooser.rs | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/app-proto/src/components/test_assembly_chooser.rs b/app-proto/src/components/test_assembly_chooser.rs index 86ad836..1444325 100644 --- a/app-proto/src/components/test_assembly_chooser.rs +++ b/app-proto/src/components/test_assembly_chooser.rs @@ -19,6 +19,14 @@ use crate::{ specified::SpecifiedValue, }; +// Convenience: macro to allow elision of const array lengths +// adapted from https://stackoverflow.com/a/59905715 +macro_rules! const_array { + ($name: ident: $ty: ty = $value: expr) => { + const $name: [$ty; $value.len()] = $value; + } +} + // Convenience: use sqrt() as a function to get the square root of // anything that can be losslessly converted to f64, since we happen // to always want our sqrts to be f64. @@ -299,9 +307,9 @@ fn load_pointed(assembly: &Assembly) { // A-C -0.25 * φ^2 = -0.6545084971874737 fn load_tridiminished_icosahedron(assembly: &Assembly) { // create the vertices - const COLOR_A: ElementColor = [1.00_f32, 0.25_f32, 0.25_f32]; - const COLOR_B: ElementColor = [0.75_f32, 0.75_f32, 0.75_f32]; - const COLOR_C: ElementColor = [0.25_f32, 0.50_f32, 1.00_f32]; + const COLOR_A: ElementColor = [1.00, 0.25, 0.25]; + const COLOR_B: ElementColor = [0.75, 0.75, 0.75]; + const COLOR_C: ElementColor = [0.25, 0.50, 1.00]; let vertices = [ Point::new("a1", "A₁", COLOR_A, point( 0.25, 0.75, 0.75)), Point::new("a2", "A₂", COLOR_A, point( 0.75, 0.25, 0.75)), @@ -318,7 +326,7 @@ fn load_tridiminished_icosahedron(assembly: &Assembly) { } // create the faces - const COLOR_FACE: ElementColor = [0.75_f32, 0.75_f32, 0.75_f32]; + const COLOR_FACE: ElementColor = [0.75, 0.75, 0.75]; const SQRT_1_6: f64 = invsqrt(6.); const SQRT_2_3: f64 = 2. * SQRT_1_6; let faces = [ @@ -442,8 +450,8 @@ fn load_dodecahedral_packing(assembly: &Assembly) { const COLOR_C: ElementColor = [0.25_f32, 0.00_f32, 1.00_f32]; const PHI_INV: f64 = 1.0 / PHI; const COORD_SCALE: f64 = sqrt(PHI + 2.0); - const FACE_SCALES: [f64; 2] = [PHI_INV, (13.0 / 12.0) / COORD_SCALE]; - const FACE_RADII: [f64; 2] = [PHI_INV, 5.0 / 12.0]; + const_array!(FACE_SCALES: f64 = [PHI_INV, (13.0 / 12.0) / COORD_SCALE]); + const_array!(FACE_RADII: f64 = [PHI_INV, 5.0 / 12.0]); let mut faces = Vec::>::new(); let subscripts = ["₀", "₁"]; for j in 0..2 { @@ -652,7 +660,7 @@ fn load_radius_ratio(assembly: &Assembly) { let index_range = 1..=4; // create the spheres - const GRAY: ElementColor = [0.75_f32, 0.75_f32, 0.75_f32]; + const GRAY: ElementColor = [0.75, 0.75, 0.75]; let spheres = [ Sphere::new( "sphere_faces".to_string(), @@ -891,7 +899,7 @@ const F: bool = false; // Free // Initial data for the vertices commmon to 554aug2 and 554domed. // Used the Vectornaut near_miss branch final positions for 554aug2, // to the 3 decimal places currently shown. -const ACRON554_COMMON: [(&str, (f64, f64, f64), bool, usize, &str); 38] = [ +const_array!(ACRON554_COMMON: (&str, (f64, f64, f64), bool, usize, &str) = [ // id, coordinates, Pin/Free, level, earlier neighbor IDs. ("A_NE", ( 0.5, 0.5, 0.), P, 0, ""), ("A_NW", (-0.5, 0.5, 0.), P, 0, ""), @@ -931,9 +939,9 @@ const ACRON554_COMMON: [(&str, (f64, f64, f64), bool, usize, &str); 38] = [ ("G_8", (-1.19, -0.517, 3.312), F, 8, "E_W,F_SW,G_7"), ("G_10", (-1.19, 0.483, 3.318), F, 8, "E_W,F_NW,G_8"), ("G_11", (-0.483, 1.19, 3.318), F, 8, "E_N,F_NW,G_1,G_10"), -]; +]); -const LEVEL_COLORS: [[f32; 3]; 9] = [ +const_array!(LEVEL_COLORS: ElementColor = [ [0.75, 0., 0.75], [1., 0.4, 0.6], [1., 0., 0.25], @@ -943,7 +951,7 @@ const LEVEL_COLORS: [[f32; 3]; 9] = [ [0.25, 0.75, 0.], [0., 0.5, 0.75], [0.25, 0., 1.], -]; +]); fn load_554aug2(assembly: &Assembly) { for (id, v, _pinned, level, _neighbors) in ACRON554_COMMON {