-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_swizzle.rs
83 lines (76 loc) · 2.24 KB
/
gen_swizzle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
fn main() {
let mut count = 0;
let mut s =
"// WARNING: This file was generated automatically using gen_swizzle.rs. Don't modify
use crate::vec::{PolyVec2, PolyVec3, PolyVec4, W, X, Y, Z};
"
.to_owned();
let comps = ['x', 'y', 'z', 'w'];
s.push_str("// ---------- 2-Component Swizzle ----------\n");
for a in comps {
for b in comps {
gen_swizzle(&mut count, &mut s, &[a, b], "PolyVec2");
}
}
s.push_str("\n\n// ---------- 3-Component Swizzle ----------\n");
for a in comps {
for b in comps {
for c in comps {
gen_swizzle(&mut count, &mut s, &[a, b, c], "PolyVec3");
}
}
}
s.push_str("\n\n// ---------- 4-Component Swizzle ----------\n");
for a in comps {
for b in comps {
for c in comps {
for d in comps {
gen_swizzle(&mut count, &mut s, &[a, b, c, d], "PolyVec4");
}
}
}
}
s.push('\n');
use std::io::Write;
let mut file = std::fs::File::create("./src/swizzle.rs").unwrap();
file.write_all(s.as_bytes()).unwrap();
println!("Successfully wrote {} swizzle implementations", count);
}
fn gen_swizzle(count: &mut u32, s: &mut String, comps: &[char], out: &str) {
*count += 1;
let mut trait_name = String::new();
let comps_str = comps.iter().collect::<String>();
for c in comps {
trait_name.push(c.to_uppercase().next().unwrap());
}
let mut required_traits = comps
.iter()
.map(|c| format!("{}<T>", c.to_uppercase().next().unwrap()))
.collect::<Vec<String>>();
required_traits.dedup();
let required_traits = required_traits.join(" + ");
let new_args = comps
.iter()
.map(|c| format!("*self.{}()", c))
.collect::<Vec<String>>()
.join(", ");
s.push_str(&format!("
pub trait {trait}<T> {{
fn {comps}(&self) -> {out}<T>;
}}
impl<S, T> {trait}<T> for S
where
S: {required},
T: Copy,
{{
#[inline(always)]
fn {comps}(&self) -> {out}<T> {{
{out}::new({new_args})
}}
}}",
trait = trait_name,
comps = comps_str,
required = required_traits,
new_args = new_args
));
}