Can #[cfg(test)] load parent module twice with different configs?

I have the following code:

#[cfg(gpu)]
pub fn foo() -> String {
    return "gpu".to_string();
}

#[cfg(not(gpu))]
pub fn foo() -> String {
    return "cpu".to_string();
}



#[cfg(test)]
mod tests {
    use crate::foo;

    #[test]
    fn test_00() {
        println!("{}", foo());
        assert_eq!(2 + 2, 4);
    }

}

Now, I like keeping unit tests together with the defining *.rs file. My ques5tin is: inside the

#[cfg(test)]
mod tests {
....
}

is it possible to somehow load the parent crate twice, one with cfg gpu, once without?

You would build&test-run the entire code twice to use different configurations.
Typically using Cargo Features.

1 Like

From googling so far, this still doesn't work: reason: Cargo wants features to be ADDITIVE.

This means if:

feature=gpu defines one pub fn foo
feature=cpu defines another pub fn foo

then this ends up being a conflict (I'm running into this problem right now.)

=====

It appears that when a crate is imported many times with different features, Rust just loads the crate once, with the UNION of all the features. Thus, it's really requiring that the features be 'additive'.

This is all from trial+error / googling , so I don't have an official reference I can cite.