From fb5d511ff4937fef8caf14411f827d061b7ad774 Mon Sep 17 00:00:00 2001 From: Mike Boutin Date: Mon, 25 Mar 2019 19:27:25 -0400 Subject: [PATCH] Correct macros to generate valid 2015/2018 edition code. Doc test on the `$quantities!` macro would fail with the error `no ``mks`` external crate` when the `system!` macro was invoked in a crate using the 2018 edition. All invocations of the `$quantities!` macro are corrected and a new test crate, `edition_check`, is added to validate `uom` can be used in 2018 edition code. Resolves #119. --- .travis.yml | 3 +- Cargo.toml | 1 + examples/mks.rs | 6 ++-- src/system.rs | 8 ++++- tests/edition_check/Cargo.toml | 11 ++++++ tests/edition_check/src/lib.rs | 63 ++++++++++++++++++++++++++++++++++ tests/feature_check/Cargo.toml | 1 + tests/feature_check/src/lib.rs | 6 ++-- 8 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 tests/edition_check/Cargo.toml create mode 100644 tests/edition_check/src/lib.rs diff --git a/.travis.yml b/.travis.yml index 78123dc8..2cf415e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,7 +31,8 @@ matrix: script: | set -e cargo test --verbose --features "use_serde" - cargo test --manifest-path tests/feature_check/Cargo.toml --verbose + cargo test -p feature_check --verbose --features "use_serde" + cargo test -p edition_check --verbose --features "use_serde" cargo test --verbose --no-default-features --features "autoconvert f32 f64 si use_serde" cargo test --verbose --no-run --no-default-features --features "autoconvert usize u8 u16 u32 u64 isize i8 i16 i32 i64 bigint biguint rational rational32 rational64 bigrational f32 f64 std use_serde" - name: Rustfmt diff --git a/Cargo.toml b/Cargo.toml index 3d4c0a14..ef801304 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ maintenance = { status = "actively-developed" } [workspace] members = [ "uom-macros", + "tests/edition_check", "tests/feature_check", ] diff --git a/examples/mks.rs b/examples/mks.rs index 2f7fa907..11f7ea56 100644 --- a/examples/mks.rs +++ b/examples/mks.rs @@ -81,9 +81,9 @@ system! { } mod f32 { - mod s { - pub use *; + mod mks { + pub use super::super::*; } - Q!(f32::s, f32); + Q!(self::mks, f32); } diff --git a/src/system.rs b/src/system.rs index 930a4a55..2d34f2e2 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1455,7 +1455,13 @@ macro_rules! system { /// # } /// # } /// mod f32 { - /// Q!(mks, f32/*, (centimeter, gram, second)*/); + /// mod mks { + /// pub use super::super::*; + /// } + /// + /// // `crate::mks` works in Rust 1.30.0 or later. `mod mks {...}` workaround is needed + /// // to support older versions of Rust and the 2018 edition at the same time. + /// Q!(self::mks, f32/*, (centimeter, gram, second)*/); /// } /// # } /// ``` diff --git a/tests/edition_check/Cargo.toml b/tests/edition_check/Cargo.toml new file mode 100644 index 00000000..81182b25 --- /dev/null +++ b/tests/edition_check/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "edition_check" +version = "0.1.0" +edition = "2018" + +[dependencies] +uom = { path = "../.." } + +[features] +default = [] +use_serde = ["uom/use_serde"] diff --git a/tests/edition_check/src/lib.rs b/tests/edition_check/src/lib.rs new file mode 100644 index 00000000..546499ef --- /dev/null +++ b/tests/edition_check/src/lib.rs @@ -0,0 +1,63 @@ +//! Validate that the `system!`, `quantity!`, and `$quantities!` macros generate valid code for the +//! 2018 edition. + +#[macro_use] +extern crate uom; + +mod mks { + mod length { + quantity! { + /// Length (base unit meter, m1). + quantity: Length; "length"; + /// Length dimension, m1. + dimension: Q; + units { + @meter: 1.0E0; "m", "meter", "meters"; + @foot: 3.048E-1; "ft", "foot", "feet"; + } + } + } + + mod mass { + quantity! { + /// Mass (base unit kilogram, kg1). + quantity: Mass; "mass"; + /// Mass dimension, kg1. + dimension: Q; + units { + @kilogram: 1.0; "kg", "kilogram", "kilograms"; + } + } + } + + mod time { + quantity! { + /// Time (base unit second, s1). + quantity: Time; "time"; + /// Time dimension, s1. + dimension: Q; + units { + @second: 1.0; "s", "second", "seconds"; + } + } + } + + system! { + /// System of quantities, Q. + quantities: Q { + length: meter, L; + mass: kilogram, M; + time: second, T; + } + /// System of units, U. + units: U { + mod length::Length, + mod mass::Mass, + mod time::Time, + } + } + + mod f32 { + Q!(crate::mks, f32 /*, (centimeter, gram, second)*/); + } +} diff --git a/tests/feature_check/Cargo.toml b/tests/feature_check/Cargo.toml index 4cf062f0..803a6a50 100644 --- a/tests/feature_check/Cargo.toml +++ b/tests/feature_check/Cargo.toml @@ -7,3 +7,4 @@ uom = { path = "../.." } [features] default = [] +use_serde = ["uom/use_serde"] diff --git a/tests/feature_check/src/lib.rs b/tests/feature_check/src/lib.rs index 50d17dae..39f3339a 100644 --- a/tests/feature_check/src/lib.rs +++ b/tests/feature_check/src/lib.rs @@ -42,9 +42,11 @@ //! } //! //! mod f32 { -//! mod s { pub use *; } +//! mod m { +//! pub use super::super::*; +//! } //! -//! Q!(f32::s, f32); +//! Q!(self::m, f32); //! } //! //! fn main() {