@@ -320,7 +320,7 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
320
320
/// ```
321
321
/// #![feature(inclusive_range_fields)]
322
322
///
323
- /// assert_eq!((3..=5), std::ops::RangeInclusive { start: 3, end: 5 } );
323
+ /// assert_eq!((3..=5), std::ops::RangeInclusive::new( 3, 5) );
324
324
/// assert_eq!(3 + 4 + 5, (3..=5).sum());
325
325
///
326
326
/// let arr = [0, 1, 2, 3];
@@ -331,14 +331,72 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
331
331
#[ derive( Clone , PartialEq , Eq , Hash ) ] // not Copy -- see #27186
332
332
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
333
333
pub struct RangeInclusive < Idx > {
334
+ // FIXME: The current representation follows RFC 1980,
335
+ // but it is known that LLVM is not able to optimize loops following that RFC.
336
+ // Consider adding an extra `bool` field to indicate emptiness of the range.
337
+ // See #45222 for performance test cases.
338
+ #[ cfg( not( stage0) ) ]
339
+ pub ( crate ) start : Idx ,
340
+ #[ cfg( not( stage0) ) ]
341
+ pub ( crate ) end : Idx ,
334
342
/// The lower bound of the range (inclusive).
343
+ #[ cfg( stage0) ]
335
344
#[ unstable( feature = "inclusive_range_fields" , issue = "49022" ) ]
336
345
pub start : Idx ,
337
346
/// The upper bound of the range (inclusive).
347
+ #[ cfg( stage0) ]
338
348
#[ unstable( feature = "inclusive_range_fields" , issue = "49022" ) ]
339
349
pub end : Idx ,
340
350
}
341
351
352
+ impl < Idx > RangeInclusive < Idx > {
353
+ /// Creates a new inclusive range. Equivalent to writing `start..=end`.
354
+ ///
355
+ /// # Examples
356
+ ///
357
+ /// ```
358
+ /// #![feature(inclusive_range_methods)]
359
+ /// use std::ops::RangeInclusive;
360
+ ///
361
+ /// assert_eq!(3..=5, RangeInclusive::new(3, 5));
362
+ /// ```
363
+ #[ unstable( feature = "inclusive_range_methods" , issue = "49022" ) ]
364
+ #[ inline]
365
+ pub fn new ( start : Idx , end : Idx ) -> Self {
366
+ Self { start, end }
367
+ }
368
+
369
+ /// Returns the lower bound of the range (inclusive).
370
+ ///
371
+ /// # Examples
372
+ ///
373
+ /// ```
374
+ /// #![feature(inclusive_range_methods)]
375
+ ///
376
+ /// assert_eq!((3..=5).start(), &3);
377
+ /// ```
378
+ #[ unstable( feature = "inclusive_range_methods" , issue = "49022" ) ]
379
+ #[ inline]
380
+ pub fn start ( & self ) -> & Idx {
381
+ & self . start
382
+ }
383
+
384
+ /// Returns the upper bound of the range (inclusive).
385
+ ///
386
+ /// # Examples
387
+ ///
388
+ /// ```
389
+ /// #![feature(inclusive_range_methods)]
390
+ ///
391
+ /// assert_eq!((3..=5).end(), &5);
392
+ /// ```
393
+ #[ unstable( feature = "inclusive_range_methods" , issue = "49022" ) ]
394
+ #[ inline]
395
+ pub fn end ( & self ) -> & Idx {
396
+ & self . end
397
+ }
398
+ }
399
+
342
400
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
343
401
impl < Idx : fmt:: Debug > fmt:: Debug for RangeInclusive < Idx > {
344
402
fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
0 commit comments