| 1 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::{Addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#, Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#, RangeInclusiverust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#, Subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#}; |
| 2 | |
| 3 | pub trait RangeExtrust-analyzer cargo rangemap 1.7.1 std_ext/RangeExt#<T> { |
| 4 | fn overlapsrust-analyzer cargo rangemap 1.7.1 std_ext/RangeExt#overlaps().(&self, other: &Self) -> bool; |
| 5 | fn touchesrust-analyzer cargo rangemap 1.7.1 std_ext/RangeExt#touches().(&self, other: &Self) -> bool; |
| 6 | } |
| 7 | |
| 8 | impl<T> RangeExtrust-analyzer cargo rangemap 1.7.1 std_ext/RangeExt#<T> for Rangerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#<T> |
| 9 | where |
| 10 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#, |
| 11 | { |
| 12 | fn overlapsrust-analyzer cargo rangemap 1.7.1 std_ext/impl#[`Range<T>`][`RangeExt<T>`]overlaps().(&self, other: &Selfrust-analyzer cargo rangemap 1.7.1 std_ext/impl#[`Range<T>`][`RangeExt<T>`]) -> bool { |
| 13 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::{maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max()., minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().}; |
| 14 | // Strictly less than, because ends are excluded. |
| 15 | maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(&self.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start., &other.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt().minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(&self.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end., &other.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end.) |
| 16 | } |
| 17 | |
| 18 | fn touchesrust-analyzer cargo rangemap 1.7.1 std_ext/impl#[`Range<T>`][`RangeExt<T>`]touches().(&self, other: &Selfrust-analyzer cargo rangemap 1.7.1 std_ext/impl#[`Range<T>`][`RangeExt<T>`]) -> bool { |
| 19 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::{maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max()., minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().}; |
| 20 | // Less-than-or-equal-to because if one end is excluded, the other is included. |
| 21 | // I.e. the two could be joined into a single range, because they're overlapping |
| 22 | // or immediately adjacent. |
| 23 | maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(&self.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start., &other.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#start.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]le().minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(&self.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end., &other.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#end.) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | pub trait RangeInclusiveExtrust-analyzer cargo rangemap 1.7.1 std_ext/RangeInclusiveExt#<T> { |
| 28 | fn overlapsrust-analyzer cargo rangemap 1.7.1 std_ext/RangeInclusiveExt#overlaps().(&self, other: &Self) -> bool; |
| 29 | fn touchesrust-analyzer cargo rangemap 1.7.1 std_ext/RangeInclusiveExt#touches().<StepFnsT>(&self, other: &Self) -> bool |
| 30 | where |
| 31 | StepFnsT: StepFnsrust-analyzer cargo rangemap 1.7.1 std_ext/StepFns#<T>; |
| 32 | } |
| 33 | |
| 34 | impl<T> RangeInclusiveExtrust-analyzer cargo rangemap 1.7.1 std_ext/RangeInclusiveExt#<T> for RangeInclusiverust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#<T> |
| 35 | where |
| 36 | T: Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 37 | { |
| 38 | fn overlapsrust-analyzer cargo rangemap 1.7.1 std_ext/impl#[`RangeInclusive<T>`][`RangeInclusiveExt<T>`]overlaps().(&self, other: &Selfrust-analyzer cargo rangemap 1.7.1 std_ext/impl#[`RangeInclusive<T>`][`RangeInclusiveExt<T>`]) -> bool { |
| 39 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::{maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max()., minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().}; |
| 40 | // Less than or equal, because ends are included. |
| 41 | maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(self.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]start().(), other.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]start().()) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]le().minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(self.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]end().(), other.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]end().()) |
| 42 | } |
| 43 | |
| 44 | fn touchesrust-analyzer cargo rangemap 1.7.1 std_ext/impl#[`RangeInclusive<T>`][`RangeInclusiveExt<T>`]touches().<StepFnsT>(&self, other: &Selfrust-analyzer cargo rangemap 1.7.1 std_ext/impl#[`RangeInclusive<T>`][`RangeInclusiveExt<T>`]) -> bool |
| 45 | where |
| 46 | StepFnsT: StepFnsrust-analyzer cargo rangemap 1.7.1 std_ext/StepFns#<T>, |
| 47 | { |
| 48 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::{maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max()., minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().}; |
| 49 | |
| 50 | // Touching for end-inclusive ranges is equivalent to touching of |
| 51 | // slightly longer end-inclusive ranges. |
| 52 | // |
| 53 | // We need to do a small dance to avoid arithmetic overflow |
| 54 | // at the extremes of the key space. And to do this without |
| 55 | // needing to bound our key type on something like `num::Bounded` |
| 56 | // (https://docs.rs/num/0.3.0/num/trait.Bounded.html), |
| 57 | // we'll just extend the end of the _earlier_ range iff |
| 58 | // its end is already earlier than the latter range's start. |
| 59 | let max_start = maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/max().(self.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]start().(), other.startrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]start().()); |
| 60 | let min_range_end = minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(self.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]end().(), other.endrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]end().()); |
| 61 | let min_range_end_extended = if min_range_end rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt().max_start { |
| 62 | StepFnsT::add_onerust-analyzer cargo rangemap 1.7.1 std_ext/StepFns#add_one().(min_range_end) |
| 63 | } else { |
| 64 | min_range_end.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().() |
| 65 | }; |
| 66 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().max_start rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#le().min_range_end_extended |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Minimal version of unstable [`Step`](core::iter::Step) trait |
| 71 | /// from the Rust standard library. |
| 72 | /// |
| 73 | /// This is needed for [`RangeInclusiveMap`](crate::RangeInclusiveMap) |
| 74 | /// because ranges stored as its keys interact with each other |
| 75 | /// when the start of one is _adjacent_ the end of another. |
| 76 | /// I.e. we need a concept of successor values rather than just |
| 77 | /// equality, and that is what `Step` will |
| 78 | /// eventually provide once it is stabilized. |
| 79 | /// |
| 80 | /// **NOTE:** This will likely be deprecated and then eventually |
| 81 | /// removed once the standard library's `Step` |
| 82 | /// trait is stabilised, as most crates will then likely implement `Step` |
| 83 | /// for their types where appropriate. |
| 84 | /// |
| 85 | /// See [this issue](https://github.com/rust-lang/rust/issues/42168) |
| 86 | /// for details about that stabilization process. |
| 87 | pub trait StepLiterust-analyzer cargo rangemap 1.7.1 std_ext/StepLite# { |
| 88 | /// Returns the _successor_ of `self`. |
| 89 | /// |
| 90 | /// If this would overflow the range of values supported by `Self`, |
| 91 | /// this function is allowed to panic, wrap, or saturate. |
| 92 | /// The suggested behavior is to panic when debug assertions are enabled, |
| 93 | /// and to wrap or saturate otherwise. |
| 94 | fn add_onerust-analyzer cargo rangemap 1.7.1 std_ext/StepLite#add_one().(&self) -> Self; |
| 95 | |
| 96 | /// Returns the _predecessor_ of `self`. |
| 97 | /// |
| 98 | /// If this would overflow the range of values supported by `Self`, |
| 99 | /// this function is allowed to panic, wrap, or saturate. |
| 100 | /// The suggested behavior is to panic when debug assertions are enabled, |
| 101 | /// and to wrap or saturate otherwise. |
| 102 | fn sub_onerust-analyzer cargo rangemap 1.7.1 std_ext/StepLite#sub_one().(&self) -> Self; |
| 103 | } |
| 104 | |
| 105 | // Implement for all common integer types. |
| 106 | macro_rules! impl_step_literust-analyzer cargo rangemap 1.7.1 std_ext/impl_step_lite! { |
| 107 | ($($t:ty)*) => ($( |
| 108 | impl StepLite for $t { |
| 109 | #[inline] |
| 110 | fn add_one(&self) -> Self { |
| 111 | Add::add(*self, 1) |
| 112 | } |
| 113 | |
| 114 | #[inline] |
| 115 | fn sub_one(&self) -> Self { |
| 116 | Sub::sub(*self, 1) |
| 117 | } |
| 118 | } |
| 119 | )*) |
| 120 | } |
| 121 | |
| 122 | impl_step_literust-analyzer cargo rangemap 1.7.1 std_ext/impl_step_lite!!(usize u8 u16 u32 u64 u128 i8 i16 i32 i64 i128); |
| 123 | |
| 124 | // TODO: When on nightly, a blanket implementation for |
| 125 | // all types that implement `core::iter::Step` instead |
| 126 | // of the auto-impl above. |
| 127 | |
| 128 | /// Successor and predecessor functions defined for `T`, |
| 129 | /// but as free functions rather than methods on `T` itself. |
| 130 | /// |
| 131 | /// This is useful as a workaround for Rust's "orphan rules", |
| 132 | /// which prevent you from implementing [`StepLite`](crate::StepLite) for `T` if `T` |
| 133 | /// is a foreign type. |
| 134 | /// |
| 135 | /// **NOTE:** This will likely be deprecated and then eventually |
| 136 | /// removed once the standard library's [`Step`](core::iter::Step) |
| 137 | /// trait is stabilised, as most crates will then likely implement `Step` |
| 138 | /// for their types where appropriate. |
| 139 | /// |
| 140 | /// See [this issue](https://github.com/rust-lang/rust/issues/42168) |
| 141 | /// for details about that stabilization process. |
| 142 | /// |
| 143 | /// There is also a blanket implementation of `StepFns` for all |
| 144 | /// types implementing `StepLite`. Consumers of this crate should |
| 145 | /// prefer to implement `StepLite` for their own types, and only |
| 146 | /// fall back to `StepFns` when dealing with foreign types. |
| 147 | pub trait StepFnsrust-analyzer cargo rangemap 1.7.1 std_ext/StepFns#<T> { |
| 148 | /// Returns the _successor_ of value `start`. |
| 149 | /// |
| 150 | /// If this would overflow the range of values supported by `Self`, |
| 151 | /// this function is allowed to panic, wrap, or saturate. |
| 152 | /// The suggested behavior is to panic when debug assertions are enabled, |
| 153 | /// and to wrap or saturate otherwise. |
| 154 | fn add_onerust-analyzer cargo rangemap 1.7.1 std_ext/StepFns#add_one().(start: &T) -> T; |
| 155 | |
| 156 | /// Returns the _predecessor_ of value `start`. |
| 157 | /// |
| 158 | /// If this would overflow the range of values supported by `Self`, |
| 159 | /// this function is allowed to panic, wrap, or saturate. |
| 160 | /// The suggested behavior is to panic when debug assertions are enabled, |
| 161 | /// and to wrap or saturate otherwise. |
| 162 | fn sub_onerust-analyzer cargo rangemap 1.7.1 std_ext/StepFns#sub_one().(start: &T) -> T; |
| 163 | } |
| 164 | |
| 165 | impl<T> StepFnsrust-analyzer cargo rangemap 1.7.1 std_ext/StepFns#<T> for T |
| 166 | where |
| 167 | T: StepLiterust-analyzer cargo rangemap 1.7.1 std_ext/StepLite#, |
| 168 | { |
| 169 | fn add_onerust-analyzer cargo rangemap 1.7.1 std_ext/impl#[T][`StepFns<Self>`]add_one().(start: &T) -> T { |
| 170 | start.add_onerust-analyzer cargo rangemap 1.7.1 std_ext/StepLite#add_one().() |
| 171 | } |
| 172 | |
| 173 | fn sub_onerust-analyzer cargo rangemap 1.7.1 std_ext/impl#[T][`StepFns<Self>`]sub_one().(start: &T) -> T { |
| 174 | start.sub_onerust-analyzer cargo rangemap 1.7.1 std_ext/StepLite#sub_one().() |
| 175 | } |
| 176 | } |