From 004db80afe08b28d79741c486ceb8398e6725829 Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Tue, 4 Nov 2014 15:31:46 -0800 Subject: [PATCH] libcore: DST-ify AsSlice This commit 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` by value. These should now be taken by reference: ```rust fn foo>(v: &V) { .. } ``` A few std lib functions have been changed accordingly. [breaking-change] --- src/libcollections/slice.rs | 2 +- src/libcollections/vec.rs | 5 +++-- src/libcore/slice.rs | 20 +++++++++++++++----- src/libgraphviz/maybe_owned_vec.rs | 2 +- src/libstd/path/posix.rs | 3 ++- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 5e341ba8b04..e077f7f6021 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -121,7 +121,7 @@ pub trait VectorVector for Sized? { fn connect_vec(&self, sep: &T) -> Vec; } -impl> VectorVector for [V] { +impl<'a, T: Clone, V: AsSlice> VectorVector for [V] { fn concat_vec(&self) -> Vec { let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len()); let mut result = Vec::with_capacity(size); diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 7111a077630..d6a21ef19a2 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -20,6 +20,7 @@ use core::cmp::max; use core::default::Default; use core::fmt; use core::kinds::marker::{ContravariantLifetime, InvariantType}; +use core::kinds::Sized; use core::mem; use core::num::{Int, UnsignedInt}; use core::ops; @@ -516,7 +517,7 @@ impl PartialOrd for Vec { impl Eq for Vec {} #[experimental] -impl> Equiv for Vec { +impl> Equiv for Vec { #[inline] fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() } } @@ -1181,7 +1182,7 @@ impl AsSlice for Vec { } } -impl> Add> for Vec { +impl> Add> for Vec { #[inline] fn add(&self, rhs: &V) -> Vec { let mut res = Vec::with_capacity(self.len() + rhs.as_slice().len()); diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 48e52fab51c..7a3e06e7eb4 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -1008,15 +1008,25 @@ impl CloneSlicePrelude for [T] { /// Data that is viewable as a slice. #[unstable = "may merge with other traits"] -pub trait AsSlice { +pub trait AsSlice for Sized? { /// Work with `self` as a slice. fn as_slice<'a>(&'a self) -> &'a [T]; } #[unstable = "trait is unstable"] -impl<'a,T> AsSlice for &'a [T] { +impl AsSlice for [T] { #[inline(always)] - fn as_slice<'a>(&'a self) -> &'a [T] { *self } + fn as_slice<'a>(&'a self) -> &'a [T] { self } +} + +impl<'a, T, Sized? U: AsSlice> AsSlice for &'a U { + #[inline(always)] + fn as_slice<'a>(&'a self) -> &'a [T] { AsSlice::as_slice(*self) } +} + +impl<'a, T, Sized? U: AsSlice> AsSlice for &'a mut U { + #[inline(always)] + fn as_slice<'a>(&'a self) -> &'a [T] { AsSlice::as_slice(*self) } } #[unstable = "waiting for DST"] @@ -1681,13 +1691,13 @@ impl PartialEq for [T] { impl Eq for [T] {} #[unstable = "waiting for DST"] -impl> Equiv for [T] { +impl> Equiv for [T] { #[inline] fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() } } #[unstable = "waiting for DST"] -impl<'a,T:PartialEq, V: AsSlice> Equiv for &'a mut [T] { +impl<'a,T:PartialEq, Sized? V: AsSlice> Equiv for &'a mut [T] { #[inline] fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() } } diff --git a/src/libgraphviz/maybe_owned_vec.rs b/src/libgraphviz/maybe_owned_vec.rs index 5d97e9787e8..70b3971c6b8 100644 --- a/src/libgraphviz/maybe_owned_vec.rs +++ b/src/libgraphviz/maybe_owned_vec.rs @@ -89,7 +89,7 @@ impl<'a, T: Ord> Ord for MaybeOwnedVector<'a, T> { } } -impl<'a, T: PartialEq, V: AsSlice> Equiv for MaybeOwnedVector<'a, T> { +impl<'a, T: PartialEq, Sized? V: AsSlice> Equiv for MaybeOwnedVector<'a, T> { fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() } diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index 3e013ba20c4..2b444fdc32b 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -16,6 +16,7 @@ use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use hash; use io::Writer; use iter::{DoubleEndedIterator, AdditiveIterator, Extend, Iterator, Map}; +use kinds::Sized; use option::{Option, None, Some}; use str::{FromStr, Str}; use str; @@ -342,7 +343,7 @@ impl Path { /// Returns a normalized byte vector representation of a path, by removing all empty /// components, and unnecessary . and .. components. - fn normalize>(v: V) -> Vec { + fn normalize>(v: &V) -> Vec { // borrowck is being very picky let val = { let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == SEP_BYTE;