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

std: Remove index notation on slice iterators #25006

Merged
merged 1 commit into from
May 2, 2015
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
21 changes: 12 additions & 9 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,7 @@ impl<'a> FromIterator<&'a str> for String {
}
}

#[unstable(feature = "collections",
reason = "waiting on Extend stabilization")]
#[stable(feature = "rust1", since = "1.0.0")]
impl Extend<char> for String {
fn extend<I: IntoIterator<Item=char>>(&mut self, iterable: I) {
let iterator = iterable.into_iter();
Expand All @@ -753,8 +752,7 @@ impl Extend<char> for String {
}
}

#[unstable(feature = "collections",
reason = "waiting on Extend stabilization")]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Extend<&'a str> for String {
fn extend<I: IntoIterator<Item=&'a str>>(&mut self, iterable: I) {
let iterator = iterable.into_iter();
Expand Down Expand Up @@ -869,8 +867,7 @@ impl hash::Hash for String {
}
}

#[unstable(feature = "collections",
reason = "recent addition, needs more experience")]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> Add<&'a str> for String {
type Output = String;

Expand Down Expand Up @@ -964,11 +961,17 @@ pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
DerefString { x: as_vec(x.as_bytes()) }
}

#[unstable(feature = "collections", reason = "associated error type may change")]
/// Error returned from `String::from_str`
#[unstable(feature = "str_parse_error", reason = "may want to be replaced with \
Void if it ever exists")]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct ParseError(());

#[stable(feature = "rust1", since = "1.0.0")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: the unstable comment -- should we consider future-proofing this by returning a newtype around unit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow I totally missed that! I think that it may actually be appropriate in this case as String::from_str can never fail. It would in theory be more appropriately represented as Result<String, Void>, but adding Void probably isn't super well motivated by this use case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but this might be one reason to leave it opaque for the time being -- so that if we get Void (which has come up from time to time, and is a tiny API) we could switch over to it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm ok, I've switched it over to an #[unstable] ParseError newtype in the module.

impl FromStr for String {
type Err = ();
type Err = ParseError;
#[inline]
fn from_str(s: &str) -> Result<String, ()> {
fn from_str(s: &str) -> Result<String, ParseError> {
Ok(String::from_str(s))
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
// Common trait implementations for Vec
////////////////////////////////////////////////////////////////////////////////

#[unstable(feature = "collections")]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T:Clone> Clone for Vec<T> {
#[cfg(not(test))]
fn clone(&self) -> Vec<T> { <[T]>::to_vec(&**self) }
Expand Down Expand Up @@ -1554,7 +1554,7 @@ impl<'a, T> IntoIterator for &'a mut Vec<T> {
}
}

#[unstable(feature = "collections", reason = "waiting on Extend stability")]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Extend<T> for Vec<T> {
#[inline]
fn extend<I: IntoIterator<Item=T>>(&mut self, iterable: I) {
Expand Down Expand Up @@ -1614,8 +1614,7 @@ impl<T: Ord> Ord for Vec<T> {
}
}

#[unstable(feature = "collections",
reason = "recent addition, needs more experience")]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T: Clone> Add<&'a [T]> for Vec<T> {
type Output = Vec<T>;

Expand Down Expand Up @@ -1694,7 +1693,7 @@ impl<'a> From<&'a str> for Vec<u8> {
// Clone-on-write
////////////////////////////////////////////////////////////////////////////////

#[unstable(feature = "collections")]
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
fn from_iter<I: IntoIterator<Item=T>>(it: I) -> Cow<'a, [T]> {
Cow::Owned(FromIterator::from_iter(it))
Expand Down
13 changes: 4 additions & 9 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,10 @@ pub trait Iterator {
/// # Examples
///
/// ```
/// # #![feature(core)]
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert!(it.any(|x| *x == 3));
/// assert_eq!(&it[..], [4, 5]);
///
/// assert_eq!(it.collect::<Vec<_>>(), [&4, &5]);
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -654,11 +652,10 @@ pub trait Iterator {
/// # Examples
///
/// ```
/// # #![feature(core)]
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert_eq!(it.find(|&x| *x == 3).unwrap(), &3);
/// assert_eq!(&it[..], [4, 5]);
/// assert_eq!(it.collect::<Vec<_>>(), [&4, &5]);
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
Expand All @@ -678,11 +675,10 @@ pub trait Iterator {
/// # Examples
///
/// ```
/// # #![feature(core)]
/// let a = [1, 2, 3, 4, 5];
/// let mut it = a.iter();
/// assert_eq!(it.position(|x| *x == 3).unwrap(), 2);
/// assert_eq!(&it[..], [4, 5]);
/// assert_eq!(it.collect::<Vec<_>>(), [&4, &5]);
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn position<P>(&mut self, mut predicate: P) -> Option<usize> where
Expand All @@ -708,11 +704,10 @@ pub trait Iterator {
/// # Examples
///
/// ```
/// # #![feature(core)]
/// let a = [1, 2, 2, 4, 5];
/// let mut it = a.iter();
/// assert_eq!(it.rposition(|x| *x == 2).unwrap(), 2);
/// assert_eq!(&it[..], [1, 2]);
/// assert_eq!(it.collect::<Vec<_>>(), [&1, &2]);
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
fn rposition<P>(&mut self, mut predicate: P) -> Option<usize> where
Expand Down
110 changes: 0 additions & 110 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,46 +762,6 @@ pub struct Iter<'a, T: 'a> {
unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {}
unsafe impl<'a, T: Sync> Send for Iter<'a, T> {}

#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::Range<usize>> for Iter<'a, T> {
type Output = [T];

#[inline]
fn index(&self, index: ops::Range<usize>) -> &[T] {
self.as_slice().index(index)
}
}

#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeTo<usize>> for Iter<'a, T> {
type Output = [T];

#[inline]
fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
self.as_slice().index(index)
}
}

#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeFrom<usize>> for Iter<'a, T> {
type Output = [T];

#[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
self.as_slice().index(index)
}
}

#[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 @@ -873,76 +833,6 @@ pub struct IterMut<'a, T: 'a> {
unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}
unsafe impl<'a, T: Send> Send for IterMut<'a, T> {}

#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::Range<usize>> for IterMut<'a, T> {
type Output = [T];

#[inline]
fn index(&self, index: ops::Range<usize>) -> &[T] {
self.index(RangeFull).index(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeTo<usize>> for IterMut<'a, T> {
type Output = [T];

#[inline]
fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
self.index(RangeFull).index(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<ops::RangeFrom<usize>> for IterMut<'a, T> {
type Output = [T];

#[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
self.index(RangeFull).index(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::Index<RangeFull> for IterMut<'a, T> {
type Output = [T];

#[inline]
fn index(&self, _index: RangeFull) -> &[T] {
make_slice!(T => &[T]: self.ptr, self.end)
}
}

#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::Range<usize>> for IterMut<'a, T> {
#[inline]
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] {
self.index_mut(RangeFull).index_mut(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::RangeTo<usize>> for IterMut<'a, T> {

#[inline]
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] {
self.index_mut(RangeFull).index_mut(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<ops::RangeFrom<usize>> for IterMut<'a, T> {

#[inline]
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] {
self.index_mut(RangeFull).index_mut(index)
}
}
#[unstable(feature = "core")]
impl<'a, T> ops::IndexMut<RangeFull> for IterMut<'a, T> {

#[inline]
fn index_mut(&mut self, _index: RangeFull) -> &mut [T] {
make_mut_slice!(T => &mut [T]: self.ptr, self.end)
}
}


impl<'a, T> IterMut<'a, T> {
/// View the underlying data as a subslice of the original data.
///
Expand Down
49 changes: 0 additions & 49 deletions src/libcoretest/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,6 @@ fn binary_search_not_found() {
assert!(b.binary_search_by(|v| v.cmp(&9)) == Err(6));
}

#[test]
fn iterator_to_slice() {
macro_rules! test {
($data: expr) => {{
let data: &mut [_] = &mut $data;
let other_data: &mut [_] = &mut $data;

{
let mut iter = data.iter();
assert_eq!(&iter[..], &other_data[..]);

iter.next();
assert_eq!(&iter[..], &other_data[1..]);

iter.next_back();
assert_eq!(&iter[..], &other_data[1..2]);

let s = iter.as_slice();
iter.next();
assert_eq!(s, &other_data[1..2]);
}
{
let mut iter = data.iter_mut();
assert_eq!(&iter[..], &other_data[..]);
// mutability:
assert!(&mut iter[..] == other_data);

iter.next();
assert_eq!(&iter[..], &other_data[1..]);
assert!(&mut iter[..] == &mut other_data[1..]);

iter.next_back();

assert_eq!(&iter[..], &other_data[1..2]);
assert!(&mut iter[..] == &mut other_data[1..2]);

let s = iter.into_slice();
assert!(s == &mut other_data[1..2]);
}
}}
}

// try types of a variety of sizes
test!([(1u64, 1u64, 1u8), (2, 2, 2), (3, 3, 3)]);
test!([1u64,2,3]);
test!([1u8,2,3]);
test!([(),(),()]);
}

#[test]
fn test_iterator_nth() {
let v: &[_] = &[0, 1, 2, 3, 4];
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,11 @@ pub fn monitor<F:FnOnce()+Send+'static>(f: F) {
pub fn diagnostics_registry() -> diagnostics::registry::Registry {
use syntax::diagnostics::registry::Registry;

let all_errors = Vec::new() +
&rustc::DIAGNOSTICS[..] +
&rustc_typeck::DIAGNOSTICS[..] +
&rustc_borrowck::DIAGNOSTICS[..] +
&rustc_resolve::DIAGNOSTICS[..];
let mut all_errors = Vec::new();
all_errors.push_all(&rustc::DIAGNOSTICS);
all_errors.push_all(&rustc_typeck::DIAGNOSTICS);
all_errors.push_all(&rustc_borrowck::DIAGNOSTICS);
all_errors.push_all(&rustc_resolve::DIAGNOSTICS);

Registry::new(&*all_errors)
}
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1605,8 +1605,7 @@ impl HashState for RandomState {
}
}

#[unstable(feature = "std_misc",
reason = "hashing an hash maps may be altered")]
#[stable(feature = "rust1", since = "1.0.0")]
impl Default for RandomState {
#[inline]
fn default() -> RandomState {
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<R> fmt::Debug for BufReader<R> where R: fmt::Debug {
}
}

#[unstable(feature = "buf_seek", reason = "recently added")]
#[stable(feature = "rust1", since = "1.0.0")]
impl<R: Seek> Seek for BufReader<R> {
/// Seek to an offset, in bytes, in the underlying reader.
///
Expand Down Expand Up @@ -282,8 +282,8 @@ impl<W: Write> fmt::Debug for BufWriter<W> where W: fmt::Debug {
}
}

#[unstable(feature = "buf_seek", reason = "recently added")]
impl<W: Write+Seek> Seek for BufWriter<W> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write + Seek> Seek for BufWriter<W> {
/// Seek to the offset, in bytes, in the underlying writer.
///
/// Seeking always writes out the internal buffer before seeking.
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/net/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl<'a> Parser<'a> {
}
}

#[unstable(feature = "ip_addr", reason = "recent addition")]
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for IpAddr {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
Expand Down