Skip to content

Commit

Permalink
Auto merge of rust-lang#21717 - nick29581:prelude-fullrange, r=acrichto
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Jan 30, 2015
2 parents e0f5980 + 023d49e commit 1a51eb9
Show file tree
Hide file tree
Showing 25 changed files with 219 additions and 89 deletions.
3 changes: 2 additions & 1 deletion src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ mod std {
pub use core::option; // necessary for panic!()
pub use core::clone; // derive(Clone)
pub use core::cmp; // derive(Eq, Ord, etc.)
pub use core::marker; // derive(Copy)
pub use core::marker; // derive(Copy)
pub use core::hash; // derive(Hash)
pub use core::ops; // RangeFull
}

#[cfg(test)]
Expand Down
6 changes: 6 additions & 0 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ use core::iter::{range_step, MultiplicativeIterator};
use core::marker::Sized;
use core::mem::size_of;
use core::mem;
#[cfg(stage0)]
use core::ops::{FnMut, FullRange};
#[cfg(not(stage0))]
use core::ops::FnMut;
use core::option::Option::{self, Some, None};
use core::ptr::PtrExt;
use core::ptr;
Expand Down Expand Up @@ -1509,7 +1512,10 @@ mod tests {
use core::prelude::{Some, None, range, Clone};
use core::prelude::{Iterator, IteratorExt};
use core::prelude::{AsSlice};
#[cfg(stage0)]
use core::prelude::{Ord, FullRange};
#[cfg(not(stage0))]
use core::prelude::Ord;
use core::default::Default;
use core::mem;
use std::iter::RandomAccessIterator;
Expand Down
10 changes: 8 additions & 2 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ use core::char::CharExt;
use core::clone::Clone;
use core::iter::AdditiveIterator;
use core::iter::{Iterator, IteratorExt};
use core::ops::{FullRange, Index};
use core::ops::Index;
#[cfg(stage0)]
use core::ops::FullRange as RangeFull;
#[cfg(stage0)]
use core::ops::FullRange;
#[cfg(not(stage0))]
use core::ops::RangeFull;
use core::option::Option::{self, Some, None};
use core::slice::AsSlice;
use core::str as core_str;
Expand Down Expand Up @@ -408,7 +414,7 @@ Section: Trait implementations

/// Any string that can be represented as a slice.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait StrExt: Index<FullRange, Output = str> {
pub trait StrExt: Index<RangeFull, Output = str> {
/// Escapes each char in `s` with `char::escape_default`.
#[unstable(feature = "collections",
reason = "return type may change to be an iterator")]
Expand Down
11 changes: 11 additions & 0 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ impl ops::Index<ops::RangeFrom<uint>> for String {
&self[][*index]
}
}
#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::FullRange> for String {
type Output = str;
Expand All @@ -885,6 +886,15 @@ impl ops::Index<ops::FullRange> for String {
unsafe { mem::transmute(self.vec.as_slice()) }
}
}
#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFull> for String {
type Output = str;
#[inline]
fn index(&self, _index: &ops::RangeFull) -> &str {
unsafe { mem::transmute(self.vec.as_slice()) }
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Deref for String {
Expand Down Expand Up @@ -995,6 +1005,7 @@ mod tests {
use str::Utf8Error;
use core::iter::repeat;
use super::{as_string, CowString};
#[cfg(stage0)]
use core::ops::FullRange;

#[test]
Expand Down
21 changes: 21 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,7 @@ impl<T> ops::Index<ops::RangeFrom<uint>> for Vec<T> {
self.as_slice().index(index)
}
}
#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::FullRange> for Vec<T> {
type Output = [T];
Expand All @@ -1325,6 +1326,15 @@ impl<T> ops::Index<ops::FullRange> for Vec<T> {
self.as_slice()
}
}
#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeFull> for Vec<T> {
type Output = [T];
#[inline]
fn index(&self, _index: &ops::RangeFull) -> &[T] {
self.as_slice()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::Range<uint>> for Vec<T> {
Expand All @@ -1350,6 +1360,7 @@ impl<T> ops::IndexMut<ops::RangeFrom<uint>> for Vec<T> {
self.as_mut_slice().index_mut(index)
}
}
#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::FullRange> for Vec<T> {
type Output = [T];
Expand All @@ -1358,6 +1369,15 @@ impl<T> ops::IndexMut<ops::FullRange> for Vec<T> {
self.as_mut_slice()
}
}
#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFull> for Vec<T> {
type Output = [T];
#[inline]
fn index_mut(&mut self, _index: &ops::RangeFull) -> &mut [T] {
self.as_mut_slice()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Deref for Vec<T> {
Expand Down Expand Up @@ -1896,6 +1916,7 @@ mod tests {
use prelude::*;
use core::mem::size_of;
use core::iter::repeat;
#[cfg(stage0)]
use core::ops::FullRange;
use test::Bencher;
use super::as_vec;
Expand Down
3 changes: 3 additions & 0 deletions src/libcore/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
use fmt;
use hash::{Hash, Hasher, self};
use marker::Copy;
#[cfg(stage0)]
use ops::{Deref, FullRange};
#[cfg(not(stage0))]
use ops::Deref;
use option::Option;

// macro for implementing n-ary tuple functions and operations
Expand Down
5 changes: 3 additions & 2 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ mod core {
mod std {
pub use clone;
pub use cmp;
pub use marker;
pub use option;
pub use fmt;
pub use hash;
pub use marker;
pub use ops;
pub use option;
}
17 changes: 17 additions & 0 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,18 +947,35 @@ pub trait IndexMut<Index: ?Sized> {
}

/// An unbounded range.
#[cfg(stage0)]
#[derive(Copy, Clone, PartialEq, Eq)]
#[lang="full_range"]
#[unstable(feature = "core", reason = "may be renamed to RangeFull")]
pub struct FullRange;

/// An unbounded range.
#[cfg(not(stage0))]
#[derive(Copy, Clone, PartialEq, Eq)]
#[lang="range_full"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct RangeFull;

#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Debug for FullRange {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt("..", fmt)
}
}

#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Debug for RangeFull {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt("..", fmt)
}
}

/// A (half-open) range which is bounded at both ends.
#[derive(Copy, Clone, PartialEq, Eq)]
#[lang="range"]
Expand Down
3 changes: 3 additions & 0 deletions src/libcore/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
// Reexported core operators
pub use marker::{Copy, Send, Sized, Sync};
#[cfg(stage0)]
pub use ops::{Drop, Fn, FnMut, FnOnce, FullRange};
#[cfg(not(stage0))]
pub use ops::{Drop, Fn, FnMut, FnOnce};

// Reexported functions
pub use iter::range;
Expand Down
42 changes: 28 additions & 14 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ use iter::*;
use marker::Copy;
use num::Int;
use ops::{FnMut, self, Index};
#[cfg(stage0)]
use ops::FullRange as RangeFull;
#[cfg(not(stage0))]
use ops::RangeFull;
use option::Option;
use option::Option::{None, Some};
use result::Result;
Expand Down Expand Up @@ -543,10 +547,10 @@ impl<T> ops::Index<ops::RangeFrom<uint>> for [T] {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::FullRange> for [T] {
impl<T> ops::Index<RangeFull> for [T] {
type Output = [T];
#[inline]
fn index(&self, _index: &ops::FullRange) -> &[T] {
fn index(&self, _index: &RangeFull) -> &[T] {
self
}
}
Expand Down Expand Up @@ -584,10 +588,10 @@ impl<T> ops::IndexMut<ops::RangeFrom<uint>> for [T] {
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::FullRange> for [T] {
impl<T> ops::IndexMut<RangeFull> for [T] {
type Output = [T];
#[inline]
fn index_mut(&mut self, _index: &ops::FullRange) -> &mut [T] {
fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
self
}
}
Expand Down Expand Up @@ -750,6 +754,7 @@ impl<'a, T> ops::Index<ops::RangeFrom<uint>> for Iter<'a, T> {
}
}

#[cfg(stage0)]
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::FullRange> for Iter<'a, T> {
type Output = [T];
Expand All @@ -758,6 +763,15 @@ impl<'a, T> ops::Index<ops::FullRange> for Iter<'a, T> {
self.as_slice()
}
}
#[cfg(not(stage0))]
#[unstable(feature = "core")]
impl<'a, T> ops::Index<RangeFull> for Iter<'a, T> {
type Output = [T];
#[inline]
fn index(&self, _index: &RangeFull) -> &[T] {
self.as_slice()
}
}

impl<'a, T> Iter<'a, T> {
/// View the underlying data as a subslice of the original data.
Expand Down Expand Up @@ -821,30 +835,30 @@ impl<'a, T> ops::Index<ops::Range<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index(&self, index: &ops::Range<uint>) -> &[T] {
self.index(&ops::FullRange).index(index)
self.index(&RangeFull).index(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeTo<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index(&self, index: &ops::RangeTo<uint>) -> &[T] {
self.index(&ops::FullRange).index(index)
self.index(&RangeFull).index(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeFrom<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
self.index(&ops::FullRange).index(index)
self.index(&RangeFull).index(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::FullRange> for IterMut<'a, T> {
impl<'a, T> ops::Index<RangeFull> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index(&self, _index: &ops::FullRange) -> &[T] {
fn index(&self, _index: &RangeFull) -> &[T] {
make_slice!(T => &[T]: self.ptr, self.end)
}
}
Expand All @@ -854,30 +868,30 @@ impl<'a, T> ops::IndexMut<ops::Range<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
self.index_mut(&ops::FullRange).index_mut(index)
self.index_mut(&RangeFull).index_mut(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::RangeTo<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
self.index_mut(&ops::FullRange).index_mut(index)
self.index_mut(&RangeFull).index_mut(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::RangeFrom<uint>> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
self.index_mut(&ops::FullRange).index_mut(index)
self.index_mut(&RangeFull).index_mut(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::FullRange> for IterMut<'a, T> {
impl<'a, T> ops::IndexMut<RangeFull> for IterMut<'a, T> {
type Output = [T];
#[inline]
fn index_mut(&mut self, _index: &ops::FullRange) -> &mut [T] {
fn index_mut(&mut self, _index: &RangeFull) -> &mut [T] {
make_slice!(T => &mut [T]: self.ptr, self.end)
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,7 @@ mod traits {
}
}

#[cfg(stage0)]
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::FullRange> for str {
type Output = str;
Expand All @@ -1257,6 +1258,15 @@ mod traits {
self
}
}
#[cfg(not(stage0))]
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFull> for str {
type Output = str;
#[inline]
fn index(&self, _index: &ops::RangeFull) -> &str {
self
}
}
}

/// Any string that can be represented as a slice
Expand Down
4 changes: 2 additions & 2 deletions src/libcoretest/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

use test::Bencher;
use core::ops::{Range, FullRange, RangeFrom, RangeTo};
use core::ops::{Range, RangeFull, RangeFrom, RangeTo};

// Overhead of dtors

Expand Down Expand Up @@ -64,5 +64,5 @@ fn test_range_to() {
#[test]
fn test_full_range() {
// Not much to test.
let _ = FullRange;
let _ = RangeFull;
}
2 changes: 1 addition & 1 deletion src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ lets_do_this! {
RangeStructLangItem, "range", range_struct;
RangeFromStructLangItem, "range_from", range_from_struct;
RangeToStructLangItem, "range_to", range_to_struct;
FullRangeStructLangItem, "full_range", full_range_struct;
RangeFullStructLangItem, "range_full", range_full_struct;

UnsafeTypeLangItem, "unsafe", unsafe_type;

Expand Down
Loading

0 comments on commit 1a51eb9

Please sign in to comment.