schungx / rhai

Rhai - An embedded scripting language for Rust [dev repo, may regularly force-push, pull from https://github.com/rhaiscript/rhai for a stable build]
https://github.com/rhaiscript/rhai
Apache License 2.0
9 stars 3 forks source link

Add #[cfg] support in submodules #42

Closed jhwgh1968 closed 4 years ago

jhwgh1968 commented 4 years ago

This is now supported on submodules:

use rhai::plugin::*;

#[export_module]
pub mod advanced_math {

    #[cfg(not(feature = "no_float"))]
    pub mod floating_point {
        use rhai::plugin::*;
        use rhai::FLOAT;
        pub fn get_mystic_number() -> FLOAT {
            42.0 as FLOAT
        }
    }
}

However, in the course of implementing this, I found additional weird ways for the macro to fail that are very confusing to users. For the moment, #[cfg] is forbidden on functions and constants exported to Rhai. I hope this situation is fixable, but if it it isn't, a future flatten tag on rhai_mod should be a suitable workaround.

Also contains an irrelevant drive-by fix for how the UI tests are run.

schungx commented 4 years ago

So far, as long as you can put #[cfg] on a sub-module, it should work nicely. I already have a Module::combine_flatten method that recursively adds all functions.