diff --git a/version-ranges/src/lib.rs b/version-ranges/src/lib.rs index 8b19881b..e8eadb9f 100644 --- a/version-ranges/src/lib.rs +++ b/version-ranges/src/lib.rs @@ -229,7 +229,11 @@ impl Ranges { } /// Returns true if self contains the specified value. - pub fn contains(&self, version: &V) -> bool { + pub fn contains(&self, version: &Q) -> bool + where + V: Borrow, + Q: ?Sized + PartialOrd, + { self.segments .binary_search_by(|segment| { // We have to reverse because we need the segment wrt to the version, while @@ -470,10 +474,14 @@ impl Ord for Ranges { /// ^ ^ ^ /// less equal greater /// ``` -fn within_bounds(version: &V, segment: &Interval) -> Ordering { +fn within_bounds(version: &Q, segment: &Interval) -> Ordering +where + V: Borrow, + Q: ?Sized + PartialOrd, +{ let below_lower_bound = match segment { - (Excluded(start), _) => version <= start, - (Included(start), _) => version < start, + (Excluded(start), _) => version <= start.borrow(), + (Included(start), _) => version < start.borrow(), (Unbounded, _) => false, }; if below_lower_bound { @@ -481,8 +489,8 @@ fn within_bounds(version: &V, segment: &Interval) -> Ordering } let below_upper_bound = match segment { (_, Unbounded) => true, - (_, Included(end)) => version <= end, - (_, Excluded(end)) => version < end, + (_, Included(end)) => version <= end.borrow(), + (_, Excluded(end)) => version < end.borrow(), }; if below_upper_bound { return Ordering::Equal; @@ -1304,7 +1312,7 @@ pub mod tests { #[test] fn always_contains_exact(version in version_strat()) { - assert!(Ranges::singleton(version).contains(&version)); + assert!(Ranges::::singleton(version).contains(&version)); } #[test] @@ -1326,7 +1334,7 @@ pub mod tests { #[test] fn from_range_bounds(range in any::<(Bound, Bound)>(), version in version_strat()) { - let rv: Ranges<_> = Ranges::from_range_bounds(range); + let rv: Ranges<_> = Ranges::::from_range_bounds(range); assert_eq!(range.contains(&version), rv.contains(&version)); }