@@ -192,7 +192,7 @@ impl fmt::Display for RangeEnd {
192
192
#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
193
193
pub enum MaybeInfiniteInt {
194
194
NegInfinity ,
195
- /// Encoded value. DO NOT CONSTRUCT BY HAND; use `new_finite `.
195
+ /// Encoded value. DO NOT CONSTRUCT BY HAND; use `new_finite_{int,uint} `.
196
196
#[ non_exhaustive]
197
197
Finite ( u128 ) ,
198
198
/// The integer after `u128::MAX`. We need it to represent `x..=u128::MAX` as an exclusive range.
@@ -229,25 +229,22 @@ impl MaybeInfiniteInt {
229
229
}
230
230
231
231
/// Note: this will not turn a finite value into an infinite one or vice-versa.
232
- pub fn minus_one ( self ) -> Self {
232
+ pub fn minus_one ( self ) -> Option < Self > {
233
233
match self {
234
- Finite ( n) => match n. checked_sub ( 1 ) {
235
- Some ( m) => Finite ( m) ,
236
- None => panic ! ( "Called `MaybeInfiniteInt::minus_one` on 0" ) ,
237
- } ,
238
- JustAfterMax => Finite ( u128:: MAX ) ,
239
- x => x,
234
+ Finite ( n) => n. checked_sub ( 1 ) . map ( Finite ) ,
235
+ JustAfterMax => Some ( Finite ( u128:: MAX ) ) ,
236
+ x => Some ( x) ,
240
237
}
241
238
}
242
239
/// Note: this will not turn a finite value into an infinite one or vice-versa.
243
- pub fn plus_one ( self ) -> Self {
240
+ pub fn plus_one ( self ) -> Option < Self > {
244
241
match self {
245
242
Finite ( n) => match n. checked_add ( 1 ) {
246
- Some ( m) => Finite ( m) ,
247
- None => JustAfterMax ,
243
+ Some ( m) => Some ( Finite ( m) ) ,
244
+ None => Some ( JustAfterMax ) ,
248
245
} ,
249
- JustAfterMax => panic ! ( "Called `MaybeInfiniteInt::plus_one` on u128::MAX+1" ) ,
250
- x => x ,
246
+ JustAfterMax => None ,
247
+ x => Some ( x ) ,
251
248
}
252
249
}
253
250
}
@@ -268,18 +265,24 @@ impl IntRange {
268
265
pub fn is_singleton ( & self ) -> bool {
269
266
// Since `lo` and `hi` can't be the same `Infinity` and `plus_one` never changes from finite
270
267
// to infinite, this correctly only detects ranges that contain exacly one `Finite(x)`.
271
- self . lo . plus_one ( ) == self . hi
268
+ self . lo . plus_one ( ) == Some ( self . hi )
272
269
}
273
270
271
+ /// Construct a singleton range.
272
+ /// `x` must be a `Finite(_)` value.
274
273
#[ inline]
275
274
pub fn from_singleton ( x : MaybeInfiniteInt ) -> IntRange {
276
- IntRange { lo : x, hi : x. plus_one ( ) }
275
+ // `unwrap()` is ok on a finite value
276
+ IntRange { lo : x, hi : x. plus_one ( ) . unwrap ( ) }
277
277
}
278
278
279
+ /// Construct a range with these boundaries.
280
+ /// `lo` must not be `PosInfinity` or `JustAfterMax`. `hi` must not be `NegInfinity`.
281
+ /// If `end` is `Included`, `hi` must also not be `JustAfterMax`.
279
282
#[ inline]
280
283
pub fn from_range ( lo : MaybeInfiniteInt , mut hi : MaybeInfiniteInt , end : RangeEnd ) -> IntRange {
281
284
if end == RangeEnd :: Included {
282
- hi = hi. plus_one ( ) ;
285
+ hi = hi. plus_one ( ) . unwrap ( ) ;
283
286
}
284
287
if lo >= hi {
285
288
// This should have been caught earlier by E0030.
0 commit comments