How to give a modules a different name than a crate

Viewed 368

I have a rather large library that I would like to split up into multiple smaller crates, but I still want to keep the same module names.

So lets say I have a library called xyz, and I want to split it up into the following crates:

  • xyz-core
  • xyz-graphics
  • xyz-audio
  • xyz-input

How can I make it so that the modules are still available like this:

  • xyz::core::*
  • xyz::graphics::*
  • xyz::audio::*
  • xyz::input::*

Instead of turning it into this:

  • xyz_core::*
  • xyz_graphics::*
  • xyz_audio::*
  • xyz_input::*

I'd also like to be able to refer to the crate contents using the xyz::* syntax from within the crates. So xyz-graphics can refer to xyz-core using xyz::core::* etc.

1 Answers

Create a main crate xyz that has those as dependencies, and re-export all items from the subcrates inside it:

pub mod core {
    #[doc(inline)]
    pub use xyz_core::*;
}
pub mod graphics {
    #[doc(inline)]
    pub use xyz_graphics::*;
}
pub mod audio {
    #[doc(inline)]
    pub use xyz_audio::*;
}
pub mod input {
    #[doc(inline)]
    pub use xyz_input::*;
}
Related