Skip to content

Commit 3624c3b

Browse files
charliermarshkonstin
authored andcommitted
Allow Ranges::contains to accept (e.g.) &str for Ranges<String> (#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>`.
1 parent d5fdb12 commit 3624c3b

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/internal/arena.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ impl<T: Hash + Eq + fmt::Debug> fmt::Debug for HashArena<T> {
150150

151151
impl<T: Hash + Eq> HashArena<T> {
152152
pub fn new() -> Self {
153-
HashArena {
154-
data: FnvIndexSet::default(),
155-
}
153+
Self::default()
156154
}
157155

158156
pub fn alloc(&mut self, value: T) -> Id<T> {
@@ -161,6 +159,14 @@ impl<T: Hash + Eq> HashArena<T> {
161159
}
162160
}
163161

162+
impl<T: Hash + Eq> Default for HashArena<T> {
163+
fn default() -> Self {
164+
Self {
165+
data: FnvIndexSet::default(),
166+
}
167+
}
168+
}
169+
164170
impl<T: Hash + Eq> Index<Id<T>> for HashArena<T> {
165171
type Output = T;
166172
fn index(&self, id: Id<T>) -> &T {

src/internal/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<DP: DependencyProvider> State<DP> {
8383
let dep_incompats =
8484
self.add_incompatibility_from_dependencies(package, version.clone(), dependencies);
8585
self.partial_solution.add_package_version_incompatibilities(
86-
package.clone(),
86+
package,
8787
version.clone(),
8888
dep_incompats,
8989
&self.incompatibility_store,

src/solver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub fn resolve<DP: DependencyProvider>(
171171
};
172172

173173
// Add that package and version if the dependencies are not problematic.
174-
state.add_package_version_dependencies(p.clone(), v.clone(), dependencies);
174+
state.add_package_version_dependencies(p, v.clone(), dependencies);
175175
} else {
176176
// `dep_incompats` are already in `incompatibilities` so we know there are not satisfied
177177
// terms and can add the decision directly.

version-ranges/src/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ impl<V: Ord> Ranges<V> {
229229
}
230230

231231
/// Returns true if self contains the specified value.
232-
pub fn contains(&self, version: &V) -> bool {
232+
pub fn contains<Q>(&self, version: &Q) -> bool
233+
where
234+
V: Borrow<Q>,
235+
Q: ?Sized + PartialOrd,
236+
{
233237
self.segments
234238
.binary_search_by(|segment| {
235239
// We have to reverse because we need the segment wrt to the version, while
@@ -470,19 +474,23 @@ impl<V: Ord> Ord for Ranges<V> {
470474
/// ^ ^ ^
471475
/// less equal greater
472476
/// ```
473-
fn within_bounds<V: PartialOrd>(version: &V, segment: &Interval<V>) -> Ordering {
477+
fn within_bounds<Q, V>(version: &Q, segment: &Interval<V>) -> Ordering
478+
where
479+
V: Borrow<Q>,
480+
Q: ?Sized + PartialOrd,
481+
{
474482
let below_lower_bound = match segment {
475-
(Excluded(start), _) => version <= start,
476-
(Included(start), _) => version < start,
483+
(Excluded(start), _) => version <= start.borrow(),
484+
(Included(start), _) => version < start.borrow(),
477485
(Unbounded, _) => false,
478486
};
479487
if below_lower_bound {
480488
return Ordering::Less;
481489
}
482490
let below_upper_bound = match segment {
483491
(_, Unbounded) => true,
484-
(_, Included(end)) => version <= end,
485-
(_, Excluded(end)) => version < end,
492+
(_, Included(end)) => version <= end.borrow(),
493+
(_, Excluded(end)) => version < end.borrow(),
486494
};
487495
if below_upper_bound {
488496
return Ordering::Equal;
@@ -1304,7 +1312,7 @@ pub mod tests {
13041312

13051313
#[test]
13061314
fn always_contains_exact(version in version_strat()) {
1307-
assert!(Ranges::singleton(version).contains(&version));
1315+
assert!(Ranges::<u32>::singleton(version).contains(&version));
13081316
}
13091317

13101318
#[test]
@@ -1326,7 +1334,7 @@ pub mod tests {
13261334

13271335
#[test]
13281336
fn from_range_bounds(range in any::<(Bound<u32>, Bound<u32>)>(), version in version_strat()) {
1329-
let rv: Ranges<_> = Ranges::from_range_bounds(range);
1337+
let rv: Ranges<_> = Ranges::<u32>::from_range_bounds(range);
13301338
assert_eq!(range.contains(&version), rv.contains(&version));
13311339
}
13321340

0 commit comments

Comments
 (0)