pubgrub-rs / pubgrub

PubGrub version solving algorithm implemented in Rust
https://pubgrub-rs.github.io/pubgrub/pubgrub/
Mozilla Public License 2.0
357 stars 32 forks source link

Support from creating `Range` from `Bound` #253

Closed charliermarsh closed 2 weeks ago

charliermarsh commented 2 weeks ago

In uv, I have two Bound<Version> that I need to turn into a Range. I ended up doing this semi-manually here. It would be nice to have a public constructor, if appropriate.

Eh2406 commented 2 weeks ago

from_range_bounds?

charliermarsh commented 2 weeks ago

Doesn't from_range_bounds require two Version? Like if v1 and v2 are Version, Range::from_range_bounds(v1..v2)?

mpizenberg commented 2 weeks ago

It takes anything that implements RangeBounds. Here is the link to the docs: https://pubgrub-rs.github.io/pubgrub/pubgrub/range/struct.Range.html#method.from_range_bounds

And since it auto implemented for a pair of bounds I think you can direcly put a pair of bounds there: https://doc.rust-lang.org/nightly/core/ops/trait.RangeBounds.html#impl-RangeBounds%3CT%3E-for-(Bound%3CT%3E,+Bound%3CT%3E)

konstin commented 2 weeks ago

Thanks, I totally missed that!

In our use case, we pass in two RequiresPythonBound that impl Into<Bound<Version>>. The trick that i missed is that we need to eagerly convert into bounds, the Into<V> only applies to the version, not the type with the bound, and specify the target of the conversion:

Range::from_range_bounds::<(Bound<Version>, Bound<Version>), _>((
    value.0.into(),
    value.1.into(),
))
charliermarsh commented 2 weeks ago

Got it, apologies. Skill issue!

charliermarsh commented 2 weeks ago

Thanks for the clarification.

Eh2406 commented 1 week ago

@konstin, is there a better way for the generics on the fn? I'd be open to resuturing to make it EZer to use.