Skip to content

Commit

Permalink
Rename slicing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Oct 7, 2014
1 parent cd21e4a commit 3b0550c
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 24 deletions.
23 changes: 23 additions & 0 deletions src/libcollections/string.rs
Expand Up @@ -928,6 +928,7 @@ impl<S: Str> Add<S, String> for String {
}
}

#[cfg(stage0)]
impl ops::Slice<uint, str> for String {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a str {
Expand All @@ -949,6 +950,28 @@ impl ops::Slice<uint, str> for String {
self[][*from..*to]
}
}
#[cfg(not(stage0))]
impl ops::Slice<uint, str> for String {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a str {
self.as_slice()
}

#[inline]
fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
self[][*from..]
}

#[inline]
fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
self[][..*to]
}

#[inline]
fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
self[][*from..*to]
}
}

/// Unsafe operations
#[unstable = "waiting on raw module conventions"]
Expand Down
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_or_fail, 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_or_fail_mut, iter = iter_mut,
mutability = mut)
}

/// Deprecated: use `lower_bound_mut`.
#[deprecated = "use lower_bound_mut"]
Expand Down
46 changes: 45 additions & 1 deletion src/libcollections/vec.rs
Expand Up @@ -460,6 +460,7 @@ impl<T> Index<uint,T> for Vec<T> {
}
}*/

#[cfg(stage0)]
impl<T> ops::Slice<uint, [T]> for Vec<T> {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a [T] {
Expand All @@ -480,7 +481,29 @@ impl<T> ops::Slice<uint, [T]> for Vec<T> {
self.as_slice().slice_(start, end)
}
}
#[cfg(not(stage0))]
impl<T> ops::Slice<uint, [T]> for Vec<T> {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a [T] {
self.as_slice()
}

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

#[inline]
fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
self.as_slice().slice_to_or_fail(end)
}
#[inline]
fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
self.as_slice().slice_or_fail(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 All @@ -501,6 +524,27 @@ impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
self.as_mut_slice().slice_mut_(start, end)
}
}
#[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_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
self.as_mut_slice().slice_from_or_fail_mut(start)
}

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

#[experimental = "waiting on FromIterator stability"]
impl<T> FromIterator<T> for Vec<T> {
Expand Down Expand Up @@ -1181,7 +1225,7 @@ impl<T> Vec<T> {
}

/// Deprecated: use `slice_mut`.
#[deprecated = "use slice_from"]
#[deprecated = "use slice_mut"]
pub fn mut_slice<'a>(&'a mut self, start: uint, end: uint)
-> &'a mut [T] {
self[mut start..end]
Expand Down
46 changes: 38 additions & 8 deletions src/libcore/ops.rs
Expand Up @@ -692,15 +692,15 @@ pub trait IndexMut<Index, Result> {
* println!("Slicing!");
* self
* }
* fn slice_from_<'a>(&'a self, from: &Foo) -> &'a Foo {
* fn slice_from_or_fail<'a>(&'a self, from: &Foo) -> &'a Foo {
* println!("Slicing!");
* self
* }
* fn slice_to_<'a>(&'a self, to: &Foo) -> &'a Foo {
* fn slice_to_or_fail<'a>(&'a self, to: &Foo) -> &'a Foo {
* println!("Slicing!");
* self
* }
* fn slice_<'a>(&'a self, from: &Foo, to: &Foo) -> &'a Foo {
* fn slice_or_fail<'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_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
/// The method for the slicing operation foo[..to]
fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
/// The method for the slicing operation foo[from..to]
fn slice_or_fail<'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 Down Expand Up @@ -742,15 +757,15 @@ pub trait Slice<Idx, Sized? Result> for Sized? {
* println!("Slicing!");
* self
* }
* fn slice_from_mut_<'a>(&'a mut self, from: &Foo) -> &'a mut Foo {
* fn slice_from_or_fail_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_or_fail_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_or_fail_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_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
/// The method for the slicing operation foo[..to]
fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
/// The method for the slicing operation foo[from..to]
fn slice_or_fail_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 Down
1 change: 1 addition & 0 deletions src/libcore/prelude.rs
Expand Up @@ -35,6 +35,7 @@ pub use ops::{BitAnd, BitOr, BitXor};
pub use ops::{Drop, Deref, DerefMut};
pub use ops::{Shl, Shr};
pub use ops::{Index, IndexMut};
pub use ops::{Slice, SliceMut};
pub use ops::{Fn, FnMut, FnOnce};
pub use option::{Option, Some, None};
pub use result::{Result, Ok, Err};
Expand Down
63 changes: 62 additions & 1 deletion src/libcore/slice.rs
Expand Up @@ -486,6 +486,37 @@ 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_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
self.slice_or_fail(start, &self.len())
}

#[inline]
fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
self.slice_or_fail(&0, end)
}
#[inline]
fn slice_or_fail<'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 @@ -514,6 +545,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_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
let len = &self.len();
self.slice_or_fail_mut(start, len)
}

#[inline]
fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
self.slice_or_fail_mut(&0, end)
}
#[inline]
fn slice_or_fail_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 @@ -556,7 +617,7 @@ pub trait MutableSlice<'a, T> {
fn as_mut_slice(self) -> &'a mut [T];

/// Deprecated: use `slice_mut`.
#[deprecated = "slice_mut"]
#[deprecated = "use slice_mut"]
fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] {
self.slice_mut(start, end)
}
Expand Down
23 changes: 23 additions & 0 deletions src/libcore/str.rs
Expand Up @@ -1164,6 +1164,7 @@ pub mod traits {
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
}

#[cfg(stage0)]
impl ops::Slice<uint, str> for str {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a str {
Expand All @@ -1185,6 +1186,28 @@ pub mod traits {
self.slice(*from, *to)
}
}
#[cfg(not(stage0))]
impl ops::Slice<uint, str> for str {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a str {
self
}

#[inline]
fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
self.slice_from(*from)
}

#[inline]
fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
self.slice_to(*to)
}

#[inline]
fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
self.slice(*from, *to)
}
}
}

/// Any string that can be represented as a slice
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_or_fail(&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_or_fail(&0, &0).iter().any(|_| fail!()));
}

#[test]
Expand Down

0 comments on commit 3b0550c

Please sign in to comment.