Skip to content

Commit 04adce0

Browse files
committed
Add overloaded slice operations to the prelude
The new overloaded slice operations have replaced the various `slice` methods on slice types. However, the relevant traits were not included in the prelude, meaning that you would have to manually import `std::ops::Slice` to get these operations -- an unintended regression. This commit adds the traits to the prelude, as is done with most other operator traits. Unfortunately, the trait `std::slice::Slice` (which defines an `as_slice` method) was already included in the prelude. This trait is renamed to `AsSlice`, which is a better name in any case. In addition, because of both `AsSlice` and `Str` traits being included in the prelude, both of which define `as_slice`, and both of which are used for generic programming, this commit renames `ops::Slice::as_slice` to `ops::Slice::as_slice_`. This is a stopgap solution; we'll need to figure out a long-term story here later on. Closes #17710 Due to the renamings, this is a: [breaking-change]
1 parent b419e9e commit 04adce0

File tree

21 files changed

+40
-48
lines changed

21 files changed

+40
-48
lines changed

src/libcollections/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ use core::iter::{range_step, MultiplicativeIterator};
9898
use MutableSeq;
9999
use vec::Vec;
100100

101-
pub use core::slice::{Chunks, Slice, ImmutableSlice, ImmutablePartialEqSlice};
101+
pub use core::slice::{Chunks, AsSlice, ImmutableSlice, ImmutablePartialEqSlice};
102102
pub use core::slice::{ImmutableOrdSlice, MutableSlice, Items, MutItems};
103103
pub use core::slice::{MutSplits, MutChunks, Splits};
104104
pub use core::slice::{bytes, mut_ref_slice, ref_slice, MutableCloneableSlice};
@@ -117,7 +117,7 @@ pub trait VectorVector<T> {
117117
fn connect_vec(&self, sep: &T) -> Vec<T>;
118118
}
119119

120-
impl<'a, T: Clone, V: Slice<T>> VectorVector<T> for &'a [V] {
120+
impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for &'a [V] {
121121
fn concat_vec(&self) -> Vec<T> {
122122
let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len());
123123
let mut result = Vec::with_capacity(size);

src/libcollections/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use core::iter::AdditiveIterator;
6161
use core::mem;
6262
use core::prelude::{Char, Clone, Collection, Eq, Equiv, ImmutableSlice};
6363
use core::prelude::{Iterator, MutableSlice, None, Option, Ord, Ordering};
64-
use core::prelude::{PartialEq, PartialOrd, Result, Slice, Some, Tuple2};
64+
use core::prelude::{PartialEq, PartialOrd, Result, AsSlice, Some, Tuple2};
6565
use core::prelude::{range};
6666

6767
use {Deque, MutableSeq};

src/libcollections/vec.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::num;
2424
use core::ops;
2525
use core::ptr;
2626
use core::raw::Slice as RawSlice;
27-
use core::slice::Slice as SliceSlice;
27+
use core::slice::AsSlice;
2828
use core::uint;
2929

3030
use {Mutable, MutableSeq};
@@ -461,34 +461,25 @@ impl<T> Index<uint,T> for Vec<T> {
461461
}
462462
}*/
463463

464-
// Annoying helper function because there are two Slice::as_slice functions in
465-
// scope.
466-
#[cfg(not(stage0))]
467-
#[inline]
468-
fn slice_to_slice<'a, T, U: Slice<T>>(this: &'a U) -> &'a [T] {
469-
this.as_slice()
470-
}
471-
472-
473464
#[cfg(not(stage0))]
474465
impl<T> ops::Slice<uint, [T]> for Vec<T> {
475466
#[inline]
476-
fn as_slice<'a>(&'a self) -> &'a [T] {
477-
slice_to_slice(self)
467+
fn as_slice_<'a>(&'a self) -> &'a [T] {
468+
self.as_slice()
478469
}
479470

480471
#[inline]
481472
fn slice_from<'a>(&'a self, start: &uint) -> &'a [T] {
482-
slice_to_slice(self).slice_from(start)
473+
self.as_slice().slice_from(start)
483474
}
484475

485476
#[inline]
486477
fn slice_to<'a>(&'a self, end: &uint) -> &'a [T] {
487-
slice_to_slice(self).slice_to(end)
478+
self.as_slice().slice_to(end)
488479
}
489480
#[inline]
490481
fn slice<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
491-
slice_to_slice(self).slice(start, end)
482+
self.as_slice().slice(start, end)
492483
}
493484
}
494485
#[cfg(stage0)]
@@ -601,7 +592,7 @@ impl<T: PartialOrd> PartialOrd for Vec<T> {
601592
impl<T: Eq> Eq for Vec<T> {}
602593

603594
#[experimental]
604-
impl<T: PartialEq, V: Slice<T>> Equiv<V> for Vec<T> {
595+
impl<T: PartialEq, V: AsSlice<T>> Equiv<V> for Vec<T> {
605596
#[inline]
606597
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
607598
}
@@ -1654,7 +1645,7 @@ impl<T: PartialEq> Vec<T> {
16541645
}
16551646
}
16561647

1657-
impl<T> Slice<T> for Vec<T> {
1648+
impl<T> AsSlice<T> for Vec<T> {
16581649
/// Returns a slice into `self`.
16591650
///
16601651
/// # Example
@@ -1672,7 +1663,7 @@ impl<T> Slice<T> for Vec<T> {
16721663
}
16731664
}
16741665

1675-
impl<T: Clone, V: Slice<T>> Add<V, Vec<T>> for Vec<T> {
1666+
impl<T: Clone, V: AsSlice<T>> Add<V, Vec<T>> for Vec<T> {
16761667
#[inline]
16771668
fn add(&self, rhs: &V) -> Vec<T> {
16781669
let mut res = Vec::with_capacity(self.len() + rhs.as_slice().len());

src/libcore/fmt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use option::{Option, Some, None};
2222
use ops::Deref;
2323
use result::{Ok, Err};
2424
use result;
25-
use slice::{Slice, ImmutableSlice};
25+
use slice::{AsSlice, ImmutableSlice};
2626
use slice;
2727
use str::StrSlice;
2828
use str;

src/libcore/ops.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ pub trait IndexMut<Index, Result> {
715715
#[lang="slice"]
716716
pub trait Slice<Idx, Sized? Result> for Sized? {
717717
/// The method for the slicing operation foo[]
718-
fn as_slice<'a>(&'a self) -> &'a Result;
718+
fn as_slice_<'a>(&'a self) -> &'a Result;
719719
/// The method for the slicing operation foo[from..]
720720
fn slice_from<'a>(&'a self, from: &Idx) -> &'a Result;
721721
/// The method for the slicing operation foo[..to]
@@ -933,4 +933,3 @@ def_fn_mut!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12)
933933
def_fn_mut!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13)
934934
def_fn_mut!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14)
935935
def_fn_mut!(A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14 A15)
936-

src/libcore/option.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize};
149149
use mem;
150150
use result::{Result, Ok, Err};
151151
use slice;
152-
use slice::Slice;
152+
use slice::AsSlice;
153153

154154
// Note that this is not a lang item per se, but it has a hidden dependency on
155155
// `Iterator`, which is one. The compiler assumes that the `next` method of
@@ -846,7 +846,7 @@ impl<T: Default> Option<T> {
846846
// Trait implementations
847847
/////////////////////////////////////////////////////////////////////////////
848848

849-
impl<T> Slice<T> for Option<T> {
849+
impl<T> AsSlice<T> for Option<T> {
850850
/// Convert from `Option<T>` to `&[T]` (without copying)
851851
#[inline]
852852
#[stable]

src/libcore/prelude.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub use ops::{Drop, Deref, DerefMut};
3636
pub use ops::{Shl, Shr};
3737
pub use ops::{Index, IndexMut};
3838
pub use ops::{Fn, FnMut, FnOnce};
39+
pub use ops::{Slice, SliceMut};
3940
pub use option::{Option, Some, None};
4041
pub use result::{Result, Ok, Err};
4142

@@ -63,4 +64,4 @@ pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8};
6364
pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12};
6465
pub use slice::{ImmutablePartialEqSlice, ImmutableOrdSlice};
6566
pub use slice::{MutableSlice};
66-
pub use slice::{Slice, ImmutableSlice};
67+
pub use slice::{AsSlice, ImmutableSlice};

src/libcore/result.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ use clone::Clone;
280280
use cmp::PartialEq;
281281
use std::fmt::Show;
282282
use slice;
283-
use slice::Slice;
283+
use slice::AsSlice;
284284
use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize};
285285
use option::{None, Option, Some};
286286

@@ -844,7 +844,7 @@ impl<T: Show, E> Result<T, E> {
844844
// Trait implementations
845845
/////////////////////////////////////////////////////////////////////////////
846846

847-
impl<T, E> Slice<T> for Result<T, E> {
847+
impl<T, E> AsSlice<T> for Result<T, E> {
848848
/// Convert from `Result<T, E>` to `&[T]` (without copying)
849849
#[inline]
850850
#[stable]

src/libcore/slice.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,13 +1057,13 @@ impl<'a, T:Clone> MutableCloneableSlice<T> for &'a mut [T] {
10571057

10581058
/// Data that is viewable as a slice.
10591059
#[unstable = "may merge with other traits"]
1060-
pub trait Slice<T> {
1060+
pub trait AsSlice<T> {
10611061
/// Work with `self` as a slice.
10621062
fn as_slice<'a>(&'a self) -> &'a [T];
10631063
}
10641064

10651065
#[unstable = "trait is unstable"]
1066-
impl<'a,T> Slice<T> for &'a [T] {
1066+
impl<'a,T> AsSlice<T> for &'a [T] {
10671067
#[inline(always)]
10681068
fn as_slice<'a>(&'a self) -> &'a [T] { *self }
10691069
}
@@ -1742,7 +1742,7 @@ impl<'a,T:PartialEq> PartialEq for &'a [T] {
17421742
impl<'a,T:Eq> Eq for &'a [T] {}
17431743

17441744
#[unstable = "waiting for DST"]
1745-
impl<'a,T:PartialEq, V: Slice<T>> Equiv<V> for &'a [T] {
1745+
impl<'a,T:PartialEq, V: AsSlice<T>> Equiv<V> for &'a [T] {
17461746
#[inline]
17471747
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
17481748
}
@@ -1763,7 +1763,7 @@ impl<'a,T:PartialEq> PartialEq for &'a mut [T] {
17631763
impl<'a,T:Eq> Eq for &'a mut [T] {}
17641764

17651765
#[unstable = "waiting for DST"]
1766-
impl<'a,T:PartialEq, V: Slice<T>> Equiv<V> for &'a mut [T] {
1766+
impl<'a,T:PartialEq, V: AsSlice<T>> Equiv<V> for &'a mut [T] {
17671767
#[inline]
17681768
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
17691769
}

src/libgraphviz/maybe_owned_vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a, T: Ord> Ord for MaybeOwnedVector<'a, T> {
8484
}
8585
}
8686

87-
impl<'a, T: PartialEq, V: Slice<T>> Equiv<V> for MaybeOwnedVector<'a, T> {
87+
impl<'a, T: PartialEq, V: AsSlice<T>> Equiv<V> for MaybeOwnedVector<'a, T> {
8888
fn equiv(&self, other: &V) -> bool {
8989
self.as_slice() == other.as_slice()
9090
}
@@ -99,7 +99,7 @@ impl<'a, T: PartialEq, V: Slice<T>> Equiv<V> for MaybeOwnedVector<'a, T> {
9999
// In any case, with `Vector` in place, the client can just use
100100
// `as_slice` if they prefer that over `match`.
101101

102-
impl<'b,T> Slice<T> for MaybeOwnedVector<'b,T> {
102+
impl<'b,T> AsSlice<T> for MaybeOwnedVector<'b,T> {
103103
fn as_slice<'a>(&'a self) -> &'a [T] {
104104
match self {
105105
&Growable(ref v) => v.as_slice(),

0 commit comments

Comments
 (0)