How do I use a crate from another crate without explicitly defining a new dependency in my project?

后端 未结 1 603
鱼传尺愫
鱼传尺愫 2020-12-11 03:00

I want to use the dijkstra function from the pathfinding crate:

pub fn dijkstra(
    start: &N, 
    neighbours: FN,         


        
相关标签:
1条回答
  • 2020-12-11 03:43

    Given the original intent of importing non-exposed dependencies from a crate (such as pathfinding) into a dependent project, that is currently not allowed. If a dependency is not re-exported by the crate, that makes it more of an implementation detail than part of the API. Allowing a dependent to access any "sub-dependency" would therefore be catastrophic.

    In this case however, since num_traits is clearly used in the crate's public API, it also makes sense for the dependent to have access to it. As it is, you are expected to add the dependency in your own project, while taking care to keep a compatible version. Otherwise, cargo might end up building duplicate dependencies.

    [dependencies]
    num_traits = "0.1"
    

    In order to avoid this, pathfinding would benefit from exporting its own num_traits, as below. PR #6 was created for this purpose, and has been merged into version 0.1.12 (thanks, @SamuelTardieu).

    pub extern crate num_traits;
    

    With that done, you can now do exactly as written at the end of your question:

    use pathfinding::num_traits::Zero;
    
    0 讨论(0)
提交回复
热议问题