1
Fork 0

Make the fields of RangeInclusive private.

Added new()/start()/end() methods to RangeInclusive.

Changed the lowering of `..=` to use RangeInclusive::new().
This commit is contained in:
kennytm 2018-04-06 02:03:22 +08:00
parent 64e6dda0bc
commit fba903a435
No known key found for this signature in database
GPG key ID: FEF6C8051D0E013C
6 changed files with 78 additions and 6 deletions

View file

@ -320,7 +320,7 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
/// ```
/// #![feature(inclusive_range_fields)]
///
/// assert_eq!((3..=5), std::ops::RangeInclusive { start: 3, end: 5 });
/// assert_eq!((3..=5), std::ops::RangeInclusive::new(3, 5));
/// assert_eq!(3 + 4 + 5, (3..=5).sum());
///
/// let arr = [0, 1, 2, 3];
@ -331,14 +331,72 @@ impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
#[derive(Clone, PartialEq, Eq, Hash)] // not Copy -- see #27186
#[stable(feature = "inclusive_range", since = "1.26.0")]
pub struct RangeInclusive<Idx> {
// FIXME: The current representation follows RFC 1980,
// but it is known that LLVM is not able to optimize loops following that RFC.
// Consider adding an extra `bool` field to indicate emptiness of the range.
// See #45222 for performance test cases.
#[cfg(not(stage0))]
pub(crate) start: Idx,
#[cfg(not(stage0))]
pub(crate) end: Idx,
/// The lower bound of the range (inclusive).
#[cfg(stage0)]
#[unstable(feature = "inclusive_range_fields", issue = "49022")]
pub start: Idx,
/// The upper bound of the range (inclusive).
#[cfg(stage0)]
#[unstable(feature = "inclusive_range_fields", issue = "49022")]
pub end: Idx,
}
impl<Idx> RangeInclusive<Idx> {
/// Creates a new inclusive range. Equivalent to writing `start..=end`.
///
/// # Examples
///
/// ```
/// #![feature(inclusive_range_methods)]
/// use std::ops::RangeInclusive;
///
/// assert_eq!(3..=5, RangeInclusive::new(3, 5));
/// ```
#[unstable(feature = "inclusive_range_methods", issue = "49022")]
#[inline]
pub fn new(start: Idx, end: Idx) -> Self {
Self { start, end }
}
/// Returns the lower bound of the range (inclusive).
///
/// # Examples
///
/// ```
/// #![feature(inclusive_range_methods)]
///
/// assert_eq!((3..=5).start(), &3);
/// ```
#[unstable(feature = "inclusive_range_methods", issue = "49022")]
#[inline]
pub fn start(&self) -> &Idx {
&self.start
}
/// Returns the upper bound of the range (inclusive).
///
/// # Examples
///
/// ```
/// #![feature(inclusive_range_methods)]
///
/// assert_eq!((3..=5).end(), &5);
/// ```
#[unstable(feature = "inclusive_range_methods", issue = "49022")]
#[inline]
pub fn end(&self) -> &Idx {
&self.end
}
}
#[stable(feature = "inclusive_range", since = "1.26.0")]
impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {