Hi all,
I'm trying to use the pub mod functionality to change the signature of public structs. I, am, however, stuck on how to make public modules which are nested in remapped modules (with pub use).
For example if I have this tree structure:
│ main.rs
│
└───locomotion
│ mod.rs
│
├───bike
│ enums.rs
│ mod.rs
│
└───car
enums.rs
mod.rs
Notice I have two enums
modules, one in bike
and one in car
. I would like them to be exported as
locomotion::{Bike, Car}
locomotion::car::enums::*
locomotion::bike::enums::*
But I haven't been able to do it ...
locomotion/mod.rs
mod bike;
mod car;
pub use self::bike::Bike;
pub use self::car::Car;
I can access Bike
and Car
in main.rs
with:
mod locomotion;
use locomotion::{Car, Bike};
The problem here is that I cannot access locomotion::car::enums::*
because they are private:
I've tried two things:
make car module public changing locomotion/mod.rs
locomotion/mod.rs
mod bike;
pub mod car;
pub use self::bike::Bike;
pub use self::car::Car;
This works - I can use locomotion::car::enums::*
without errors. However I can also do this:
main.rs
use locomotion::{Car, Bike};
use locomotion::car::Car as Automobile;
Basically I can import the same module twice with different names (super-confusing IMHO).
Reexport the locomotion::car::enums::Color
enum both in locomotion/car/mod.rs
and in locomotion/mod.rs
:
locomotion/car/mod.rs
mod enums;
pub use self::enums::Color;
#[derive(Debug, PartialEq)]
pub struct Car {
pub c : Color
}
locomotion/mod.rs
mod bike;
mod car;
pub use self::bike::Bike;
pub use self::car::Car;
pub use self::car::Color;
This also works as I can import the Color
enum in main.rs
simply like this:
mod locomotion;
use locomotion::{Car, Bike, Color};
I don't like this approach as it bring the Color
enum in the locomotion
namespace. I would like it to stay under the locomotion::car
namespace instead. This way I would clearly state that Color
applies to Car
only and not to Bike
.
How can I do that?
Thank you for reading this looong question !