From 1b487a890695e7d6dfbfe5dcd7d4fa0e8ca8003f Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 27 Aug 2014 21:46:52 -0400 Subject: Implement generalized object and type parameter bounds (Fixes #16462) --- src/libcore/any.rs | 4 ++-- src/libcore/cell.rs | 20 +++++++++++++++++ src/libcore/finally.rs | 7 ++++++ src/libcore/fmt/mod.rs | 6 ++--- src/libcore/iter.rs | 14 +++++++++--- src/libcore/lib.rs | 2 +- src/libcore/mem.rs | 4 ++-- src/libcore/slice.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++--- 8 files changed, 104 insertions(+), 14 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/any.rs b/src/libcore/any.rs index 1809988847b..625b89b3bae 100644 --- a/src/libcore/any.rs +++ b/src/libcore/any.rs @@ -132,7 +132,7 @@ pub trait AnyRefExt<'a> { } #[stable] -impl<'a> AnyRefExt<'a> for &'a Any { +impl<'a> AnyRefExt<'a> for &'a Any+'a { #[inline] #[stable] fn is(self) -> bool { @@ -181,7 +181,7 @@ pub trait AnyMutRefExt<'a> { } #[stable] -impl<'a> AnyMutRefExt<'a> for &'a mut Any { +impl<'a> AnyMutRefExt<'a> for &'a mut Any+'a { #[inline] #[unstable = "naming conventions around acquiring references may change"] fn downcast_mut(self) -> Option<&'a mut T> { diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 2a7b1630edf..4cbe7d6d963 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -324,6 +324,16 @@ impl PartialEq for RefCell { /// Wraps a borrowed reference to a value in a `RefCell` box. #[unstable] +#[cfg(not(stage0))] +pub struct Ref<'b, T:'b> { + // FIXME #12808: strange name to try to avoid interfering with + // field accesses of the contained type via Deref + _parent: &'b RefCell +} + +/// Dox. +#[unstable] +#[cfg(stage0)] pub struct Ref<'b, T> { // FIXME #12808: strange name to try to avoid interfering with // field accesses of the contained type via Deref @@ -369,6 +379,16 @@ pub fn clone_ref<'b, T>(orig: &Ref<'b, T>) -> Ref<'b, T> { /// Wraps a mutable borrowed reference to a value in a `RefCell` box. #[unstable] +#[cfg(not(stage0))] +pub struct RefMut<'b, T:'b> { + // FIXME #12808: strange name to try to avoid interfering with + // field accesses of the contained type via Deref + _parent: &'b RefCell +} + +/// Dox. +#[unstable] +#[cfg(stage0)] pub struct RefMut<'b, T> { // FIXME #12808: strange name to try to avoid interfering with // field accesses of the contained type via Deref diff --git a/src/libcore/finally.rs b/src/libcore/finally.rs index 514b3f90df7..c36150eb964 100644 --- a/src/libcore/finally.rs +++ b/src/libcore/finally.rs @@ -102,6 +102,13 @@ pub fn try_finally(mutate: &mut T, try_fn(&mut *f.mutate, drop) } +#[cfg(not(stage0))] +struct Finallyalizer<'a,A:'a> { + mutate: &'a mut A, + dtor: |&mut A|: 'a +} + +#[cfg(stage0)] struct Finallyalizer<'a,A> { mutate: &'a mut A, dtor: |&mut A|: 'a diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 85a289f1a30..f7ff92f5ce3 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -92,7 +92,7 @@ pub struct Formatter<'a> { /// Optionally specified precision for numeric types pub precision: Option, - buf: &'a mut FormatWriter, + buf: &'a mut FormatWriter+'a, curarg: slice::Items<'a, Argument<'a>>, args: &'a [Argument<'a>], } @@ -524,7 +524,7 @@ impl<'a, T: Show> Show for &'a T { impl<'a, T: Show> Show for &'a mut T { fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) } } -impl<'a> Show for &'a Show { +impl<'a> Show for &'a Show+'a { fn fmt(&self, f: &mut Formatter) -> Result { (*self).fmt(f) } } @@ -692,7 +692,7 @@ macro_rules! tuple ( tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } -impl<'a> Show for &'a any::Any { +impl<'a> Show for &'a any::Any+'a { fn fmt(&self, f: &mut Formatter) -> Result { f.pad("&Any") } } diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index b2bd8d46fb5..7df8a7864d9 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -673,7 +673,7 @@ pub trait MutableDoubleEndedIterator { fn reverse_(&mut self); } -impl<'a, A, T: DoubleEndedIterator<&'a mut A>> MutableDoubleEndedIterator for T { +impl<'a, A:'a, T: DoubleEndedIterator<&'a mut A>> MutableDoubleEndedIterator for T { // FIXME: #5898: should be called `reverse` /// Use an iterator to reverse a container in-place fn reverse_(&mut self) { @@ -777,18 +777,26 @@ impl + RandomAccessIterator> RandomAccessIterato /// A mutable reference to an iterator #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] +#[cfg(not(stage0))] +pub struct ByRef<'a, T:'a> { + iter: &'a mut T +} + +/// Dox +#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] +#[cfg(stage0)] pub struct ByRef<'a, T> { iter: &'a mut T } -impl<'a, A, T: Iterator> Iterator for ByRef<'a, T> { +impl<'a, A, T: Iterator+'a> Iterator for ByRef<'a, T> { #[inline] fn next(&mut self) -> Option { self.iter.next() } #[inline] fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } -impl<'a, A, T: DoubleEndedIterator> DoubleEndedIterator for ByRef<'a, T> { +impl<'a, A, T: DoubleEndedIterator+'a> DoubleEndedIterator for ByRef<'a, T> { #[inline] fn next_back(&mut self) -> Option { self.iter.next_back() } } diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 7e2ea492d4c..050e2348111 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -58,7 +58,7 @@ #![no_std] #![feature(globs, intrinsics, lang_items, macro_rules, managed_boxes, phase)] -#![feature(simd, unsafe_destructor)] +#![feature(simd, unsafe_destructor, issue_5723_bootstrap)] #![deny(missing_doc)] mod macros; diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index f0c39766ebb..947fa2ec92e 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -369,7 +369,7 @@ pub unsafe fn transmute_copy(src: &T) -> U { #[inline] #[unstable = "this function may be removed in the future due to its \ questionable utility"] -pub unsafe fn copy_lifetime<'a, S, T>(_ptr: &'a S, ptr: &T) -> &'a T { +pub unsafe fn copy_lifetime<'a, S, T:'a>(_ptr: &'a S, ptr: &T) -> &'a T { transmute(ptr) } @@ -377,7 +377,7 @@ pub unsafe fn copy_lifetime<'a, S, T>(_ptr: &'a S, ptr: &T) -> &'a T { #[inline] #[unstable = "this function may be removed in the future due to its \ questionable utility"] -pub unsafe fn copy_mut_lifetime<'a, S, T>(_ptr: &'a mut S, +pub unsafe fn copy_mut_lifetime<'a, S, T:'a>(_ptr: &'a mut S, ptr: &mut T) -> &'a mut T { transmute(ptr) } diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 475c2e94ec7..5a70cd8c847 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -996,9 +996,6 @@ impl<'a, T> Default for &'a [T] { fn default() -> &'a [T] { &[] } } - - - // // Iterators // @@ -1128,7 +1125,16 @@ impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {} /// An iterator over the slices of a vector separated by elements that /// match a predicate function. +#[cfg(not(stage0))] #[experimental = "needs review"] +pub struct Splits<'a, T:'a> { + v: &'a [T], + pred: |t: &T|: 'a -> bool, + finished: bool +} + +/// Dox. +#[cfg(stage0)] pub struct Splits<'a, T> { v: &'a [T], pred: |t: &T|: 'a -> bool, @@ -1186,7 +1192,16 @@ impl<'a, T> DoubleEndedIterator<&'a [T]> for Splits<'a, T> { /// An iterator over the subslices of the vector which are separated /// by elements that match `pred`. +#[cfg(not(stage0))] #[experimental = "needs review"] +pub struct MutSplits<'a, T:'a> { + v: &'a mut [T], + pred: |t: &T|: 'a -> bool, + finished: bool +} + +/// Dox +#[cfg(stage0)] pub struct MutSplits<'a, T> { v: &'a mut [T], pred: |t: &T|: 'a -> bool, @@ -1255,7 +1270,16 @@ impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplits<'a, T> { /// An iterator over the slices of a vector separated by elements that /// match a predicate function, splitting at most a fixed number of times. +#[cfg(not(stage0))] #[experimental = "needs review"] +pub struct SplitsN<'a, T:'a> { + iter: Splits<'a, T>, + count: uint, + invert: bool +} + +/// Dox. +#[cfg(stage0)] pub struct SplitsN<'a, T> { iter: Splits<'a, T>, count: uint, @@ -1291,6 +1315,7 @@ impl<'a, T> Iterator<&'a [T]> for SplitsN<'a, T> { /// An iterator over the (overlapping) slices of length `size` within /// a vector. +#[cfg(stage0)] #[deriving(Clone)] #[experimental = "needs review"] pub struct Windows<'a, T> { @@ -1298,7 +1323,16 @@ pub struct Windows<'a, T> { size: uint } +/// An iterator over the (overlapping) slices of length `size` within +/// a vector. +#[cfg(not(stage0))] +#[deriving(Clone)] #[experimental = "needs review"] +pub struct Windows<'a, T:'a> { + v: &'a [T], + size: uint +} + impl<'a, T> Iterator<&'a [T]> for Windows<'a, T> { #[inline] fn next(&mut self) -> Option<&'a [T]> { @@ -1327,6 +1361,7 @@ impl<'a, T> Iterator<&'a [T]> for Windows<'a, T> { /// /// When the vector len is not evenly divided by the chunk size, /// the last slice of the iteration will be the remainder. +#[cfg(stage0)] #[deriving(Clone)] #[experimental = "needs review"] pub struct Chunks<'a, T> { @@ -1334,6 +1369,18 @@ pub struct Chunks<'a, T> { size: uint } +/// An iterator over a vector in (non-overlapping) chunks (`size` +/// elements at a time). +/// +/// When the vector len is not evenly divided by the chunk size, +/// the last slice of the iteration will be the remainder. +#[cfg(not(stage0))] +#[deriving(Clone)] +pub struct Chunks<'a, T:'a> { + v: &'a [T], + size: uint +} + #[experimental = "needs review"] impl<'a, T> Iterator<&'a [T]> for Chunks<'a, T> { #[inline] @@ -1400,7 +1447,15 @@ impl<'a, T> RandomAccessIterator<&'a [T]> for Chunks<'a, T> { /// An iterator over a vector in (non-overlapping) mutable chunks (`size` elements at a time). When /// the vector len is not evenly divided by the chunk size, the last slice of the iteration will be /// the remainder. +#[cfg(not(stage0))] #[experimental = "needs review"] +pub struct MutChunks<'a, T:'a> { + v: &'a mut [T], + chunk_size: uint +} + +/// Dox. +#[cfg(stage0)] pub struct MutChunks<'a, T> { v: &'a mut [T], chunk_size: uint -- cgit 1.4.1-3-g733a5