enhancement - allow to express the version of a crate as a version of a dependent crate #4641
Labels
A-dependency-resolution
Area: dependency resolution and the resolver
C-feature-request
Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
This is an issue that has been bugging me with cargo for a while now, so I wanted to ask if it would be possible for cargo to support the following:
Suppose there is a library
sometinycrate
.sometinycrate
exposesTinyImportantStruct
in its public API. Becausesometinycrate
is very popular, it gets picked up by the larger librarybigwrappercrate
. Sobigwrappercrate v0.0.1
has now a dependency onsometinycrate v0.0.1
.Now I want to build a binary and use
bigwrappercrate
.bigwrappercrate
has a function that takes aTinyImportantStruct
by value. The problem now is: how can I create aTinyImportantStruct
, assuming thatbigwrappercrate
does not re-export theTinyImportantStruct
(which happens often)? There are two options:sometinycrate = "0.0.1"
into the binarysCargo.toml
(bad ifbigwrappercrate
updates tosometinycrate = "0.0.2"
, then my binary build will break)sometinycrate
(bad if he's not available)Note that macro re-exporting does not work on a crate-level basis. Hence, for macro-heavy libraries (let's say
serde-derive
) I have to use Option 1. Now, usually, this problem can be solved by just doing this:Now, in reality, Option 2 is very rare. Nearly nobody re-exports crates.
With Option 1, I now have this setup:
Option 2 is this:
The problem with this is that Rust (rightfully) thinks of
TinyImportantStruct
inv0.0.1
andTinyImportantStruct
inv0.0.2
as completely different structs and therefore won't compile on a version mismatch.How cargo could help
My solution to this problem would be to basically do Option 1, but automate the lookup using Cargo, using a syntax like this:
Don't get too stuck on the syntax, this is just about the idea. Now, let's see what happens if
bigwrappercrate v0.0.2
releases, which depends onsometinycrate = "0.0.2"
. With the manual method, I have to updatemybinary
's Cargo.toml to changesometinycrate = "0.0.2"
- otherwise, it won't even compile. With the proposed method, this is done for me.Here and here are practial applications of this problem. Thanks for reading.
The text was updated successfully, but these errors were encountered: