Benjamin Lindley
Benjamin Lindley

Reputation: 103693

How to import a crate as a sub module?

I'd like to import a crate into my library, and then use that crate as if it's a module in my library. If I do this:

extern crate sdl2;
pub use sdl2;

That gives me an error, suggesting I use sdl2::*, but I don't want to drag all the names from the crate into my library, I want them addressed as a sub-module, for example:

my_library::sdl2::init()

I also tried this:

pub extern crate sdl2;

That compiles, but I have no idea what it does. It doesn't seem to make the crate publicly accessible from my library though.

Upvotes: 1

Views: 724

Answers (2)

Lukas Kalbertodt
Lukas Kalbertodt

Reputation: 88536

Another workaround:

extern crate sdl2;
mod sdl2 {
    pub use ::sdl2::*;
}

Should work -- untested though.

Upvotes: 0

Vladimir Matveev
Vladimir Matveev

Reputation: 127741

You can use renaming in use and extern crate:

extern crate sdl2 as sdl2_;
pub use sdl2_ as sdl2;

I personally found how to do it in stdx crate (which appears to be deprecated/abandoned, though, at least for now).

Upvotes: 1

Related Questions