Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Ranges::contains to accept (e.g.) &str for Ranges<String> #35

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

charliermarsh
Copy link
Member

Summary

This PR borrows a trick from HashMap to enable users to pass (e.g.) &str to Ranges::contains, given Ranges<String>.

Copy link

codspeed-hq bot commented Dec 3, 2024

CodSpeed Performance Report

Congrats! CodSpeed is installed 🎉

🆕 6 new benchmarks were detected.

You will start to see performance impacts in the reports once the benchmarks are run from your default branch.

Detected benchmarks

  • backtracking_disjoint_versions (2.3 s)
  • backtracking_ranges (2 s)
  • backtracking_singletons (4.3 s)
  • large_case_u16_NumberVersion.ron (21.3 ms)
  • sudoku-easy (3.8 ms)
  • sudoku-hard (3.9 ms)

self.segments
.binary_search_by(|segment| {
// We have to reverse because we need the segment wrt to the version, while
// within bounds tells us the version wrt to the segment.
within_bounds(version, segment).reverse()
within_bounds(version.borrow(), segment).reverse()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I expected you to need segment.borrow() here. Why does this work... hmmm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g., Below, you are doing start.borrow() and not version.borrow(), which is what I'd expect.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This confused me too... but the inverse doesn't work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you expect that I'd need both segment.borrw() and version.borrow()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just segment.borrow(). I think I'd have to play around with this to figure it out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok. That's a hint that I can pull on then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out I don't need either...? Which I think makes sense given the impl of within_bounds?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooooo derp, yes. I was missing within_bounds, and that's where you do the borrow() call and it is done on the thing I expect.

(This pattern from HashMap inverts the typical use of Borrow, where you call borrow() on the parameter to match what is stored. But here, you want to call borrow() on the thing stored to match what is provided.)

@@ -1326,7 +1334,7 @@ pub mod tests {

#[test]
fn from_range_bounds(range in any::<(Bound<u32>, Bound<u32>)>(), version in version_strat()) {
let rv: Ranges<_> = Ranges::from_range_bounds(range);
let rv: Ranges<_> = Ranges::<u32>::from_range_bounds(range);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this means this is technically a breaking change. (Although we technically permit type inference regressions as semver compatible in Rust-library-land... Unless they are too disruptive.) I only mention this because I think version-ranges is published as a library?

Copy link
Member

@konstin konstin Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this is technically a breaking changes, but i expect that real users use their Ranges as parameters to a function that has the generic parameter fix (the DependencyProvider has), so as an inference change I consider it acceptable.

@charliermarsh charliermarsh merged commit 57832d0 into main Dec 3, 2024
6 checks passed
charliermarsh added a commit to astral-sh/uv that referenced this pull request Dec 3, 2024
## Summary

A small TODO that I found interesting. See:
astral-sh/pubgrub#35.
konstin pushed a commit that referenced this pull request Dec 13, 2024
…#35)

## Summary

This PR borrows a trick from
[HashMap](https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.contains_key)
to enable users to pass (e.g.) `&str` to `Ranges::contains`, given
`Ranges<String>`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants