Skip to content

Commit

Permalink
Remove the _ suffix from slice methods.
Browse files Browse the repository at this point in the history
Deprecates slicing methods from ImmutableSlice/MutableSlice in favour of slicing syntax or the methods in Slice/SliceMut.

Closes #17273.
  • Loading branch information
nrc committed Oct 2, 2014
1 parent 40b9f5d commit df2f1fa
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 97 deletions.
18 changes: 18 additions & 0 deletions src/libcollections/trie.rs
Expand Up @@ -389,13 +389,22 @@ macro_rules! bound {

impl<T> TrieMap<T> {
// If `upper` is true then returns upper_bound else returns lower_bound.
#[cfg(stage0)]
#[inline]
fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> {
bound!(Entries, self = self,
key = key, is_upper = upper,
slice_from = slice_from_, iter = iter,
mutability = )
}
#[cfg(not(stage0))]
#[inline]
fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> {
bound!(Entries, self = self,
key = key, is_upper = upper,
slice_from = slice_from, iter = iter,
mutability = )
}

/// Gets an iterator pointing to the first key-value pair whose key is not less than `key`.
/// If all keys in the map are less than `key` an empty iterator is returned.
Expand Down Expand Up @@ -431,13 +440,22 @@ impl<T> TrieMap<T> {
self.bound(key, true)
}
// If `upper` is true then returns upper_bound else returns lower_bound.
#[cfg(stage0)]
#[inline]
fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> {
bound!(MutEntries, self = self,
key = key, is_upper = upper,
slice_from = slice_from_mut_, iter = iter_mut,
mutability = mut)
}
#[cfg(not(stage0))]
#[inline]
fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> {
bound!(MutEntries, self = self,
key = key, is_upper = upper,
slice_from = slice_from_mut, iter = iter_mut,
mutability = mut)
}

/// Deprecated: use `lower_bound_mut`.
#[deprecated = "use lower_bound_mut"]
Expand Down
53 changes: 53 additions & 0 deletions src/libcollections/vec.rs
Expand Up @@ -23,6 +23,7 @@ use core::num;
use core::ops;
use core::ptr;
use core::raw::Slice as RawSlice;
use core::slice::Slice as SliceSlice;
use core::uint;

use {Mutable, MutableSeq};
Expand Down Expand Up @@ -459,6 +460,36 @@ impl<T> Index<uint,T> for Vec<T> {
}
}*/

// Annoying helper function because there are two Slice::as_slice functions in
// scope.
#[inline]
fn slice_to_slice<'a, T, U: Slice<T>>(this: &'a U) -> &'a [T] {
this.as_slice()
}


#[cfg(not(stage0))]
impl<T> ops::Slice<uint, [T]> for Vec<T> {
#[inline]
fn as_slice<'a>(&'a self) -> &'a [T] {
slice_to_slice(self)
}

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

#[inline]
fn slice_to<'a>(&'a self, end: &uint) -> &'a [T] {
slice_to_slice(self).slice_to(end)
}
#[inline]
fn slice<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
slice_to_slice(self).slice(start, end)
}
}
#[cfg(stage0)]
impl<T> ops::Slice<uint, [T]> for Vec<T> {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a [T] {
Expand All @@ -480,6 +511,28 @@ impl<T> ops::Slice<uint, [T]> for Vec<T> {
}
}

#[cfg(not(stage0))]
impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
#[inline]
fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
self.as_mut_slice()
}

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

#[inline]
fn slice_to_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
self.as_mut_slice().slice_to_mut(end)
}
#[inline]
fn slice_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
self.as_mut_slice().slice_mut(start, end)
}
}
#[cfg(stage0)]
impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
#[inline]
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
Expand Down
51 changes: 41 additions & 10 deletions src/libcore/ops.rs
Expand Up @@ -688,19 +688,19 @@ pub trait IndexMut<Index, Result> {
* struct Foo;
*
* impl ::core::ops::Slice<Foo, Foo> for Foo {
* fn as_slice_<'a>(&'a self) -> &'a Foo {
* fn as_slice<'a>(&'a self) -> &'a Foo {
* println!("Slicing!");
* self
* }
* fn slice_from_<'a>(&'a self, from: &Foo) -> &'a Foo {
* fn slice_from<'a>(&'a self, from: &Foo) -> &'a Foo {
* println!("Slicing!");
* self
* }
* fn slice_to_<'a>(&'a self, to: &Foo) -> &'a Foo {
* fn slice_to<'a>(&'a self, to: &Foo) -> &'a Foo {
* println!("Slicing!");
* self
* }
* fn slice_<'a>(&'a self, from: &Foo, to: &Foo) -> &'a Foo {
* fn slice<'a>(&'a self, from: &Foo, to: &Foo) -> &'a Foo {
* println!("Slicing!");
* self
* }
Expand All @@ -711,7 +711,22 @@ pub trait IndexMut<Index, Result> {
* }
* ```
*/
// FIXME(#17273) remove the postscript _s
#[cfg(not(stage0))]
#[lang="slice"]
pub trait Slice<Idx, Sized? Result> for Sized? {
/// The method for the slicing operation foo[]
fn as_slice<'a>(&'a self) -> &'a Result;
/// The method for the slicing operation foo[from..]
fn slice_from<'a>(&'a self, from: &Idx) -> &'a Result;
/// The method for the slicing operation foo[..to]
fn slice_to<'a>(&'a self, to: &Idx) -> &'a Result;
/// The method for the slicing operation foo[from..to]
fn slice<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
}
/**
*
*/
#[cfg(stage0)]
#[lang="slice"]
pub trait Slice<Idx, Sized? Result> for Sized? {
/// The method for the slicing operation foo[]
Expand All @@ -738,19 +753,19 @@ pub trait Slice<Idx, Sized? Result> for Sized? {
* struct Foo;
*
* impl ::core::ops::SliceMut<Foo, Foo> for Foo {
* fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
* fn as_mut_slice<'a>(&'a mut self) -> &'a mut Foo {
* println!("Slicing!");
* self
* }
* fn slice_from_mut_<'a>(&'a mut self, from: &Foo) -> &'a mut Foo {
* fn slice_from_mut<'a>(&'a mut self, from: &Foo) -> &'a mut Foo {
* println!("Slicing!");
* self
* }
* fn slice_to_mut_<'a>(&'a mut self, to: &Foo) -> &'a mut Foo {
* fn slice_to_mut<'a>(&'a mut self, to: &Foo) -> &'a mut Foo {
* println!("Slicing!");
* self
* }
* fn slice_mut_<'a>(&'a mut self, from: &Foo, to: &Foo) -> &'a mut Foo {
* fn slice_mut<'a>(&'a mut self, from: &Foo, to: &Foo) -> &'a mut Foo {
* println!("Slicing!");
* self
* }
Expand All @@ -761,7 +776,22 @@ pub trait Slice<Idx, Sized? Result> for Sized? {
* }
* ```
*/
// FIXME(#17273) remove the postscript _s
#[cfg(not(stage0))]
#[lang="slice_mut"]
pub trait SliceMut<Idx, Sized? Result> for Sized? {
/// The method for the slicing operation foo[]
fn as_mut_slice<'a>(&'a mut self) -> &'a mut Result;
/// The method for the slicing operation foo[from..]
fn slice_from_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
/// The method for the slicing operation foo[..to]
fn slice_to_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
/// The method for the slicing operation foo[from..to]
fn slice_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
}
/**
*
*/
#[cfg(stage0)]
#[lang="slice_mut"]
pub trait SliceMut<Idx, Sized? Result> for Sized? {
/// The method for the slicing operation foo[mut]
Expand All @@ -773,6 +803,7 @@ pub trait SliceMut<Idx, Sized? Result> for Sized? {
/// The method for the slicing operation foo[mut from..to]
fn slice_mut_<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
}

/**
*
* The `Deref` trait is used to specify the functionality of dereferencing
Expand Down
127 changes: 58 additions & 69 deletions src/libcore/slice.rs
Expand Up @@ -61,32 +61,6 @@ use raw::Slice as RawSlice;
/// Extension methods for immutable slices.
#[unstable = "may merge with other traits; region parameter may disappear"]
pub trait ImmutableSlice<'a, T> {
/// Returns a subslice spanning the interval [`start`, `end`).
///
/// Fails 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 = "waiting on final error conventions"]
//fn slice(&self, start: uint, end: uint) -> &'a [T];

/// Returns a subslice from `start` to the end of the slice.
///
/// Fails when `start` is strictly greater than the length of the original slice.
///
/// Slicing from `self.len()` yields an empty slice.
#[unstable = "waiting on final error conventions"]
// TODO
//fn slice_from(&self, start: uint) -> &'a [T];

/// Returns a subslice from the start of the slice to `end`.
///
/// Fails when `end` is strictly greater than the length of the original slice.
///
/// Slicing to `0` yields an empty slice.
#[unstable = "waiting on final error conventions"]
//fn slice_to(&self, end: uint) -> &'a [T];

/// Divides one slice into two at an index.
///
/// The first will contain all indices from `[0, mid)` (excluding
Expand Down Expand Up @@ -444,6 +418,35 @@ impl<'a,T> ImmutableSlice<'a, T> for &'a [T] {
}
}

#[cfg(not(stage0))]
impl<T> ops::Slice<uint, [T]> for [T] {
#[inline]
fn as_slice<'a>(&'a self) -> &'a [T] {
self
}

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

#[inline]
fn slice_to<'a>(&'a self, end: &uint) -> &'a [T] {
self.slice(&0, end)
}
#[inline]
fn slice<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
assert!(*start <= *end);
assert!(*end <= self.len());
unsafe {
transmute(RawSlice {
data: self.as_ptr().offset(*start as int),
len: (*end - *start)
})
}
}
}
#[cfg(stage0)]
impl<T> ops::Slice<uint, [T]> for [T] {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a [T] {
Expand Down Expand Up @@ -471,7 +474,36 @@ impl<T> ops::Slice<uint, [T]> for [T] {
}
}
}
#[cfg(not(stage0))]
impl<T> ops::SliceMut<uint, [T]> for [T] {
#[inline]
fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
self
}

#[inline]
fn slice_from_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
let len = &self.len();
self.slice_mut(start, len)
}

#[inline]
fn slice_to_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
self.slice_mut(&0, end)
}
#[inline]
fn slice_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
assert!(*start <= *end);
assert!(*end <= self.len());
unsafe {
transmute(RawSlice {
data: self.as_ptr().offset(*start as int),
len: (*end - *start)
})
}
}
}
#[cfg(stage0)]
impl<T> ops::SliceMut<uint, [T]> for [T] {
#[inline]
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
Expand Down Expand Up @@ -514,49 +546,6 @@ pub trait MutableSlice<'a, T> {
#[deprecated = "use slicing syntax"]
fn as_mut_slice(self) -> &'a mut [T];

/// Deprecated: use `slice_mut`.
#[deprecated = "use slicing syntax"]
//fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] {
// self[mut start..end]
//}

/// Returns a mutable subslice spanning the interval [`start`, `end`).
///
/// Fails 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 = "waiting on final error conventions"]
//fn slice_mut(self, start: uint, end: uint) -> &'a mut [T];

/// Deprecated: use `slicing syntax`.
#[deprecated = "use slicing syntax"]
//fn mut_slice_from(self, start: uint) -> &'a mut [T] {
// self[mut start..]
//}

/// Returns a mutable subslice from `start` to the end of the slice.
///
/// Fails when `start` is strictly greater than the length of the original slice.
///
/// Slicing from `self.len()` yields an empty slice.
#[unstable = "waiting on final error conventions"]
//fn slice_from_mut(self, start: uint) -> &'a mut [T];

/// Deprecated: use `slicing syntax`.
#[deprecated = "use slicing syntax"]
//fn mut_slice_to(self, end: uint) -> &'a mut [T] {
// self[mut ..end]
//}

/// Returns a mutable subslice from the start of the slice to `end`.
///
/// Fails when `end` is strictly greater than the length of the original slice.
///
/// Slicing to `0` yields an empty slice.
#[unstable = "waiting on final error conventions"]
//fn slice_to_mut(self, end: uint) -> &'a mut [T];

/// Deprecated: use `iter_mut`.
#[deprecated = "use iter_mut"]
fn mut_iter(self) -> MutItems<'a, T> {
Expand Down
4 changes: 2 additions & 2 deletions src/libcoretest/iter.rs
Expand Up @@ -373,7 +373,7 @@ fn test_all() {
assert!(v.iter().all(|&x| x < 10));
assert!(!v.iter().all(|&x| x % 2 == 0));
assert!(!v.iter().all(|&x| x > 100));
assert!(v.slice_(&0, &0).iter().all(|_| fail!()));
assert!(v.slice(&0, &0).iter().all(|_| fail!()));
}

#[test]
Expand All @@ -382,7 +382,7 @@ fn test_any() {
assert!(v.iter().any(|&x| x < 10));
assert!(v.iter().any(|&x| x % 2 == 0));
assert!(!v.iter().any(|&x| x > 100));
assert!(!v.slice_(&0, &0).iter().any(|_| fail!()));
assert!(!v.slice(&0, &0).iter().any(|_| fail!()));
}

#[test]
Expand Down

0 comments on commit df2f1fa

Please sign in to comment.