• DRY (不写重复代码)

    DRY (不写重复代码)

    通过提取函数或测试单元的公共部分,宏允许编写 DRY 代码(DRY 是 Don’t Repeat Yourself 的缩写,意思为“不要写重复代码”)。这里给出一个例子,实现并测试了关于 Vec<T>+=*=-= 等运算符。

    1. use std::ops::{Add, Mul, Sub};
    2. macro_rules! assert_equal_len {
    3. // `tt` (token tree,令牌树)指示符用于运算符和令牌。
    4. // (原文:The `tt` (token tree) designator is used for
    5. // operators and tokens.)
    6. ($a:ident, $b: ident, $func:ident, $op:tt) => (
    7. assert!($a.len() == $b.len(),
    8. "{:?}: dimension mismatch: {:?} {:?} {:?}",
    9. stringify!($func),
    10. ($a.len(),),
    11. stringify!($op),
    12. ($b.len(),));
    13. )
    14. }
    15. macro_rules! op {
    16. ($func:ident, $bound:ident, $op:tt, $method:ident) => (
    17. fn $func<T: $bound<T, Output=T> + Copy>(xs: &mut Vec<T>, ys: &Vec<T>) {
    18. assert_equal_len!(xs, ys, $func, $op);
    19. for (x, y) in xs.iter_mut().zip(ys.iter()) {
    20. *x = $bound::$method(*x, *y);
    21. // *x = x.$method(*y);
    22. }
    23. }
    24. )
    25. }
    26. // 实现 `add_assign`、`mul_assign` 和 `sub_assign` 等函数。
    27. op!(add_assign, Add, +=, add);
    28. op!(mul_assign, Mul, *=, mul);
    29. op!(sub_assign, Sub, -=, sub);
    30. mod test {
    31. use std::iter;
    32. macro_rules! test {
    33. ($func: ident, $x:expr, $y:expr, $z:expr) => {
    34. #[test]
    35. fn $func() {
    36. for size in 0usize..10 {
    37. let mut x: Vec<_> = iter::repeat($x).take(size).collect();
    38. let y: Vec<_> = iter::repeat($y).take(size).collect();
    39. let z: Vec<_> = iter::repeat($z).take(size).collect();
    40. super::$func(&mut x, &y);
    41. assert_eq!(x, z);
    42. }
    43. }
    44. }
    45. }
    46. // 测试 `add_assign`、`mul_assign` 和 `sub_assign`
    47. test!(add_assign, 1u32, 2u32, 3u32);
    48. test!(mul_assign, 2u32, 3u32, 6u32);
    49. test!(sub_assign, 3u32, 2u32, 1u32);
    50. }
    1. $ rustc --test dry.rs && ./dry
    2. running 3 tests
    3. test test::mul_assign ... ok
    4. test test::add_assign ... ok
    5. test test::sub_assign ... ok
    6. test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured