rstsr_test_manifest/
lib.rs

1use num::Complex;
2
3pub fn get_resources_dir() -> std::path::PathBuf {
4    let mut d = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
5    d.push("resources");
6    d
7}
8
9pub trait ResourceVecAPI<T> {
10    fn get_vec(c: char) -> Vec<T>;
11}
12
13pub fn get_vec<T>(c: char) -> Vec<T>
14where
15    (): ResourceVecAPI<T>,
16{
17    <() as ResourceVecAPI<T>>::get_vec(c)
18}
19
20impl ResourceVecAPI<f32> for () {
21    fn get_vec(c: char) -> Vec<f32> {
22        let mut npy_path = get_resources_dir();
23
24        match c {
25            'a' => npy_path.push("a-f32.npy"),
26            'b' => npy_path.push("b-f32.npy"),
27            'c' => npy_path.push("c-f32.npy"),
28            _ => panic!("Invalid character: {}", c),
29        };
30
31        let bytes = std::fs::read(npy_path).unwrap();
32        let reader = npyz::NpyFile::new(&bytes[..]).unwrap();
33        reader.into_vec::<f32>().unwrap()
34    }
35}
36
37impl ResourceVecAPI<f64> for () {
38    fn get_vec(c: char) -> Vec<f64> {
39        let mut npy_path = get_resources_dir();
40
41        match c {
42            'a' => npy_path.push("a-f64.npy"),
43            'b' => npy_path.push("b-f64.npy"),
44            'c' => npy_path.push("c-f64.npy"),
45            _ => panic!("Invalid character: {}", c),
46        };
47
48        let bytes = std::fs::read(npy_path).unwrap();
49        let reader = npyz::NpyFile::new(&bytes[..]).unwrap();
50        reader.into_vec::<f64>().unwrap()
51    }
52}
53
54impl ResourceVecAPI<Complex<f32>> for () {
55    fn get_vec(c: char) -> Vec<Complex<f32>> {
56        let mut npy_path = get_resources_dir();
57
58        match c {
59            'a' => npy_path.push("a-c32.npy"),
60            'b' => npy_path.push("b-c32.npy"),
61            'c' => npy_path.push("c-c32.npy"),
62            _ => panic!("Invalid character: {}", c),
63        };
64
65        let bytes = std::fs::read(npy_path).unwrap();
66        let reader = npyz::NpyFile::new(&bytes[..]).unwrap();
67        reader.into_vec::<Complex<f32>>().unwrap()
68    }
69}
70
71impl ResourceVecAPI<Complex<f64>> for () {
72    fn get_vec(c: char) -> Vec<Complex<f64>> {
73        let mut npy_path = get_resources_dir();
74
75        match c {
76            'a' => npy_path.push("a-c64.npy"),
77            'b' => npy_path.push("b-c64.npy"),
78            'c' => npy_path.push("c-c64.npy"),
79            _ => panic!("Invalid character: {}", c),
80        };
81
82        let bytes = std::fs::read(npy_path).unwrap();
83        let reader = npyz::NpyFile::new(&bytes[..]).unwrap();
84        reader.into_vec::<Complex<f64>>().unwrap()
85    }
86}
87
88#[test]
89fn playground() {
90    use std::path::PathBuf;
91
92    let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
93    d.push("resources");
94
95    println!("{}", d.display());
96
97    let v = get_vec::<Complex<f32>>('a');
98    println!("{:?}", v[1]);
99}