Skip to content

Commit b825b34

Browse files
committed
auto merge of #18638 : aturon/rust/as_slice_dst, r=japaric
This PR changes `AsSlice` to work on unsized types, and changes the `impl` for `&[T]` to `[T]`. Aside from making the trait more general, this also helps some ongoing work with method resolution changes. This is a breaking change: code that uses generics bounded by `AsSlice` will have to change. In particular, such code previously often took arguments of type `V` where `V: AsSlice<T>` by value. These should now be taken by reference: ```rust fn foo<Sized? V: AsSlice<T>>(v: &V) { .. } ``` A few std lib functions have been changed accordingly. The PR also relaxes constraints on generics and traits within the `core::ops` module and for the `Equiv` trait. [breaking-change] r? @nikomatsakis cc @japaric
2 parents dd5ce5a + c287afb commit b825b34

File tree

7 files changed

+42
-30
lines changed

7 files changed

+42
-30
lines changed

src/libcollections/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub trait VectorVector<T> for Sized? {
121121
fn connect_vec(&self, sep: &T) -> Vec<T>;
122122
}
123123

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

src/libcollections/vec.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use core::cmp::max;
2020
use core::default::Default;
2121
use core::fmt;
2222
use core::kinds::marker::{ContravariantLifetime, InvariantType};
23+
use core::kinds::Sized;
2324
use core::mem;
2425
use core::num::{Int, UnsignedInt};
2526
use core::ops;
@@ -516,7 +517,7 @@ impl<T: PartialOrd> PartialOrd for Vec<T> {
516517
impl<T: Eq> Eq for Vec<T> {}
517518

518519
#[experimental]
519-
impl<T: PartialEq, V: AsSlice<T>> Equiv<V> for Vec<T> {
520+
impl<T: PartialEq, Sized? V: AsSlice<T>> Equiv<V> for Vec<T> {
520521
#[inline]
521522
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
522523
}
@@ -1181,7 +1182,7 @@ impl<T> AsSlice<T> for Vec<T> {
11811182
}
11821183
}
11831184

1184-
impl<T: Clone, V: AsSlice<T>> Add<V, Vec<T>> for Vec<T> {
1185+
impl<T: Clone, Sized? V: AsSlice<T>> Add<V, Vec<T>> for Vec<T> {
11851186
#[inline]
11861187
fn add(&self, rhs: &V) -> Vec<T> {
11871188
let mut res = Vec::with_capacity(self.len() + rhs.as_slice().len());

src/libcore/cmp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ pub trait PartialOrd for Sized?: PartialEq {
241241
/// container types; e.g. it is often desirable to be able to use `&str`
242242
/// values to look up entries in a container with `String` keys.
243243
#[experimental = "Better solutions may be discovered."]
244-
pub trait Equiv<T> for Sized? {
244+
pub trait Equiv<Sized? T> for Sized? {
245245
/// Implement this function to decide equivalent values.
246246
fn equiv(&self, other: &T) -> bool;
247247
}

src/libcore/ops.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub trait Drop {
112112
* ```
113113
*/
114114
#[lang="add"]
115-
pub trait Add<RHS,Result> {
115+
pub trait Add<Sized? RHS,Result> for Sized? {
116116
/// The method for the `+` operator
117117
fn add(&self, rhs: &RHS) -> Result;
118118
}
@@ -153,7 +153,7 @@ add_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
153153
* ```
154154
*/
155155
#[lang="sub"]
156-
pub trait Sub<RHS,Result> {
156+
pub trait Sub<Sized? RHS, Result> for Sized? {
157157
/// The method for the `-` operator
158158
fn sub(&self, rhs: &RHS) -> Result;
159159
}
@@ -194,7 +194,7 @@ sub_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
194194
* ```
195195
*/
196196
#[lang="mul"]
197-
pub trait Mul<RHS,Result> {
197+
pub trait Mul<Sized? RHS, Result> for Sized? {
198198
/// The method for the `*` operator
199199
fn mul(&self, rhs: &RHS) -> Result;
200200
}
@@ -235,7 +235,7 @@ mul_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
235235
* ```
236236
*/
237237
#[lang="div"]
238-
pub trait Div<RHS,Result> {
238+
pub trait Div<Sized? RHS, Result> for Sized? {
239239
/// The method for the `/` operator
240240
fn div(&self, rhs: &RHS) -> Result;
241241
}
@@ -276,7 +276,7 @@ div_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
276276
* ```
277277
*/
278278
#[lang="rem"]
279-
pub trait Rem<RHS,Result> {
279+
pub trait Rem<Sized? RHS, Result> for Sized? {
280280
/// The method for the `%` operator
281281
fn rem(&self, rhs: &RHS) -> Result;
282282
}
@@ -331,7 +331,7 @@ rem_float_impl!(f64, fmod)
331331
* ```
332332
*/
333333
#[lang="neg"]
334-
pub trait Neg<Result> {
334+
pub trait Neg<Result> for Sized? {
335335
/// The method for the unary `-` operator
336336
fn neg(&self) -> Result;
337337
}
@@ -388,7 +388,7 @@ neg_uint_impl!(u64, i64)
388388
* ```
389389
*/
390390
#[lang="not"]
391-
pub trait Not<Result> {
391+
pub trait Not<Result> for Sized? {
392392
/// The method for the unary `!` operator
393393
fn not(&self) -> Result;
394394
}
@@ -430,7 +430,7 @@ not_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
430430
* ```
431431
*/
432432
#[lang="bitand"]
433-
pub trait BitAnd<RHS,Result> {
433+
pub trait BitAnd<Sized? RHS, Result> for Sized? {
434434
/// The method for the `&` operator
435435
fn bitand(&self, rhs: &RHS) -> Result;
436436
}
@@ -471,7 +471,7 @@ bitand_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
471471
* ```
472472
*/
473473
#[lang="bitor"]
474-
pub trait BitOr<RHS,Result> {
474+
pub trait BitOr<Sized? RHS, Result> for Sized? {
475475
/// The method for the `|` operator
476476
fn bitor(&self, rhs: &RHS) -> Result;
477477
}
@@ -512,7 +512,7 @@ bitor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
512512
* ```
513513
*/
514514
#[lang="bitxor"]
515-
pub trait BitXor<RHS,Result> {
515+
pub trait BitXor<Sized? RHS, Result> for Sized? {
516516
/// The method for the `^` operator
517517
fn bitxor(&self, rhs: &RHS) -> Result;
518518
}
@@ -553,7 +553,7 @@ bitxor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
553553
* ```
554554
*/
555555
#[lang="shl"]
556-
pub trait Shl<RHS,Result> {
556+
pub trait Shl<Sized? RHS, Result> for Sized? {
557557
/// The method for the `<<` operator
558558
fn shl(&self, rhs: &RHS) -> Result;
559559
}
@@ -596,7 +596,7 @@ shl_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
596596
* ```
597597
*/
598598
#[lang="shr"]
599-
pub trait Shr<RHS,Result> {
599+
pub trait Shr<Sized? RHS, Result> for Sized? {
600600
/// The method for the `>>` operator
601601
fn shr(&self, rhs: &RHS) -> Result;
602602
}
@@ -669,7 +669,7 @@ pub trait Index<Sized? Index, Sized? Result> for Sized? {
669669
* ```
670670
*/
671671
#[lang="index_mut"]
672-
pub trait IndexMut<Sized? Index, Result> for Sized? {
672+
pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
673673
/// The method for the indexing (`Foo[Bar]`) operation
674674
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
675675
}
@@ -712,7 +712,7 @@ pub trait IndexMut<Sized? Index, Result> for Sized? {
712712
* ```
713713
*/
714714
#[lang="slice"]
715-
pub trait Slice<Idx, Sized? Result> for Sized? {
715+
pub trait Slice<Sized? Idx, Sized? Result> for Sized? {
716716
/// The method for the slicing operation foo[]
717717
fn as_slice_<'a>(&'a self) -> &'a Result;
718718
/// The method for the slicing operation foo[from..]
@@ -761,7 +761,7 @@ pub trait Slice<Idx, Sized? Result> for Sized? {
761761
* ```
762762
*/
763763
#[lang="slice_mut"]
764-
pub trait SliceMut<Idx, Sized? Result> for Sized? {
764+
pub trait SliceMut<Sized? Idx, Sized? Result> for Sized? {
765765
/// The method for the slicing operation foo[]
766766
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
767767
/// The method for the slicing operation foo[from..]
@@ -800,7 +800,7 @@ pub trait SliceMut<Idx, Sized? Result> for Sized? {
800800
* ```
801801
*/
802802
#[lang="deref"]
803-
pub trait Deref<Sized? Result> {
803+
pub trait Deref<Sized? Result> for Sized? {
804804
/// The method called to dereference a value
805805
fn deref<'a>(&'a self) -> &'a Result;
806806
}
@@ -859,21 +859,21 @@ impl<'a, Sized? T> DerefMut<T> for &'a mut T {
859859

860860
/// A version of the call operator that takes an immutable receiver.
861861
#[lang="fn"]
862-
pub trait Fn<Args,Result> {
862+
pub trait Fn<Args,Result> for Sized? {
863863
/// This is called when the call operator is used.
864864
extern "rust-call" fn call(&self, args: Args) -> Result;
865865
}
866866

867867
/// A version of the call operator that takes a mutable receiver.
868868
#[lang="fn_mut"]
869-
pub trait FnMut<Args,Result> {
869+
pub trait FnMut<Args,Result> for Sized? {
870870
/// This is called when the call operator is used.
871871
extern "rust-call" fn call_mut(&mut self, args: Args) -> Result;
872872
}
873873

874874
/// A version of the call operator that takes a by-value receiver.
875875
#[lang="fn_once"]
876-
pub trait FnOnce<Args,Result> {
876+
pub trait FnOnce<Args,Result> for Sized? {
877877
/// This is called when the call operator is used.
878878
extern "rust-call" fn call_once(self, args: Args) -> Result;
879879
}

src/libcore/slice.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1008,15 +1008,25 @@ impl<T: Clone> CloneSlicePrelude<T> for [T] {
10081008

10091009
/// Data that is viewable as a slice.
10101010
#[unstable = "may merge with other traits"]
1011-
pub trait AsSlice<T> {
1011+
pub trait AsSlice<T> for Sized? {
10121012
/// Work with `self` as a slice.
10131013
fn as_slice<'a>(&'a self) -> &'a [T];
10141014
}
10151015

10161016
#[unstable = "trait is unstable"]
1017-
impl<'a,T> AsSlice<T> for &'a [T] {
1017+
impl<T> AsSlice<T> for [T] {
10181018
#[inline(always)]
1019-
fn as_slice<'a>(&'a self) -> &'a [T] { *self }
1019+
fn as_slice<'a>(&'a self) -> &'a [T] { self }
1020+
}
1021+
1022+
impl<'a, T, Sized? U: AsSlice<T>> AsSlice<T> for &'a U {
1023+
#[inline(always)]
1024+
fn as_slice<'a>(&'a self) -> &'a [T] { AsSlice::as_slice(*self) }
1025+
}
1026+
1027+
impl<'a, T, Sized? U: AsSlice<T>> AsSlice<T> for &'a mut U {
1028+
#[inline(always)]
1029+
fn as_slice<'a>(&'a self) -> &'a [T] { AsSlice::as_slice(*self) }
10201030
}
10211031

10221032
#[unstable = "waiting for DST"]
@@ -1681,13 +1691,13 @@ impl<T: PartialEq> PartialEq for [T] {
16811691
impl<T: Eq> Eq for [T] {}
16821692

16831693
#[unstable = "waiting for DST"]
1684-
impl<T: PartialEq, V: AsSlice<T>> Equiv<V> for [T] {
1694+
impl<T: PartialEq, Sized? V: AsSlice<T>> Equiv<V> for [T] {
16851695
#[inline]
16861696
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
16871697
}
16881698

16891699
#[unstable = "waiting for DST"]
1690-
impl<'a,T:PartialEq, V: AsSlice<T>> Equiv<V> for &'a mut [T] {
1700+
impl<'a,T:PartialEq, Sized? V: AsSlice<T>> Equiv<V> for &'a mut [T] {
16911701
#[inline]
16921702
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
16931703
}

src/libgraphviz/maybe_owned_vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'a, T: Ord> Ord for MaybeOwnedVector<'a, T> {
8989
}
9090
}
9191

92-
impl<'a, T: PartialEq, V: AsSlice<T>> Equiv<V> for MaybeOwnedVector<'a, T> {
92+
impl<'a, T: PartialEq, Sized? V: AsSlice<T>> Equiv<V> for MaybeOwnedVector<'a, T> {
9393
fn equiv(&self, other: &V) -> bool {
9494
self.as_slice() == other.as_slice()
9595
}

src/libstd/path/posix.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
1616
use hash;
1717
use io::Writer;
1818
use iter::{DoubleEndedIterator, AdditiveIterator, Extend, Iterator, Map};
19+
use kinds::Sized;
1920
use option::{Option, None, Some};
2021
use str::{FromStr, Str};
2122
use str;
@@ -342,7 +343,7 @@ impl Path {
342343

343344
/// Returns a normalized byte vector representation of a path, by removing all empty
344345
/// components, and unnecessary . and .. components.
345-
fn normalize<V: AsSlice<u8>>(v: V) -> Vec<u8> {
346+
fn normalize<Sized? V: AsSlice<u8>>(v: &V) -> Vec<u8> {
346347
// borrowck is being very picky
347348
let val = {
348349
let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;

0 commit comments

Comments
 (0)