[cargo] How do I use the dependency of a dependency?


Let’s say I have a package mypackage that depends on a package kiss3d, and kiss3d depends on some third package glfw. Those last two are real packages, but I don’t think that matters right now. Now: let’s say I want to make an object glfw::Obj, and pass it to kiss3d::some_function. Now, in my Cargo.toml I need to depend on both:

kiss3d = "*"
glfw = "*"

But if dependency depends on a different version of base, this won’t work; instead I get a weird error like so:

src/main.rs:213:17: 213:74 error: mismatched types:
 expected `glfw::WindowEvent`,
    found `glfw::WindowEvent`
(expected enum `glfw::WindowEvent`,
    found a different enum `glfw::WindowEvent`) [E0308]

Note that glfw::WindowEvent is the glfw::Obj mentioned above.

This happens because kiss3d has this in its Cargo.toml:

git = "https://github.com/bjz/glfw-rs"
default-features = false

So one version is compiled for kiss3d without default features from the GitHub source, and one is compiled from crates.io with default features for my library.

So now I need to copy and paste this dependency into my Cargo.toml, right?

Or is there some way to say extern crate kiss3d::glfw, meaning “use the same glfw crate that kiss3d is using”, so that I’m not copying and pasting dependencies?

Or should kiss3d be exporting all possible objects from glfw that I might want? Or just exporting the module glfw?


Not sure if this will help, but I solved a similar problem by chance: kiss-ui depends on iup-sys, and I wanted to use a function defined in iup-sys. Seems to work like this (although I am still working on type mismatches between kiss-ui types and iup-sys types, thats a different problem!)

My Cargo.toml

git = "https://github.com/cybergeek94/kiss-ui"

iup-sys = "*"

My main.rs

extern crate kiss_ui;
use kiss_ui::<whatever you need>
extern crate iup_sys;
use iup_sys::IupSetAttributes; 
fn main() {
... IupSetAttributes( ... );

Good luck!


default-features=false shouldn’t be a problem for dependency resolution, since I believe a dependency with the same version used in multiple crates just activates all features used by all of them (so you not having default-features=false will just override their default-features=false).

However, you using the crates.io version and them using the git version does have a conflict. In this, there really isn’t anything to do other then to depend on the git version yourself. Ideally, all crates would depend on each other using crates.io, but that isn’t the case, yet.

I think that’s the case at least - not everything I said might be completely accurate since I haven’t checked in in a while. I would think that if you used the crates.io version of kiss3d, it would be disallowed from using any git dependencies. Are you compiling with the git version of kiss3d or the crates.io one? Better yet, if you could paste the Cargo.lock cargo generated when there was a conflict, that would show what the conflict really is.