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

21 files changed

+40
-48
lines changed

src/libcollections/slice.rs

+2-2
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

+1-1
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

+9-18
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

+1-1
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

+1-2
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

+2-2
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

+2-1
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

+2-2
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

+4-4
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

+2-2
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(),

src/libstd/ascii.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use fmt;
1919
use iter::Iterator;
2020
use mem;
2121
use option::{Option, Some, None};
22-
use slice::{ImmutableSlice, MutableSlice, Slice};
22+
use slice::{ImmutableSlice, MutableSlice, AsSlice};
2323
use str::{Str, StrSlice};
2424
use string::{mod, String};
2525
use to_string::IntoStr;

src/libstd/c_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use option::{Option, Some, None};
4343
use ptr::RawPtr;
4444
use ptr;
4545
use raw;
46-
use slice::Slice;
46+
use slice::AsSlice;
4747

4848
/// The type representing a foreign chunk of memory
4949
pub struct CVec<T> {
@@ -145,7 +145,7 @@ impl<T> CVec<T> {
145145
}
146146
}
147147

148-
impl<T> Slice<T> for CVec<T> {
148+
impl<T> AsSlice<T> for CVec<T> {
149149
/// View the stored data as a slice.
150150
fn as_slice<'a>(&'a self) -> &'a [T] {
151151
unsafe {

src/libstd/dynamic_lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use option::*;
2929
use os;
3030
use path::{Path,GenericPath};
3131
use result::*;
32-
use slice::{Slice,ImmutableSlice};
32+
use slice::{AsSlice,ImmutableSlice};
3333
use str;
3434
use string::String;
3535
use vec::Vec;

src/libstd/io/extensions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use num::Int;
2323
use option::{Option, Some, None};
2424
use ptr::RawPtr;
2525
use result::{Ok, Err};
26-
use slice::{ImmutableSlice, Slice};
26+
use slice::{ImmutableSlice, AsSlice};
2727

2828
/// An iterator that reads a single byte on each iteration,
2929
/// until `.read_byte()` returns `EndOfFile`.

src/libstd/io/mem.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use result::{Err, Ok};
1919
use io;
2020
use io::{Reader, Writer, Seek, Buffer, IoError, SeekStyle, IoResult};
2121
use slice;
22-
use slice::Slice;
22+
use slice::AsSlice;
2323
use vec::Vec;
2424

2525
static BUF_CAPACITY: uint = 128;

src/libstd/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ use os;
235235
use boxed::Box;
236236
use result::{Ok, Err, Result};
237237
use rt::rtio;
238-
use slice::{Slice, ImmutableSlice};
238+
use slice::{AsSlice, ImmutableSlice};
239239
use str::{Str, StrSlice};
240240
use str;
241241
use string::String;

src/libstd/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use path::{Path, GenericPath, BytesContainer};
4545
use ptr::RawPtr;
4646
use ptr;
4747
use result::{Err, Ok, Result};
48-
use slice::{Slice, ImmutableSlice, MutableSlice, ImmutablePartialEqSlice};
48+
use slice::{AsSlice, ImmutableSlice, MutableSlice, ImmutablePartialEqSlice};
4949
use slice::CloneableVector;
5050
use str::{Str, StrSlice, StrAllocating};
5151
use string::String;

src/libstd/path/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use option::{Option, None, Some};
7676
use str;
7777
use str::{MaybeOwned, Str, StrSlice};
7878
use string::String;
79-
use slice::{Slice, CloneableVector};
79+
use slice::{AsSlice, CloneableVector};
8080
use slice::{ImmutablePartialEqSlice, ImmutableSlice};
8181
use vec::Vec;
8282

src/libstd/path/posix.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use iter::{DoubleEndedIterator, AdditiveIterator, Extendable, Iterator, Map};
2121
use option::{Option, None, Some};
2222
use str::Str;
2323
use str;
24-
use slice::{CloneableVector, Splits, Slice, VectorVector,
24+
use slice::{CloneableVector, Splits, AsSlice, VectorVector,
2525
ImmutablePartialEqSlice, ImmutableSlice};
2626
use vec::Vec;
2727

@@ -367,7 +367,7 @@ impl Path {
367367

368368
/// Returns a normalized byte vector representation of a path, by removing all empty
369369
/// components, and unnecessary . and .. components.
370-
fn normalize<V: Slice<u8>+CloneableVector<u8>>(v: V) -> Vec<u8> {
370+
fn normalize<V: AsSlice<u8>+CloneableVector<u8>>(v: V) -> Vec<u8> {
371371
// borrowck is being very picky
372372
let val = {
373373
let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;

src/libstd/path/windows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use io::Writer;
2323
use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Iterator, Map};
2424
use mem;
2525
use option::{Option, Some, None};
26-
use slice::{Slice, ImmutableSlice};
26+
use slice::{AsSlice, ImmutableSlice};
2727
use str::{CharSplits, Str, StrAllocating, StrVector, StrSlice};
2828
use string::String;
2929
use unicode::char::UnicodeChar;

src/libstd/prelude.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#[doc(no_inline)] pub use ops::{Shl, Shr};
4848
#[doc(no_inline)] pub use ops::{Index, IndexMut};
4949
#[doc(no_inline)] pub use ops::{Fn, FnMut, FnOnce};
50+
#[doc(no_inline)] pub use ops::{Slice, SliceMut};
5051
#[doc(no_inline)] pub use option::{Option, Some, None};
5152
#[doc(no_inline)] pub use result::{Result, Ok, Err};
5253

@@ -87,7 +88,7 @@
8788
#[doc(no_inline)] pub use slice::{MutableCloneableSlice, MutableOrdSlice};
8889
#[doc(no_inline)] pub use slice::{ImmutableSlice, MutableSlice};
8990
#[doc(no_inline)] pub use slice::{ImmutablePartialEqSlice, ImmutableOrdSlice};
90-
#[doc(no_inline)] pub use slice::{Slice, VectorVector};
91+
#[doc(no_inline)] pub use slice::{AsSlice, VectorVector};
9192
#[doc(no_inline)] pub use slice::MutableSliceAllocating;
9293
#[doc(no_inline)] pub use string::String;
9394
#[doc(no_inline)] pub use vec::Vec;

0 commit comments

Comments
 (0)