Skip to content

generic slice/container indexing using RangeArgument #31191

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

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#![feature(alloc)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(collections_range)]
#![feature(core_intrinsics)]
#![feature(decode_utf16)]
#![feature(drop_in_place)]
Expand Down Expand Up @@ -89,7 +90,6 @@ pub mod borrow;
pub mod enum_set;
pub mod fmt;
pub mod linked_list;
pub mod range;
pub mod slice;
pub mod str;
pub mod string;
Expand Down
60 changes: 0 additions & 60 deletions src/libcollections/range.rs

This file was deleted.

63 changes: 7 additions & 56 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use core::fmt;
use core::hash;
use core::iter::FromIterator;
use core::mem;
use core::ops::{self, Add};
use core::ops::{self, Add, RangeArgument};
use core::ptr;
use core::slice;
use core::str::pattern::Pattern;
Expand All @@ -68,7 +68,6 @@ use rustc_unicode::str as unicode_str;

#[allow(deprecated)]
use borrow::{Cow, IntoCow};
use range::RangeArgument;
use str::{self, FromStr, Utf8Error, Chars};
use vec::Vec;
use boxed::Box;
Expand Down Expand Up @@ -1570,68 +1569,20 @@ impl<'a> Add<&'a str> for String {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::Range<usize>> for String {
impl<R: RangeArgument<usize>> ops::Index<R> for String {
type Output = str;

#[inline]
fn index(&self, index: ops::Range<usize>) -> &str {
&self[..][index]
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeTo<usize>> for String {
type Output = str;

#[inline]
fn index(&self, index: ops::RangeTo<usize>) -> &str {
&self[..][index]
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl ops::Index<ops::RangeFrom<usize>> for String {
type Output = str;

#[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &str {
&self[..][index]
}
}
#[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 { str::from_utf8_unchecked(&self.vec) }
fn index(&self, index: R) -> &str {
&(**self)[index]
}
}

#[stable(feature = "derefmut_for_string", since = "1.2.0")]
impl ops::IndexMut<ops::Range<usize>> for String {
#[inline]
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut str {
&mut self[..][index]
}
}
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
impl ops::IndexMut<ops::RangeTo<usize>> for String {
#[inline]
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut str {
&mut self[..][index]
}
}
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
impl ops::IndexMut<ops::RangeFrom<usize>> for String {
#[inline]
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut str {
&mut self[..][index]
}
}
#[stable(feature = "derefmut_for_string", since = "1.2.0")]
impl ops::IndexMut<ops::RangeFull> for String {
impl<R: RangeArgument<usize>> ops::IndexMut<R> for String {
#[inline]
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut str {
unsafe { mem::transmute(&mut *self.vec) }
fn index_mut(&mut self, index: R) -> &mut str {
&mut (**self)[index]
}
}

Expand Down
60 changes: 5 additions & 55 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,14 @@ use core::hash::{self, Hash};
use core::intrinsics::{arith_offset, assume, needs_drop};
use core::iter::FromIterator;
use core::mem;
use core::ops::{Index, IndexMut};
use core::ops::{Index, IndexMut, RangeArgument};
use core::ops;
use core::ptr;
use core::slice;

#[allow(deprecated)]
use borrow::{Cow, IntoCow};

use super::range::RangeArgument;

/// A growable list type, written `Vec<T>` but pronounced 'vector.'
///
/// # Examples
Expand Down Expand Up @@ -1146,70 +1144,22 @@ impl<T> IndexMut<usize> for Vec<T> {


#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::Range<usize>> for Vec<T> {
impl<T, R: RangeArgument<usize>> ops::Index<R> for Vec<T> {
type Output = [T];

#[inline]
fn index(&self, index: ops::Range<usize>) -> &[T] {
fn index(&self, index: R) -> &[T] {
Index::index(&**self, index)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeTo<usize>> for Vec<T> {
type Output = [T];

#[inline]
fn index(&self, index: ops::RangeTo<usize>) -> &[T] {
Index::index(&**self, index)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Index<ops::RangeFrom<usize>> for Vec<T> {
type Output = [T];

impl<T, R: RangeArgument<usize>> ops::IndexMut<R> for Vec<T> {
#[inline]
fn index(&self, index: ops::RangeFrom<usize>) -> &[T] {
Index::index(&**self, index)
}
}
#[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
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::Range<usize>> for Vec<T> {
#[inline]
fn index_mut(&mut self, index: ops::Range<usize>) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeTo<usize>> for Vec<T> {
#[inline]
fn index_mut(&mut self, index: ops::RangeTo<usize>) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFrom<usize>> for Vec<T> {
#[inline]
fn index_mut(&mut self, index: ops::RangeFrom<usize>) -> &mut [T] {
fn index_mut(&mut self, index: R) -> &mut [T] {
IndexMut::index_mut(&mut **self, index)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::IndexMut<ops::RangeFull> for Vec<T> {
#[inline]
fn index_mut(&mut self, _index: ops::RangeFull) -> &mut [T] {
self
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> ops::Deref for Vec<T> {
Expand Down
4 changes: 1 addition & 3 deletions src/libcollections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use core::cmp::Ordering;
use core::fmt;
use core::iter::{repeat, FromIterator};
use core::mem;
use core::ops::{Index, IndexMut};
use core::ops::{Index, IndexMut, RangeArgument};
use core::ptr;
use core::slice;

Expand All @@ -31,8 +31,6 @@ use core::cmp;

use alloc::raw_vec::RawVec;

use super::range::RangeArgument;

const INITIAL_CAPACITY: usize = 7; // 2^3 - 1
const MINIMUM_CAPACITY: usize = 1; // 2 - 1
#[cfg(target_pointer_width = "32")]
Expand Down
66 changes: 65 additions & 1 deletion src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@

#![stable(feature = "rust1", since = "1.0.0")]

use marker::{Sized, Unsize};
use fmt;
use marker::{Sized, Unsize};
use option::Option;
use option::Option::{Some, None};

/// The `Drop` trait is used to run some code when a value goes out of scope.
/// This is sometimes called a 'destructor'.
Expand Down Expand Up @@ -1885,3 +1887,65 @@ pub trait BoxPlace<Data: ?Sized> : Place<Data> {
/// Creates a globally fresh place.
fn make_place() -> Self;
}




/// **RangeArgument** is implemented by Rust's built-in range types, produced
/// by range syntax like `..`, `a..`, `..b` or `c..d`.
#[unstable(feature = "collections_range",
reason = "waiting for dust to settle on inclusive ranges",
issue = "30877")]
#[fundamental]
pub trait RangeArgument<T> {
/// Start index (inclusive)
///
/// Return start value if present, else `None`.
fn start(&self) -> Option<&T> {
None
}

/// End index (exclusive)
///
/// Return end value if present, else `None`.
fn end(&self) -> Option<&T> {
None
}
}


#[unstable(feature = "collections_range",
reason = "waiting for dust to settle on inclusive ranges",
issue = "30877")]
impl<T> RangeArgument<T> for RangeFull {}

#[unstable(feature = "collections_range",
reason = "waiting for dust to settle on inclusive ranges",
issue = "30877")]
impl<T> RangeArgument<T> for RangeFrom<T> {
fn start(&self) -> Option<&T> {
Some(&self.start)
}
}

#[unstable(feature = "collections_range",
reason = "waiting for dust to settle on inclusive ranges",
issue = "30877")]
impl<T> RangeArgument<T> for RangeTo<T> {
fn end(&self) -> Option<&T> {
Some(&self.end)
}
}

#[unstable(feature = "collections_range",
reason = "waiting for dust to settle on inclusive ranges",
issue = "30877")]
impl<T> RangeArgument<T> for Range<T> {
fn start(&self) -> Option<&T> {
Some(&self.start)
}
fn end(&self) -> Option<&T> {
Some(&self.end)
}
}

Loading