Skip to content

Stabilize Index traits and most range notation #21258

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

Merged
merged 5 commits into from
Jan 22, 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
3 changes: 1 addition & 2 deletions src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ pub fn parse_name_value_directive(line: &str, directive: &str)
let keycolon = format!("{}:", directive);
match line.find_str(keycolon.as_slice()) {
Some(colon) => {
let value = line.slice(colon + keycolon.len(),
line.len()).to_string();
let value = line[(colon + keycolon.len()) .. line.len()].to_string();
debug!("{}: {}", directive, value);
Some(value)
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ fn check_debugger_output(debugger_run_result: &ProcRes, check_lines: &[String])
break;
}
Some(i) => {
rest = rest.slice_from(i + frag.len());
rest = &rest[(i + frag.len())..];
}
}
first = false;
Expand Down Expand Up @@ -1045,7 +1045,7 @@ fn scan_until_char(haystack: &str, needle: char, idx: &mut uint) -> bool {
if *idx >= haystack.len() {
return false;
}
let opt = haystack.slice_from(*idx).find(needle);
let opt = haystack[(*idx)..].find(needle);
if opt.is_none() {
return false;
}
Expand Down
8 changes: 3 additions & 5 deletions src/doc/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,12 @@ use std::sync::{Arc,Mutex};
fn main() {
let numbers = Arc::new(Mutex::new(vec![1is, 2, 3]));

for i in 0..3 {
for i in 0us..3 {
let number = numbers.clone();
Thread::spawn(move || {
let mut array = number.lock().unwrap();

(*array)[i] += 1;

println!("numbers[{}] is {}", i, (*array)[i]);
array[i] += 1;
println!("numbers[{}] is {}", i, array[i]);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/doc/trpl/looping.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ We now loop forever with `loop` and use `break` to break out early.
iteration. This will only print the odd numbers:

```{rust}
for x in 0..10 {
for x in 0u32..10 {
if x % 2 == 0 { continue; }

println!("{}", x);
Expand Down
4 changes: 2 additions & 2 deletions src/doc/trpl/threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ for init_val in 0 .. 3 {
}

let result = rx.recv().unwrap() + rx.recv().unwrap() + rx.recv().unwrap();
# fn some_expensive_computation(_i: u32) -> u32 { 42 }
# fn some_expensive_computation(_i: i32) -> i32 { 42 }
```

Cloning a `Sender` produces a new handle to the same channel, allowing multiple
Expand Down Expand Up @@ -207,7 +207,7 @@ let rxs = (0 .. 3).map(|&:init_val| {

// Wait on each port, accumulating the results
let result = rxs.iter().fold(0, |&:accum, rx| accum + rx.recv().unwrap() );
# fn some_expensive_computation(_i: u32) -> u32 { 42 }
# fn some_expensive_computation(_i: i32) -> i32 { 42 }
```

## Backgrounding computations: Futures
Expand Down
21 changes: 10 additions & 11 deletions src/libcollections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use core::prelude::*;
use core::borrow::BorrowFrom;
use core::cmp::Ordering::{Greater, Less, Equal};
use core::iter::Zip;
use core::ops::{Deref, DerefMut};
use core::ops::{Deref, DerefMut, Index, IndexMut};
use core::ptr::Unique;
use core::{slice, mem, ptr, cmp, num, raw};
use alloc::heap;
Expand Down Expand Up @@ -1487,7 +1487,7 @@ impl<K, V, E, Impl> AbsTraversal<Impl>

macro_rules! node_slice_impl {
($NodeSlice:ident, $Traversal:ident,
$as_slices_internal:ident, $slice_from:ident, $slice_to:ident, $iter:ident) => {
$as_slices_internal:ident, $index:ident, $iter:ident) => {
impl<'a, K: Ord + 'a, V: 'a> $NodeSlice<'a, K, V> {
/// Performs linear search in a slice. Returns a tuple of (index, is_exact_match).
fn search_linear<Q: ?Sized>(&self, key: &Q) -> (uint, bool)
Expand Down Expand Up @@ -1521,10 +1521,10 @@ macro_rules! node_slice_impl {
edges: if !self.has_edges {
self.edges
} else {
self.edges.$slice_from(pos)
self.edges.$index(&(pos ..))
},
keys: self.keys.slice_from(pos),
vals: self.vals.$slice_from(pos),
keys: &self.keys[pos ..],
vals: self.vals.$index(&(pos ..)),
head_is_edge: !pos_is_kv,
tail_is_edge: self.tail_is_edge,
}
Expand All @@ -1550,10 +1550,10 @@ macro_rules! node_slice_impl {
edges: if !self.has_edges {
self.edges
} else {
self.edges.$slice_to(pos + 1)
self.edges.$index(&(.. (pos + 1)))
},
keys: self.keys.slice_to(pos),
vals: self.vals.$slice_to(pos),
keys: &self.keys[..pos],
vals: self.vals.$index(&(.. pos)),
head_is_edge: self.head_is_edge,
tail_is_edge: !pos_is_kv,
}
Expand Down Expand Up @@ -1583,6 +1583,5 @@ macro_rules! node_slice_impl {
}
}

node_slice_impl!(NodeSlice, Traversal, as_slices_internal, slice_from, slice_to, iter);
node_slice_impl!(MutNodeSlice, MutTraversal, as_slices_internal_mut, slice_from_mut,
slice_to_mut, iter_mut);
node_slice_impl!(NodeSlice, Traversal, as_slices_internal, index, iter);
node_slice_impl!(MutNodeSlice, MutTraversal, as_slices_internal_mut, index_mut, iter_mut);
2 changes: 1 addition & 1 deletion src/libcollections/ring_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl<T> RingBuf<T> {

if contiguous {
let (empty, buf) = buf.split_at_mut(0);
(buf.slice_mut(tail, head), empty)
(&mut buf[tail .. head], empty)
} else {
let (mid, right) = buf.split_at_mut(tail);
let (left, _) = mid.split_at_mut(head);
Expand Down
64 changes: 19 additions & 45 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,29 +169,16 @@ pub trait SliceExt {
#[unstable = "uncertain about this API approach"]
fn move_from(&mut self, src: Vec<Self::Item>, start: uint, end: uint) -> uint;

/// Returns a subslice spanning the interval [`start`, `end`).
///
/// Panics when the end of the new slice lies beyond the end of the
/// original slice (i.e. when `end > self.len()`) or when `start > end`.
///
/// Slicing with `start` equal to `end` yields an empty slice.
#[unstable = "will be replaced by slice syntax"]
/// Deprecated: use `&s[start .. end]` notation instead.
#[deprecated = "use &s[start .. end] instead"]
fn slice(&self, start: uint, end: uint) -> &[Self::Item];

/// Returns a subslice from `start` to the end of the slice.
///
/// Panics when `start` is strictly greater than the length of the original slice.
///
/// Slicing from `self.len()` yields an empty slice.
#[unstable = "will be replaced by slice syntax"]
/// Deprecated: use `&s[start..]` notation instead.
#[deprecated = "use &s[start..] isntead"]
fn slice_from(&self, start: uint) -> &[Self::Item];

/// Returns a subslice from the start of the slice to `end`.
///
/// Panics when `end` is strictly greater than the length of the original slice.
///
/// Slicing to `0` yields an empty slice.
#[unstable = "will be replaced by slice syntax"]
/// Deprecated: use `&s[..end]` notation instead.
#[deprecated = "use &s[..end] instead"]
fn slice_to(&self, end: uint) -> &[Self::Item];

/// Divides one slice into two at an index.
Expand Down Expand Up @@ -378,29 +365,16 @@ pub trait SliceExt {
#[stable]
fn as_mut_slice(&mut self) -> &mut [Self::Item];

/// Returns a mutable subslice spanning the interval [`start`, `end`).
///
/// Panics when the end of the new slice lies beyond the end of the
/// original slice (i.e. when `end > self.len()`) or when `start > end`.
///
/// Slicing with `start` equal to `end` yields an empty slice.
#[unstable = "will be replaced by slice syntax"]
/// Deprecated: use `&mut s[start .. end]` instead.
#[deprecated = "use &mut s[start .. end] instead"]
fn slice_mut(&mut self, start: uint, end: uint) -> &mut [Self::Item];

/// Returns a mutable subslice from `start` to the end of the slice.
///
/// Panics when `start` is strictly greater than the length of the original slice.
///
/// Slicing from `self.len()` yields an empty slice.
#[unstable = "will be replaced by slice syntax"]
/// Deprecated: use `&mut s[start ..]` instead.
#[deprecated = "use &mut s[start ..] instead"]
fn slice_from_mut(&mut self, start: uint) -> &mut [Self::Item];

/// Returns a mutable subslice from the start of the slice to `end`.
///
/// Panics when `end` is strictly greater than the length of the original slice.
///
/// Slicing to `0` yields an empty slice.
#[unstable = "will be replaced by slice syntax"]
/// Deprecated: use `&mut s[.. end]` instead.
#[deprecated = "use &mut s[.. end] instead"]
fn slice_to_mut(&mut self, end: uint) -> &mut [Self::Item];

/// Returns an iterator that allows modifying each value
Expand Down Expand Up @@ -712,25 +686,25 @@ impl<T> SliceExt for [T] {

#[inline]
fn move_from(&mut self, mut src: Vec<T>, start: uint, end: uint) -> uint {
for (a, b) in self.iter_mut().zip(src.slice_mut(start, end).iter_mut()) {
for (a, b) in self.iter_mut().zip(src[start .. end].iter_mut()) {
mem::swap(a, b);
}
cmp::min(self.len(), end-start)
}

#[inline]
fn slice<'a>(&'a self, start: uint, end: uint) -> &'a [T] {
core_slice::SliceExt::slice(self, start, end)
&self[start .. end]
}

#[inline]
fn slice_from<'a>(&'a self, start: uint) -> &'a [T] {
core_slice::SliceExt::slice_from(self, start)
&self[start ..]
}

#[inline]
fn slice_to<'a>(&'a self, end: uint) -> &'a [T] {
core_slice::SliceExt::slice_to(self, end)
&self[.. end]
}

#[inline]
Expand Down Expand Up @@ -834,17 +808,17 @@ impl<T> SliceExt for [T] {

#[inline]
fn slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T] {
core_slice::SliceExt::slice_mut(self, start, end)
&mut self[start .. end]
}

#[inline]
fn slice_from_mut<'a>(&'a mut self, start: uint) -> &'a mut [T] {
core_slice::SliceExt::slice_from_mut(self, start)
&mut self[start ..]
}

#[inline]
fn slice_to_mut<'a>(&'a mut self, end: uint) -> &'a mut [T] {
core_slice::SliceExt::slice_to_mut(self, end)
&mut self[.. end]
}

#[inline]
Expand Down
82 changes: 22 additions & 60 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,67 +750,17 @@ pub trait StrExt: Index<FullRange, Output = str> {
core_str::StrExt::lines_any(&self[])
}

/// Returns a slice of the given string from the byte range
/// [`begin`..`end`).
///
/// This operation is `O(1)`.
///
/// Panics when `begin` and `end` do not point to valid characters
/// or point beyond the last character of the string.
///
/// See also `slice_to` and `slice_from` for slicing prefixes and
/// suffixes of strings, and `slice_chars` for slicing based on
/// code point counts.
///
/// # Example
///
/// ```rust
/// let s = "Löwe 老虎 Léopard";
/// assert_eq!(s.slice(0, 1), "L");
///
/// assert_eq!(s.slice(1, 9), "öwe 老");
///
/// // these will panic:
/// // byte 2 lies within `ö`:
/// // s.slice(2, 3);
///
/// // byte 8 lies within `老`
/// // s.slice(1, 8);
///
/// // byte 100 is outside the string
/// // s.slice(3, 100);
/// ```
#[unstable = "use slice notation [a..b] instead"]
fn slice(&self, begin: uint, end: uint) -> &str {
core_str::StrExt::slice(&self[], begin, end)
}
/// Deprecated: use `s[a .. b]` instead.
#[deprecated = "use slice notation [a..b] instead"]
fn slice(&self, begin: uint, end: uint) -> &str;

/// Returns a slice of the string from `begin` to its end.
///
/// Equivalent to `self.slice(begin, self.len())`.
///
/// Panics when `begin` does not point to a valid character, or is
/// out of bounds.
///
/// See also `slice`, `slice_to` and `slice_chars`.
#[unstable = "use slice notation [a..] instead"]
fn slice_from(&self, begin: uint) -> &str {
core_str::StrExt::slice_from(&self[], begin)
}
/// Deprecated: use `s[a..]` instead.
#[deprecated = "use slice notation [a..] instead"]
fn slice_from(&self, begin: uint) -> &str;

/// Returns a slice of the string from the beginning to byte
/// `end`.
///
/// Equivalent to `self.slice(0, end)`.
///
/// Panics when `end` does not point to a valid character, or is
/// out of bounds.
///
/// See also `slice`, `slice_from` and `slice_chars`.
#[unstable = "use slice notation [..a] instead"]
fn slice_to(&self, end: uint) -> &str {
core_str::StrExt::slice_to(&self[], end)
}
/// Deprecated: use `s[..a]` instead.
#[deprecated = "use slice notation [..a] instead"]
fn slice_to(&self, end: uint) -> &str;

/// Returns a slice of the string from the character range
/// [`begin`..`end`).
Expand Down Expand Up @@ -1348,7 +1298,19 @@ pub trait StrExt: Index<FullRange, Output = str> {
}

#[stable]
impl StrExt for str {}
impl StrExt for str {
fn slice(&self, begin: uint, end: uint) -> &str {
&self[begin..end]
}

fn slice_from(&self, begin: uint) -> &str {
&self[begin..]
}

fn slice_to(&self, end: uint) -> &str {
&self[..end]
}
}

#[cfg(test)]
mod tests {
Expand Down
4 changes: 4 additions & 0 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,27 +848,31 @@ impl<'a> Add<&'a str> for String {
}
}

#[stable]
impl ops::Index<ops::Range<uint>> for String {
type Output = str;
#[inline]
fn index(&self, index: &ops::Range<uint>) -> &str {
&self[][*index]
}
}
#[stable]
impl ops::Index<ops::RangeTo<uint>> for String {
type Output = str;
#[inline]
fn index(&self, index: &ops::RangeTo<uint>) -> &str {
&self[][*index]
}
}
#[stable]
impl ops::Index<ops::RangeFrom<uint>> for String {
type Output = str;
#[inline]
fn index(&self, index: &ops::RangeFrom<uint>) -> &str {
&self[][*index]
}
}
#[stable]
impl ops::Index<ops::FullRange> for String {
type Output = str;
#[inline]
Expand Down
Loading