Skip to content

Commit

Permalink
Impls using the new scheme for slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Jan 6, 2015
1 parent 6539cb4 commit 918255e
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 209 deletions.
24 changes: 15 additions & 9 deletions src/libcollections/string.rs
Expand Up @@ -818,25 +818,31 @@ 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 {
unsafe { mem::transmute(self.vec.as_slice()) }
fn index(&self, &index: &ops::Range<uint>) -> &str {
self[][*index]
}
}

impl<T> ops::Index<ops::RangeTo<uint>, str> for String {
#[inline]
fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
self[][*from..]
fn index(&self, &index: &ops::RangeTo<uint>) -> &str {
self[][*index]
}
}

impl<T> ops::Index<ops::RangeFrom<uint>, str> for String {
#[inline]
fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
self[][..*to]
fn index(&self, &index: &ops::RangeFrom<uint>) -> &str {
self[][*index]
}
}

impl<T> ops::Index<ops::FullRange<uint>, str> for String {
#[inline]
fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
self[][*from..*to]
fn index(&self, &index: &ops::FullRange<uint>) -> &str {
unsafe { mem::transmute(self.vec.as_slice()) }
}
}

Expand Down
55 changes: 38 additions & 17 deletions src/libcollections/vec.rs
Expand Up @@ -1211,43 +1211,64 @@ 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()
fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
&mut self.as_mut_slice()[*index]
}
}

impl<T> ops::Index<ops::Range<uint>, [T]> for Vec<T> {
#[inline]
fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
self.as_slice().slice_from_or_fail(start)
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 slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
self.as_slice().slice_to_or_fail(end)
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 slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
self.as_slice().slice_or_fail(start, end)
fn index(&self, &index: &ops::RangeFrom<uint>) -> &[T] {
self.as_slice().index(index)
}
}

impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
impl<T> ops::Index<ops::FullRange<uint>, [T]> for Vec<T> {
#[inline]
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
self.as_mut_slice()
fn index(&self, &index: &ops::FullRange<uint>) -> &[T] {
self.as_slice()
}
}

impl<T> ops::IndexMut<ops::Range<uint>, [T]> for Vec<T> {
#[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)
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 slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
self.as_mut_slice().slice_to_or_fail_mut(end)
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 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)
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()
}
}

Expand Down
101 changes: 0 additions & 101 deletions src/libcore/ops.rs
Expand Up @@ -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"]
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/prelude.rs
Expand Up @@ -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;
Expand Down

0 comments on commit 918255e

Please sign in to comment.