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
use super::{Hz, LetterOctave, Mel, Perc, ScaledPerc, Step};
use crate::calc::Hz as CalcHz;
use std::convert::From;
macro_rules! impl_from {
($FromType:ty, $ToType:ty, $id:ident => $conv:expr) => {
impl From<$FromType> for $ToType {
fn from($id: $FromType) -> Self {
$conv
}
}
};
($FromType:ty, $ToType:ty, $member:ident) => {
impl_from!($FromType, $ToType, other => other.$member());
};
}
macro_rules! impl_all_pairs {
( $member:ident => $To:ty ) => { };
( $head_member:ident => $Head:ty, $( $tail_member:ident => $Tail:ty ),* )
=> {
$(
impl_from!($Head, $Tail, $tail_member);
impl_from!($Tail, $Head, $head_member);
)*
impl_all_pairs!($($tail_member => $Tail),*);
}
}
impl_all_pairs!(
to_hz => Hz,
to_mel => Mel,
to_letter_octave => LetterOctave,
to_scaled_perc => ScaledPerc,
to_perc => Perc,
to_step => Step
);
impl_from!(CalcHz, Hz, other => Hz(other));
#[cfg(test)]
mod tests {
use super::super::*;
use std::convert::Into;
fn into_test_gen<T: Into<Hz>>(val: T) -> Hz {
val.into()
}
#[test]
fn conversion() {
let lo = LetterOctave(Letter::A, 4);
assert!(Hz::from(lo) == Hz(440.0));
}
#[test]
fn function_call() {
let lo = LetterOctave(Letter::A, 4);
assert!(into_test_gen(lo) == Hz(440.0));
}
}