Allow Ranges::contains to accept borrows, e.g. &str for Ranges<String>#301
Merged
Allow Ranges::contains to accept borrows, e.g. &str for Ranges<String>#301
Ranges::contains to accept borrows, e.g. &str for Ranges<String>#301Conversation
…#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>`.
Eh2406
approved these changes
Dec 20, 2024
Member
|
What is our process for releasing a new version of this crate? |
Member
Author
|
It's updating |
Member
|
Two thoughts about this PR.
|
Member
So moving on with my life. |
Eh2406
added a commit
to Eh2406/pubgrub
that referenced
this pull request
Dec 26, 2024
Member
Author
|
This works, but i don't know if it's better: use std::ops::Deref;
struct Db<V>(Vec<V>);
impl<V> Db<V> {
pub fn contains<'a, Q, CommonCmp>(&self, version: &'a Q) -> bool
where
// We deref both internal and external type to this common type that we use for comparison.
CommonCmp: PartialOrd<CommonCmp> + ?Sized,
V: Deref<Target = CommonCmp>,
&'a Q: Deref<Target = CommonCmp>,
Q: PartialOrd<CommonCmp> + ?Sized,
{
self.0
.binary_search_by(|segment| segment.deref().partial_cmp(version.deref()).unwrap())
// An equal interval is one that contains the version
.is_ok()
}
}
fn main() {
let haystack = Db(vec!["1".to_string(), "2".to_string(), "3".to_string()]);
let one = haystack.contains("1");
println!("1: {}", one);
let four = haystack.contains(&"4".to_string());
println!("4: {}", four);
} |
konstin
added a commit
to astral-sh/pubgrub
that referenced
this pull request
Nov 13, 2025
We need pubgrub-rs#301 in uv
github-merge-queue bot
pushed a commit
that referenced
this pull request
Nov 13, 2025
We need #301 in uv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR upstreams astral-sh#35.
This PR borrows a trick from
HashMap to enable users to pass (e.g.)
&strtoRanges::contains, givenRanges<String>.