Controlling module names in crate?


#1

I want to write a couple of crates that exist in a shared module namespace.

What I’ll do now is this:

Crate 1 is named library_core so its definitions reside in library_core.

Crate 2 is named library_extras so its definitions reside in library_extras.

What I’d like to do is

Crate 1 is named library_core and its definitions reside in library::core.

Crate 2 is named library_extras and its definitions reside in library::extras.

I want these to be separate crates with separate Cargo.toml so dependencies are more isolated. Is that possible?


#2

Will this do the trick?

extern crate library_core;
extern crate library_extras;

pub mod library {
    pub use library_core as core;
    pub use library_extras as extras;
}

#3

However, given “I want these to be separate crates with separate Cargo.toml so dependencies are more isolated.” and core and extra names, I would suggest looking into Cargo features: http://doc.crates.io/manifest.html#the-features-section


#4

I expect that example would work when if I included them a single package.

I haven’t seen anything in the Cargo documentation that looks to me like the ability to specify the “root module” a given package resides within.


#5

I haven’t seen anything in the Cargo documentation that looks to me like the ability to specify the “root module” a given package resides within.

I mean, maybe you don’t want two crates here? Maybe you want one crate with optional dependencies?

But if you do want separate crates, and do want to access them via a single interface, then you need to create a third crate, which will play the role of facade.


#6

I haven’t seen anything in the Cargo documentation that looks to me like the ability to specify the “root module” a given package resides within.

IIRC, you can place extern crate anywhere:

mod library {
  extern crate foo;
  extern crate bar;
}


#7

By itself each crate has its own root, only when you import it into another crate it becomes a submodule. So you can’t specify such a “root module” in the crate itself.