samedi 27 juin 2015

Using custom macros in doc tests

I'm trying to get a custom macro to work in a doc test for the given code, but it's failing to pick up the macro. I believe I've exported it properly, but I can't get the test to pick it up. Can anyone help? Thanks!

In doc / module re-export...

/// The `Measurement` trait and the `implement_measurement!` macro
/// provides a common way for various measurements to be implemented.
///
/// # Example
/// ```
/// #[macro_use] // <-- Not sure this is correct / necessary...
/// use measurements::measurement::*;
/// 
/// struct Cubits {
///     forearms: f64
/// }
/// 
/// impl Measurement for Cubits {
///     fn get_base_units(&self) -> f64 {
///         self.forearms
///     }
///     
///     fn from_base_units(units: f64) -> Self {
///         Cubits { forearms: units }
///     }
/// }
///
/// // Invoke the macro to automatically implement Add, Sub, etc...
/// implement_measurement! { Cubits }
/// ```
#[macro_use]
pub mod measurement;

In definition...

pub use std::ops::{Add,Sub,Div,Mul};
pub use std::cmp::{Eq, PartialEq};
pub use std::cmp::{PartialOrd, Ordering};

pub trait Measurement {
    fn get_base_units(&self) -> f64;
    fn from_base_units(units: f64) -> Self;
}

#[macro_export]
macro_rules! implement_measurement {
    ($($t:ty)*) => ($(
        impl Add for $t {
            type Output = Self;

            fn add(self, rhs: Self) -> Self {
                Self::from_base_units(self.get_base_units() + rhs.get_base_units())
            }
        }

    // ... others ...
))

}

Aucun commentaire:

Enregistrer un commentaire