Rename slicing methods

This commit is contained in:
Nick Cameron 2014-10-05 11:59:10 +13:00
parent cd21e4a72c
commit 3b0550c3a9
11 changed files with 225 additions and 24 deletions

View File

@ -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 {
@ -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"]

View File

@ -389,6 +389,7 @@ 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,
@ -396,6 +397,14 @@ impl<T> TrieMap<T> {
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.
@ -431,6 +440,7 @@ 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,
@ -438,6 +448,14 @@ impl<T> TrieMap<T> {
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"]

View File

@ -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] {
@ -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] {
@ -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> {
@ -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]

View File

@ -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
* }
@ -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[]
@ -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
* }
@ -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]

View File

@ -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};

View File

@ -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] {
@ -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] {
@ -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)
}

View File

@ -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 {
@ -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

View File

@ -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]
@ -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]

View File

@ -2272,9 +2272,9 @@ fn try_overloaded_slice(fcx: &FnCtxt,
match fcx.tcx().lang_items.slice_mut_trait() {
Some(trait_did) => {
let method_name = match (start_expr, end_expr) {
(&Some(_), &Some(_)) => "slice_mut_",
(&Some(_), &None) => "slice_from_mut_",
(&None, &Some(_)) => "slice_to_mut_",
(&Some(_), &Some(_)) => "slice_or_fail_mut",
(&Some(_), &None) => "slice_from_or_fail_mut",
(&None, &Some(_)) => "slice_to_or_fail_mut",
(&None, &None) => "as_mut_slice_",
};
@ -2297,9 +2297,9 @@ fn try_overloaded_slice(fcx: &FnCtxt,
match fcx.tcx().lang_items.slice_trait() {
Some(trait_did) => {
let method_name = match (start_expr, end_expr) {
(&Some(_), &Some(_)) => "slice_",
(&Some(_), &None) => "slice_from_",
(&None, &Some(_)) => "slice_to_",
(&Some(_), &Some(_)) => "slice_or_fail",
(&Some(_), &None) => "slice_from_or_fail",
(&None, &Some(_)) => "slice_to_or_fail",
(&None, &None) => "as_slice_",
};

View File

@ -46,6 +46,7 @@
#[doc(no_inline)] pub use ops::{Drop, Deref, DerefMut};
#[doc(no_inline)] pub use ops::{Shl, Shr};
#[doc(no_inline)] pub use ops::{Index, IndexMut};
#[doc(no_inline)] pub use ops::{Slice, SliceMut};
#[doc(no_inline)] pub use ops::{Fn, FnMut, FnOnce};
#[doc(no_inline)] pub use option::{Option, Some, None};
#[doc(no_inline)] pub use result::{Result, Ok, Err};

View File

@ -24,15 +24,15 @@ impl Slice<Foo, Foo> for Foo {
unsafe { COUNT += 1; }
self
}
fn slice_from_<'a>(&'a self, _from: &Foo) -> &'a Foo {
fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
unsafe { COUNT += 1; }
self
}
fn slice_to_<'a>(&'a self, _to: &Foo) -> &'a Foo {
fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
unsafe { COUNT += 1; }
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 {
unsafe { COUNT += 1; }
self
}
@ -43,15 +43,15 @@ impl SliceMut<Foo, Foo> for Foo {
unsafe { COUNT += 1; }
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 {
unsafe { COUNT += 1; }
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 {
unsafe { COUNT += 1; }
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 {
unsafe { COUNT += 1; }
self
}