Skip to content

Commit 94ddb51

Browse files
author
Jorge Aparicio
committed
DSTify [T]/str extension traits
This PR changes the signature of several methods from `foo(self, ...)` to `foo(&self, ...)`/`foo(&mut self, ...)`, but there is no breakage of the usage of these methods due to the autoref nature of `method.call()`s. This PR also removes the lifetime parameter from some traits (`Trait<'a>` -> `Trait`). These changes break any use of the extension traits for generic programming, but those traits are not meant to be used for generic programming in the first place. In the whole rust distribution there was only one misuse of a extension trait as a bound, which got corrected (the bound was unnecessary and got removed) as part of this PR. [breaking-change]
1 parent bd7138d commit 94ddb51

File tree

8 files changed

+235
-230
lines changed

8 files changed

+235
-230
lines changed

src/libcollections/slice.rs

+25-24
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989

9090
use alloc::boxed::Box;
9191
use core::cmp;
92+
use core::kinds::Sized;
9293
use core::mem::size_of;
9394
use core::mem;
9495
use core::prelude::{Clone, Collection, Greater, Iterator, Less, None, Option};
@@ -109,7 +110,7 @@ pub use core::slice::{Found, NotFound};
109110
// Functional utilities
110111

111112
#[allow(missing_doc)]
112-
pub trait VectorVector<T> {
113+
pub trait VectorVector<T> for Sized? {
113114
// FIXME #5898: calling these .concat and .connect conflicts with
114115
// StrVector::con{cat,nect}, since they have generic contents.
115116
/// Flattens a vector of vectors of `T` into a single `Vec<T>`.
@@ -119,7 +120,7 @@ pub trait VectorVector<T> {
119120
fn connect_vec(&self, sep: &T) -> Vec<T>;
120121
}
121122

122-
impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for &'a [V] {
123+
impl<T: Clone, V: AsSlice<T>> VectorVector<T> for [V] {
123124
fn concat_vec(&self) -> Vec<T> {
124125
let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len());
125126
let mut result = Vec::with_capacity(size);
@@ -267,17 +268,17 @@ impl<T: Clone> Iterator<Vec<T>> for Permutations<T> {
267268
}
268269

269270
/// Extension methods for vector slices with cloneable elements
270-
pub trait CloneableVector<T> {
271+
pub trait CloneableVector<T> for Sized? {
271272
/// Copies `self` into a new `Vec`.
272273
fn to_vec(&self) -> Vec<T>;
273274
}
274275

275-
impl<'a, T: Clone> CloneableVector<T> for &'a [T] {
276+
impl<T: Clone> CloneableVector<T> for [T] {
276277
/// Returns a copy of `v`.
277278
#[inline]
278279
fn to_vec(&self) -> Vec<T> {
279280
let mut vector = Vec::with_capacity(self.len());
280-
vector.push_all(*self);
281+
vector.push_all(self);
281282
vector
282283
}
283284
}
@@ -300,7 +301,7 @@ impl<T> BoxedSlice<T> for Box<[T]> {
300301
}
301302

302303
/// Extension methods for vectors containing `Clone` elements.
303-
pub trait ImmutableCloneableVector<T> {
304+
pub trait ImmutableCloneableVector<T> for Sized? {
304305
/// Partitions the vector into two vectors `(a, b)`, where all
305306
/// elements of `a` satisfy `f` and all elements of `b` do not.
306307
fn partitioned(&self, f: |&T| -> bool) -> (Vec<T>, Vec<T>);
@@ -329,10 +330,10 @@ pub trait ImmutableCloneableVector<T> {
329330
/// assert_eq!(Some(vec![1i, 3, 2]), perms.next());
330331
/// assert_eq!(Some(vec![3i, 1, 2]), perms.next());
331332
/// ```
332-
fn permutations(self) -> Permutations<T>;
333+
fn permutations(&self) -> Permutations<T>;
333334
}
334335

335-
impl<'a,T:Clone> ImmutableCloneableVector<T> for &'a [T] {
336+
impl<T: Clone> ImmutableCloneableVector<T> for [T] {
336337
#[inline]
337338
fn partitioned(&self, f: |&T| -> bool) -> (Vec<T>, Vec<T>) {
338339
let mut lefts = Vec::new();
@@ -350,7 +351,7 @@ impl<'a,T:Clone> ImmutableCloneableVector<T> for &'a [T] {
350351
}
351352

352353
/// Returns an iterator over all permutations of a vector.
353-
fn permutations(self) -> Permutations<T> {
354+
fn permutations(&self) -> Permutations<T> {
354355
Permutations{
355356
swaps: ElementSwaps::new(self.len()),
356357
v: self.to_vec(),
@@ -564,7 +565,7 @@ fn merge_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) {
564565

565566
/// Extension methods for vectors such that their elements are
566567
/// mutable.
567-
pub trait MutableSliceAllocating<'a, T> {
568+
pub trait MutableSliceAllocating<T> for Sized? {
568569
/// Sorts the slice, in place, using `compare` to compare
569570
/// elements.
570571
///
@@ -582,7 +583,7 @@ pub trait MutableSliceAllocating<'a, T> {
582583
/// v.sort_by(|a, b| b.cmp(a));
583584
/// assert!(v == [5, 4, 3, 2, 1]);
584585
/// ```
585-
fn sort_by(self, compare: |&T, &T| -> Ordering);
586+
fn sort_by(&mut self, compare: |&T, &T| -> Ordering);
586587

587588
/// Consumes `src` and moves as many elements as it can into `self`
588589
/// from the range [start,end).
@@ -605,17 +606,17 @@ pub trait MutableSliceAllocating<'a, T> {
605606
/// assert_eq!(num_moved, 3);
606607
/// assert!(a == [6i, 7, 8, 4, 5]);
607608
/// ```
608-
fn move_from(self, src: Vec<T>, start: uint, end: uint) -> uint;
609+
fn move_from(&mut self, src: Vec<T>, start: uint, end: uint) -> uint;
609610
}
610611

611-
impl<'a,T> MutableSliceAllocating<'a, T> for &'a mut [T] {
612+
impl<T> MutableSliceAllocating<T> for [T] {
612613
#[inline]
613-
fn sort_by(self, compare: |&T, &T| -> Ordering) {
614+
fn sort_by(&mut self, compare: |&T, &T| -> Ordering) {
614615
merge_sort(self, compare)
615616
}
616617

617618
#[inline]
618-
fn move_from(self, mut src: Vec<T>, start: uint, end: uint) -> uint {
619+
fn move_from(&mut self, mut src: Vec<T>, start: uint, end: uint) -> uint {
619620
for (a, b) in self.iter_mut().zip(src[mut start..end].iter_mut()) {
620621
mem::swap(a, b);
621622
}
@@ -625,7 +626,7 @@ impl<'a,T> MutableSliceAllocating<'a, T> for &'a mut [T] {
625626

626627
/// Methods for mutable vectors with orderable elements, such as
627628
/// in-place sorting.
628-
pub trait MutableOrdSlice<T> {
629+
pub trait MutableOrdSlice<T> for Sized? {
629630
/// Sorts the slice, in place.
630631
///
631632
/// This is equivalent to `self.sort_by(|a, b| a.cmp(b))`.
@@ -638,7 +639,7 @@ pub trait MutableOrdSlice<T> {
638639
/// v.sort();
639640
/// assert!(v == [-5i, -3, 1, 2, 4]);
640641
/// ```
641-
fn sort(self);
642+
fn sort(&mut self);
642643

643644
/// Mutates the slice to the next lexicographic permutation.
644645
///
@@ -656,7 +657,7 @@ pub trait MutableOrdSlice<T> {
656657
/// let b: &mut [_] = &mut [1i, 0, 2];
657658
/// assert!(v == b);
658659
/// ```
659-
fn next_permutation(self) -> bool;
660+
fn next_permutation(&mut self) -> bool;
660661

661662
/// Mutates the slice to the previous lexicographic permutation.
662663
///
@@ -674,16 +675,16 @@ pub trait MutableOrdSlice<T> {
674675
/// let b: &mut [_] = &mut [0i, 1, 2];
675676
/// assert!(v == b);
676677
/// ```
677-
fn prev_permutation(self) -> bool;
678+
fn prev_permutation(&mut self) -> bool;
678679
}
679680

680-
impl<'a, T: Ord> MutableOrdSlice<T> for &'a mut [T] {
681+
impl<T: Ord> MutableOrdSlice<T> for [T] {
681682
#[inline]
682-
fn sort(self) {
683-
self.sort_by(|a,b| a.cmp(b))
683+
fn sort(&mut self) {
684+
self.sort_by(|a, b| a.cmp(b))
684685
}
685686

686-
fn next_permutation(self) -> bool {
687+
fn next_permutation(&mut self) -> bool {
687688
// These cases only have 1 permutation each, so we can't do anything.
688689
if self.len() < 2 { return false; }
689690

@@ -713,7 +714,7 @@ impl<'a, T: Ord> MutableOrdSlice<T> for &'a mut [T] {
713714
true
714715
}
715716

716-
fn prev_permutation(self) -> bool {
717+
fn prev_permutation(&mut self) -> bool {
717718
// These cases only have 1 permutation each, so we can't do anything.
718719
if self.len() < 2 { return false; }
719720

src/libcollections/str.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use core::default::Default;
5858
use core::fmt;
5959
use core::cmp;
6060
use core::iter::AdditiveIterator;
61+
use core::kinds::Sized;
6162
use core::prelude::{Char, Clone, Collection, Eq, Equiv, ImmutableSlice};
6263
use core::prelude::{Iterator, MutableSlice, None, Option, Ord, Ordering};
6364
use core::prelude::{PartialEq, PartialOrd, Result, AsSlice, Some, Tuple2};
@@ -84,7 +85,7 @@ Section: Creating a string
8485
*/
8586

8687
/// Methods for vectors of strings.
87-
pub trait StrVector {
88+
pub trait StrVector for Sized? {
8889
/// Concatenates a vector of strings.
8990
///
9091
/// # Example
@@ -110,7 +111,7 @@ pub trait StrVector {
110111
fn connect(&self, sep: &str) -> String;
111112
}
112113

113-
impl<'a, S: Str> StrVector for &'a [S] {
114+
impl<S: Str> StrVector for [S] {
114115
fn concat(&self) -> String {
115116
if self.is_empty() {
116117
return String::new();
@@ -157,7 +158,7 @@ impl<'a, S: Str> StrVector for &'a [S] {
157158
}
158159
}
159160

160-
impl<'a, S: Str> StrVector for Vec<S> {
161+
impl<S: Str> StrVector for Vec<S> {
161162
#[inline]
162163
fn concat(&self) -> String {
163164
self.as_slice().concat()

src/libcore/raw.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! Their definition should always match the ABI defined in `rustc::back::abi`.
2020
2121
use mem;
22+
use kinds::Sized;
2223

2324
/// The representation of a Rust slice
2425
#[repr(C)]
@@ -53,14 +54,14 @@ pub struct TraitObject {
5354

5455
/// This trait is meant to map equivalences between raw structs and their
5556
/// corresponding rust values.
56-
pub trait Repr<T> {
57+
pub trait Repr<T> for Sized? {
5758
/// This function "unwraps" a rust value (without consuming it) into its raw
5859
/// struct representation. This can be used to read/write different values
5960
/// for the struct. This is a safe method because by default it does not
6061
/// enable write-access to the fields of the return value in safe code.
6162
#[inline]
62-
fn repr(&self) -> T { unsafe { mem::transmute_copy(self) } }
63+
fn repr(&self) -> T { unsafe { mem::transmute_copy(&self) } }
6364
}
6465

65-
impl<'a, T> Repr<Slice<T>> for &'a [T] {}
66-
impl<'a> Repr<Slice<u8>> for &'a str {}
66+
impl<T> Repr<Slice<T>> for [T] {}
67+
impl Repr<Slice<u8>> for str {}

0 commit comments

Comments
 (0)