Impls using the new scheme for slicing

This commit is contained in:
Nick Cameron 2014-12-31 20:20:40 +13:00
parent 6539cb417f
commit 918255ef8c
6 changed files with 243 additions and 255 deletions

View File

@ -818,26 +818,32 @@ impl<'a> Add<&'a str> for String {
}
}
impl ops::Slice<uint, str> for String {
impl<T> ops::Index<ops::Range<uint>, str> for String {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a str {
fn index(&self, &index: &ops::Range<uint>) -> &str {
self[][*index]
}
}
impl<T> ops::Index<ops::RangeTo<uint>, str> for String {
#[inline]
fn index(&self, &index: &ops::RangeTo<uint>) -> &str {
self[][*index]
}
}
impl<T> ops::Index<ops::RangeFrom<uint>, str> for String {
#[inline]
fn index(&self, &index: &ops::RangeFrom<uint>) -> &str {
self[][*index]
}
}
impl<T> ops::Index<ops::FullRange<uint>, str> for String {
#[inline]
fn index(&self, &index: &ops::FullRange<uint>) -> &str {
unsafe { mem::transmute(self.vec.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]
}
}
#[stable]

View File

@ -1211,44 +1211,65 @@ impl<T> IndexMut<uint> for Vec<T> {
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)
fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
&mut self.as_mut_slice()[*index]
}
}
impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
impl<T> ops::Index<ops::Range<uint>, [T]> for Vec<T> {
#[inline]
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
fn index(&self, &index: &ops::Range<uint>) -> &[T] {
self.as_slice().index(index)
}
}
impl<T> ops::Index<ops::RangeTo<uint>, [T]> for Vec<T> {
#[inline]
fn index(&self, &index: &ops::RangeTo<uint>) -> &[T] {
self.as_slice().index(index)
}
}
impl<T> ops::Index<ops::RangeFrom<uint>, [T]> for Vec<T> {
#[inline]
fn index(&self, &index: &ops::RangeFrom<uint>) -> &[T] {
self.as_slice().index(index)
}
}
impl<T> ops::Index<ops::FullRange<uint>, [T]> for Vec<T> {
#[inline]
fn index(&self, &index: &ops::FullRange<uint>) -> &[T] {
self.as_slice()
}
}
impl<T> ops::IndexMut<ops::Range<uint>, [T]> for Vec<T> {
#[inline]
fn index_mut(&mut self, &index: &ops::Range<uint>) -> &mut [T] {
self.as_mut_slice().index_mut(index)
}
}
impl<T> ops::IndexMut<ops::RangeTo<uint>, [T]> for Vec<T> {
#[inline]
fn index_mut(&mut self, &index: &ops::RangeTo<uint>) -> &mut [T] {
self.as_mut_slice().index_mut(index)
}
}
impl<T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for Vec<T> {
#[inline]
fn index_mut(&mut self, &index: &ops::RangeFrom<uint>) -> &mut [T] {
self.as_mut_slice().index_mut(index)
}
}
impl<T> ops::IndexMut<ops::FullRange<uint>, [T]> for Vec<T> {
#[inline]
fn index_mut(&mut self, &index: &ops::FullRange<uint>) -> &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)
}
}
#[stable]

View File

@ -846,105 +846,6 @@ pub trait IndexMut<Index: ?Sized> {
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output;
}
/// The `Slice` trait is used to specify the functionality of slicing operations
/// like `arr[from..to]` when used in an immutable context.
///
/// # Example
///
/// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
/// calling `slice_to`, and therefore, `main` prints `Slicing!`.
///
/// ```ignore
/// use std::ops::Slice;
///
/// #[derive(Copy)]
/// struct Foo;
///
/// impl Slice<Foo, Foo> for Foo {
/// fn as_slice_<'a>(&'a self) -> &'a Foo {
/// println!("Slicing!");
/// self
/// }
/// fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
/// println!("Slicing!");
/// self
/// }
/// fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
/// println!("Slicing!");
/// self
/// }
/// fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
/// println!("Slicing!");
/// self
/// }
/// }
///
/// fn main() {
/// Foo[..Foo];
/// }
/// ```
#[lang="slice"]
pub trait Slice<Idx: ?Sized, Result: ?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;
}
/// The `SliceMut` trait is used to specify the functionality of slicing
/// operations like `arr[from..to]`, when used in a mutable context.
///
/// # Example
///
/// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
/// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
///
/// ```ignore
/// use std::ops::SliceMut;
///
/// #[derive(Copy)]
/// struct Foo;
///
/// impl SliceMut<Foo, Foo> for Foo {
/// fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
/// println!("Slicing!");
/// self
/// }
/// fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
/// println!("Slicing!");
/// self
/// }
/// fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
/// println!("Slicing!");
/// self
/// }
/// fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
/// println!("Slicing!");
/// self
/// }
/// }
///
/// pub fn main() {
/// Foo[mut Foo..];
/// }
/// ```
#[lang="slice_mut"]
pub trait SliceMut<Idx: ?Sized, Result: ?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;
}
/// An unbounded range.
#[derive(Copy)]
#[lang="full_range"]
@ -962,8 +863,6 @@ pub struct Range<Idx> {
pub end: Idx,
}
// FIXME(#19391) needs a snapshot
//impl<Idx: Clone + Step<T=uint>> Iterator<Idx> for Range<Idx> {
#[unstable = "API still in development"]
impl<Idx: Clone + Step> Iterator for Range<Idx> {
type Item = Idx;

View File

@ -30,7 +30,7 @@
// Reexported core operators
pub use kinds::{Copy, Send, Sized, Sync};
pub use ops::{Drop, Fn, FnMut, FnOnce};
pub use ops::{Drop, Fn, FnMut, FnOnce, FullRange};
// Reexported functions
pub use iter::range;

View File

@ -292,17 +292,17 @@ impl<T> SliceExt for [T] {
fn as_mut_slice(&mut self) -> &mut [T] { self }
fn slice_mut(&mut self, start: uint, end: uint) -> &mut [T] {
ops::SliceMut::slice_or_fail_mut(self, &start, &end)
ops::IndexMut::index_mut(self, &ops::Range { start: start, end: end } )
}
#[inline]
fn slice_from_mut(&mut self, start: uint) -> &mut [T] {
ops::SliceMut::slice_from_or_fail_mut(self, &start)
ops::IndexMut::index_mut(self, &ops::RangeFrom { start: start } )
}
#[inline]
fn slice_to_mut(&mut self, end: uint) -> &mut [T] {
ops::SliceMut::slice_to_or_fail_mut(self, &end)
ops::IndexMut::index_mut(self, &ops::RangeTo { end: end } )
}
#[inline]
@ -310,8 +310,8 @@ impl<T> SliceExt for [T] {
unsafe {
let self2: &mut [T] = mem::transmute_copy(&self);
(ops::SliceMut::slice_to_or_fail_mut(self, &mid),
ops::SliceMut::slice_from_or_fail_mut(self2, &mid))
(ops::IndexMut::index_mut(self, &ops::RangeTo { end: mid } ),
ops::IndexMut::index_mut(self2, &ops::RangeFrom { start: mid } ))
}
}
@ -551,63 +551,78 @@ impl<T> ops::IndexMut<uint> for [T] {
}
}
impl<T> ops::Slice<uint, [T]> for [T] {
impl<T> ops::Index<ops::Range<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());
fn index(&self, &index: &ops::Range<uint>) -> &[T] {
assert!(index.start <= index.end);
assert!(index.end <= self.len());
unsafe {
transmute(RawSlice {
data: self.as_ptr().offset(*start as int),
len: (*end - *start)
data: self.as_ptr().offset(index.start as int),
len: index.end - index.start
})
}
}
}
impl<T> ops::SliceMut<uint, [T]> for [T] {
impl<T> ops::Index<ops::RangeTo<uint>, [T]> for [T] {
#[inline]
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
fn index(&self, &index: &ops::RangeTo<uint>) -> &[T] {
self.index(&ops::Range{ start: 0, end: index.end })
}
}
impl<T> ops::Index<ops::RangeFrom<uint>, [T]> for [T] {
#[inline]
fn index(&self, &index: &ops::RangeFrom<uint>) -> &[T] {
self.index(&ops::Range{ start: index.start, end: self.len() })
}
}
impl<T> ops::Index<ops::FullRange, [T]> for [T] {
#[inline]
fn index(&self, &index: &ops::FullRange) -> &[T] {
self
}
}
impl<T> ops::IndexMut<ops::Range<uint>, [T]> for [T] {
#[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());
fn index_mut(&mut self, &index: &ops::Range<uint>) -> &mut [T] {
assert!(index.start <= index.end);
assert!(index.end <= self.len());
unsafe {
transmute(RawSlice {
data: self.as_ptr().offset(*start as int),
len: (*end - *start)
data: self.as_ptr().offset(index.start as int),
len: index.end - index.start
})
}
}
}
impl<T> ops::IndexMut<ops::RangeTo<uint>, [T]> for [T] {
#[inline]
fn index_mut(&mut self, &index: &ops::RangeTo<uint>) -> &mut [T] {
self.index_mut(&ops::Range{ start: 0, end: index.end })
}
}
impl<T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for [T] {
#[inline]
fn index_mut(&mut self, &index: &ops::RangeFrom<uint>) -> &mut [T] {
let len = self.len();
self.index_mut(&ops::Range{ start: index.start, end: len })
}
}
impl<T> ops::IndexMut<ops::FullRange, [T]> for [T] {
#[inline]
fn index_mut(&mut self, &index: &ops::FullRange) -> &mut [T] {
self
}
}
////////////////////////////////////////////////////////////////////////////////
// Common traits
////////////////////////////////////////////////////////////////////////////////
@ -738,24 +753,38 @@ pub struct Iter<'a, T: 'a> {
}
#[experimental]
impl<'a, T> ops::Slice<uint, [T]> for Iter<'a, T> {
fn as_slice_(&self) -> &[T] {
self.as_slice()
}
fn slice_from_or_fail<'b>(&'b self, from: &uint) -> &'b [T] {
use ops::Slice;
self.as_slice().slice_from_or_fail(from)
}
fn slice_to_or_fail<'b>(&'b self, to: &uint) -> &'b [T] {
use ops::Slice;
self.as_slice().slice_to_or_fail(to)
}
fn slice_or_fail<'b>(&'b self, from: &uint, to: &uint) -> &'b [T] {
use ops::Slice;
self.as_slice().slice_or_fail(from, to)
impl<'a, T> ops::Index<ops::Range<uint>, [T]> for Iter<'a, T> {
#[inline]
fn index(&self, index: &ops::Range<uint>) -> &[T] {
self.as_slice().index(index)
}
}
#[experimental]
impl<'a, T> ops::Index<ops::RangeTo<uint>, [T]> for Iter<'a, T> {
#[inline]
fn index(&self, index: &ops::RangeTo<uint>) -> &[T] {
self.as_slice().index(index)
}
}
#[experimental]
impl<'a, T> ops::Index<ops::RangeFrom<uint>, [T]> for Iter<'a, T> {
#[inline]
fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
self.as_slice().index(index)
}
}
#[experimental]
impl<'a, T> ops::Index<ops::FullRange, [T]> for Iter<'a, T> {
#[inline]
fn index(&self, index: &ops::FullRange) -> &[T] {
self.as_slice()
}
}
impl<'a, T> Iter<'a, T> {
/// View the underlying data as a subslice of the original data.
///
@ -813,43 +842,70 @@ pub struct IterMut<'a, T: 'a> {
}
#[experimental]
impl<'a, T> ops::Slice<uint, [T]> for IterMut<'a, T> {
fn as_slice_<'b>(&'b self) -> &'b [T] {
make_slice!(T -> &'b [T]: self.ptr, self.end)
}
fn slice_from_or_fail<'b>(&'b self, from: &uint) -> &'b [T] {
use ops::Slice;
self.as_slice_().slice_from_or_fail(from)
}
fn slice_to_or_fail<'b>(&'b self, to: &uint) -> &'b [T] {
use ops::Slice;
self.as_slice_().slice_to_or_fail(to)
}
fn slice_or_fail<'b>(&'b self, from: &uint, to: &uint) -> &'b [T] {
use ops::Slice;
self.as_slice_().slice_or_fail(from, to)
impl<'a, T> ops::Index<ops::Range<uint>, [T]> for IterMut<'a, T> {
#[inline]
fn index(&self, index: &ops::Range<uint>) -> &[T] {
self.index(&ops::FullRange).index(index)
}
}
#[experimental]
impl<'a, T> ops::SliceMut<uint, [T]> for IterMut<'a, T> {
fn as_mut_slice_<'b>(&'b mut self) -> &'b mut [T] {
make_slice!(T -> &'b mut [T]: self.ptr, self.end)
}
fn slice_from_or_fail_mut<'b>(&'b mut self, from: &uint) -> &'b mut [T] {
use ops::SliceMut;
self.as_mut_slice_().slice_from_or_fail_mut(from)
}
fn slice_to_or_fail_mut<'b>(&'b mut self, to: &uint) -> &'b mut [T] {
use ops::SliceMut;
self.as_mut_slice_().slice_to_or_fail_mut(to)
}
fn slice_or_fail_mut<'b>(&'b mut self, from: &uint, to: &uint) -> &'b mut [T] {
use ops::SliceMut;
self.as_mut_slice_().slice_or_fail_mut(from, to)
impl<'a, T> ops::Index<ops::RangeTo<uint>, [T]> for IterMut<'a, T> {
#[inline]
fn index(&self, index: &ops::RangeTo<uint>) -> &[T] {
self.index(&ops::FullRange).index(index)
}
}
#[experimental]
impl<'a, T> ops::Index<ops::RangeFrom<uint>, [T]> for IterMut<'a, T> {
#[inline]
fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
self.index(&ops::FullRange).index(index)
}
}
#[experimental]
impl<'a, T> ops::Index<ops::FullRange, [T]> for IterMut<'a, T> {
#[inline]
fn index(&self, index: &ops::FullRange) -> &[T] {
make_slice!(T -> &[T]: self.ptr, self.end)
}
}
#[experimental]
impl<'a, T> ops::IndexMut<ops::Range<uint>, [T]> for IterMut<'a, T> {
#[inline]
fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
self.index_mut(&ops::FullRange).index_mut(index)
}
}
#[experimental]
impl<'a, T> ops::IndexMut<ops::RangeTo<uint>, [T]> for IterMut<'a, T> {
#[inline]
fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
self.index_mut(&ops::FullRange).index_mut(index)
}
}
#[experimental]
impl<'a, T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for IterMut<'a, T> {
#[inline]
fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
self.index_mut(&ops::FullRange).index_mut(index)
}
}
#[experimental]
impl<'a, T> ops::IndexMut<ops::FullRange, [T]> for IterMut<'a, T> {
#[inline]
fn index_mut(&mut self, index: &ops::FullRange) -> &mut [T] {
make_slice!(T -> &mut [T]: self.ptr, self.end)
}
}
impl<'a, T> IterMut<'a, T> {
/// View the underlying data as a subslice of the original data.
///

View File

@ -1119,26 +1119,32 @@ mod traits {
}
}
impl ops::Slice<uint, str> for str {
impl ops::Index<ops::Range<uint>, str> for str {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a str {
fn index(&self, &index: &ops::Range<uint>) -> &str {
self.slice(index.start, index.end)
}
}
impl ops::Index<ops::RangeTo<uint>, str> for str {
#[inline]
fn index(&self, &index: &ops::RangeTo<uint>) -> &str {
self.slice_to(index.end)
}
}
impl ops::Index<ops::RangeFrom<uint>, str> for str {
#[inline]
fn index(&self, &index: &ops::RangeFrom<uint>) -> &str {
self.slice_from(index.start)
}
}
impl ops::Index<ops::FullRange, str> for str {
#[inline]
fn index(&self, &index: &ops::FullRange) -> &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)
}
}
}