Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 6 pull requests #42644

Merged
merged 14 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
- [n16](library-features/n16.md)
- [never_type_impls](library-features/never-type-impls.md)
- [nonzero](library-features/nonzero.md)
- [ord_max_min](library-features/ord-max-min.md)
- [offset_to](library-features/offset-to.md)
- [once_poison](library-features/once-poison.md)
- [oom](library-features/oom.md)
Expand Down
7 changes: 7 additions & 0 deletions src/doc/unstable-book/src/library-features/ord-max-min.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# `ord-max-min`

The tracking issue for this feature is: [#25663]

[#25663]: https://github.com/rust-lang/rust/issues/25663

------------------------
2 changes: 1 addition & 1 deletion src/grammar/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ while { return WHILE; }
{ident} { return IDENT; }

0x[0-9a-fA-F_]+ { BEGIN(suffix); return LIT_INTEGER; }
0o[0-8_]+ { BEGIN(suffix); return LIT_INTEGER; }
0o[0-7_]+ { BEGIN(suffix); return LIT_INTEGER; }
0b[01_]+ { BEGIN(suffix); return LIT_INTEGER; }
[0-9][0-9_]* { BEGIN(suffix); return LIT_INTEGER; }
[0-9][0-9_]*\.(\.|[a-zA-Z]) { yyless(yyleng - 2); BEGIN(suffix); return LIT_INTEGER; }
Expand Down
1 change: 1 addition & 0 deletions src/libcollections/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#![feature(repr_align)]
#![feature(slice_rotate)]
#![feature(splice)]
#![feature(str_checked_slicing)]
#![feature(str_escape)]
#![feature(test)]
#![feature(unboxed_closures)]
Expand Down
42 changes: 42 additions & 0 deletions src/libcollections/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,48 @@ fn test_slice_fail() {
&"中华Việt Nam"[0..2];
}

#[test]
#[should_panic]
fn test_str_slice_rangetoinclusive_max_panics() {
&"hello"[...usize::max_value()];
}

#[test]
#[should_panic]
fn test_str_slice_rangeinclusive_max_panics() {
&"hello"[1...usize::max_value()];
}

#[test]
#[should_panic]
fn test_str_slicemut_rangetoinclusive_max_panics() {
let mut s = "hello".to_owned();
let s: &mut str = &mut s;
&mut s[...usize::max_value()];
}

#[test]
#[should_panic]
fn test_str_slicemut_rangeinclusive_max_panics() {
let mut s = "hello".to_owned();
let s: &mut str = &mut s;
&mut s[1...usize::max_value()];
}

#[test]
fn test_str_get_maxinclusive() {
let mut s = "hello".to_owned();
{
let s: &str = &s;
assert_eq!(s.get(...usize::max_value()), None);
assert_eq!(s.get(1...usize::max_value()), None);
}
{
let s: &mut str = &mut s;
assert_eq!(s.get(...usize::max_value()), None);
assert_eq!(s.get(1...usize::max_value()), None);
}
}

#[test]
fn test_is_char_boundary() {
Expand Down
44 changes: 42 additions & 2 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,42 @@ pub trait Ord: Eq + PartialOrd<Self> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn cmp(&self, other: &Self) -> Ordering;

/// Compares and returns the maximum of two values.
///
/// Returns the second argument if the comparison determines them to be equal.
///
/// # Examples
///
/// ```
/// #![feature(ord_max_min)]
///
/// assert_eq!(2, 1.max(2));
/// assert_eq!(2, 2.max(2));
/// ```
#[unstable(feature = "ord_max_min", issue = "25663")]
fn max(self, other: Self) -> Self
where Self: Sized {
if other >= self { other } else { self }
}

/// Compares and returns the minimum of two values.
///
/// Returns the first argument if the comparison determines them to be equal.
///
/// # Examples
///
/// ```
/// #![feature(ord_max_min)]
///
/// assert_eq!(1, 1.min(2));
/// assert_eq!(2, 2.min(2));
/// ```
#[unstable(feature = "ord_max_min", issue = "25663")]
fn min(self, other: Self) -> Self
where Self: Sized {
if self <= other { self } else { other }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -678,6 +714,8 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
///
/// Returns the first argument if the comparison determines them to be equal.
///
/// Internally uses an alias to `Ord::min`.
///
/// # Examples
///
/// ```
Expand All @@ -689,13 +727,15 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn min<T: Ord>(v1: T, v2: T) -> T {
if v1 <= v2 { v1 } else { v2 }
v1.min(v2)
}

/// Compares and returns the maximum of two values.
///
/// Returns the second argument if the comparison determines them to be equal.
///
/// Internally uses an alias to `Ord::max`.
///
/// # Examples
///
/// ```
Expand All @@ -707,7 +747,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn max<T: Ord>(v1: T, v2: T) -> T {
if v2 >= v1 { v2 } else { v1 }
v1.max(v2)
}

// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
Expand Down
71 changes: 30 additions & 41 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,12 +1617,7 @@ mod traits {

#[inline]
fn index(&self, index: ops::RangeTo<usize>) -> &str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(index.end) {
unsafe { self.slice_unchecked(0, index.end) }
} else {
super::slice_error_fail(self, 0, index.end)
}
index.index(self)
}
}

Expand All @@ -1636,12 +1631,7 @@ mod traits {
impl ops::IndexMut<ops::RangeTo<usize>> for str {
#[inline]
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(index.end) {
unsafe { self.slice_mut_unchecked(0, index.end) }
} else {
super::slice_error_fail(self, 0, index.end)
}
index.index_mut(self)
}
}

Expand All @@ -1657,12 +1647,7 @@ mod traits {

#[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(index.start) {
unsafe { self.slice_unchecked(index.start, self.len()) }
} else {
super::slice_error_fail(self, index.start, self.len())
}
index.index(self)
}
}

Expand All @@ -1676,13 +1661,7 @@ mod traits {
impl ops::IndexMut<ops::RangeFrom<usize>> for str {
#[inline]
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
// is_char_boundary checks that the index is in [0, .len()]
if self.is_char_boundary(index.start) {
let len = self.len();
unsafe { self.slice_mut_unchecked(index.start, len) }
} else {
super::slice_error_fail(self, index.start, self.len())
}
index.index_mut(self)
}
}

Expand Down Expand Up @@ -1724,9 +1703,7 @@ mod traits {

#[inline]
fn index(&self, index: ops::RangeInclusive<usize>) -> &str {
assert!(index.end != usize::max_value(),
"attempted to index str up to maximum usize");
self.index(index.start .. index.end+1)
index.index(self)
}
}

Expand All @@ -1738,9 +1715,7 @@ mod traits {

#[inline]
fn index(&self, index: ops::RangeToInclusive<usize>) -> &str {
assert!(index.end != usize::max_value(),
"attempted to index str up to maximum usize");
self.index(.. index.end+1)
index.index(self)
}
}

Expand All @@ -1750,9 +1725,7 @@ mod traits {
impl ops::IndexMut<ops::RangeInclusive<usize>> for str {
#[inline]
fn index_mut(&mut self, index: ops::RangeInclusive<usize>) -> &mut str {
assert!(index.end != usize::max_value(),
"attempted to index str up to maximum usize");
self.index_mut(index.start .. index.end+1)
index.index_mut(self)
}
}
#[unstable(feature = "inclusive_range",
Expand All @@ -1761,9 +1734,7 @@ mod traits {
impl ops::IndexMut<ops::RangeToInclusive<usize>> for str {
#[inline]
fn index_mut(&mut self, index: ops::RangeToInclusive<usize>) -> &mut str {
assert!(index.end != usize::max_value(),
"attempted to index str up to maximum usize");
self.index_mut(.. index.end+1)
index.index_mut(self)
}
}

Expand Down Expand Up @@ -1886,6 +1857,7 @@ mod traits {
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
// is_char_boundary checks that the index is in [0, .len()]
if slice.is_char_boundary(self.end) {
unsafe { self.get_unchecked_mut(slice) }
} else {
Expand Down Expand Up @@ -1932,6 +1904,7 @@ mod traits {
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
// is_char_boundary checks that the index is in [0, .len()]
if slice.is_char_boundary(self.start) {
unsafe { self.get_unchecked_mut(slice) }
} else {
Expand All @@ -1945,11 +1918,19 @@ mod traits {
type Output = str;
#[inline]
fn get(self, slice: &str) -> Option<&Self::Output> {
(self.start..self.end+1).get(slice)
if let Some(end) = self.end.checked_add(1) {
(self.start..end).get(slice)
} else {
None
}
}
#[inline]
fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
(self.start..self.end+1).get_mut(slice)
if let Some(end) = self.end.checked_add(1) {
(self.start..end).get_mut(slice)
} else {
None
}
}
#[inline]
unsafe fn get_unchecked(self, slice: &str) -> &Self::Output {
Expand All @@ -1961,10 +1942,14 @@ mod traits {
}
#[inline]
fn index(self, slice: &str) -> &Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
(self.start..self.end+1).index(slice)
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
(self.start..self.end+1).index_mut(slice)
}
}
Expand All @@ -1976,15 +1961,15 @@ mod traits {
type Output = str;
#[inline]
fn get(self, slice: &str) -> Option<&Self::Output> {
if slice.is_char_boundary(self.end + 1) {
if self.end < usize::max_value() && slice.is_char_boundary(self.end + 1) {
Some(unsafe { self.get_unchecked(slice) })
} else {
None
}
}
#[inline]
fn get_mut(self, slice: &mut str) -> Option<&mut Self::Output> {
if slice.is_char_boundary(self.end + 1) {
if self.end < usize::max_value() && slice.is_char_boundary(self.end + 1) {
Some(unsafe { self.get_unchecked_mut(slice) })
} else {
None
Expand All @@ -2002,11 +1987,15 @@ mod traits {
}
#[inline]
fn index(self, slice: &str) -> &Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
let end = self.end + 1;
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
assert!(self.end != usize::max_value(),
"attempted to index str up to maximum usize");
if slice.is_char_boundary(self.end) {
unsafe { self.get_unchecked_mut(slice) }
} else {
Expand Down
10 changes: 10 additions & 0 deletions src/libcore/tests/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ fn test_mut_int_totalord() {
assert_eq!((&mut 12).cmp(&&mut -5), Greater);
}

#[test]
fn test_ord_max_min() {
assert_eq!(1.max(2), 2);
assert_eq!(2.max(1), 2);
assert_eq!(1.min(2), 1);
assert_eq!(2.min(1), 1);
assert_eq!(1.max(1), 1);
assert_eq!(1.min(1), 1);
}

#[test]
fn test_ordering_reverse() {
assert_eq!(Less.reverse(), Greater);
Expand Down
1 change: 1 addition & 0 deletions src/libcore/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#![feature(iter_rfind)]
#![feature(libc)]
#![feature(nonzero)]
#![feature(ord_max_min)]
#![feature(rand)]
#![feature(raw)]
#![feature(sip_hash_13)]
Expand Down
Loading