4242//!
4343//! ## Traits
4444//!
45- //! A number of traits add methods that allow you to accomplish tasks with slices.
46- //! These traits include `ImmutableSlice`, which is defined for `&[T]` types,
47- //! `MutableSlice`, defined for `&mut [T]` types, and `Slice` and `SliceMut`
48- //! which are defined for `[T]` .
45+ //! A number of traits add methods that allow you to accomplish tasks
46+ //! with slices, the most important being `SlicePrelude`. Other traits
47+ //! apply only to slices of elements satisfying certain bounds (like
48+ //! `Ord`) .
4949//!
5050//! An example is the `slice` method which enables slicing syntax `[a..b]` that
5151//! returns an immutable "view" into a `Vec` or another slice from the index
@@ -99,11 +99,11 @@ use core::iter::{range_step, MultiplicativeIterator};
9999
100100use vec:: Vec ;
101101
102- pub use core:: slice:: { Chunks , AsSlice , ImmutableSlice , ImmutablePartialEqSlice } ;
103- pub use core:: slice:: { ImmutableOrdSlice , MutableSlice , Items , MutItems } ;
102+ pub use core:: slice:: { Chunks , AsSlice , SlicePrelude , PartialEqSlicePrelude } ;
103+ pub use core:: slice:: { OrdSlicePrelude , SlicePrelude , Items , MutItems } ;
104104pub use core:: slice:: { ImmutableIntSlice , MutableIntSlice } ;
105105pub use core:: slice:: { MutSplits , MutChunks , Splits } ;
106- pub use core:: slice:: { bytes, mut_ref_slice, ref_slice, MutableCloneableSlice } ;
106+ pub use core:: slice:: { bytes, mut_ref_slice, ref_slice, CloneSlicePrelude } ;
107107pub use core:: slice:: { Found , NotFound } ;
108108
109109// Functional utilities
@@ -266,29 +266,13 @@ impl<T: Clone> Iterator<Vec<T>> for Permutations<T> {
266266 }
267267}
268268
269- /// Extension methods for vector slices with cloneable elements
270- pub trait CloneableVector < T > for Sized ? {
271- /// Copies `self` into a new `Vec`.
272- fn to_vec ( & self ) -> Vec < T > ;
273- }
274-
275- impl < T : Clone > CloneableVector < T > for [ T ] {
276- /// Returns a copy of `v`.
277- #[ inline]
278- fn to_vec ( & self ) -> Vec < T > {
279- let mut vector = Vec :: with_capacity ( self . len ( ) ) ;
280- vector. push_all ( self ) ;
281- vector
282- }
283- }
284-
285- #[ experimental]
286- pub trait BoxedSlice < T > {
269+ /// Extension methods for boxed slices.
270+ pub trait BoxedSlicePrelude < T > {
287271 /// Convert `self` into a vector without clones or allocation.
288272 fn into_vec ( self ) -> Vec < T > ;
289273}
290274
291- impl < T > BoxedSlice < T > for Box < [ T ] > {
275+ impl < T > BoxedSlicePrelude < T > for Box < [ T ] > {
292276 #[ experimental]
293277 fn into_vec ( mut self ) -> Vec < T > {
294278 unsafe {
@@ -299,8 +283,11 @@ impl<T> BoxedSlice<T> for Box<[T]> {
299283 }
300284}
301285
302- /// Extension methods for vectors containing `Clone` elements.
303- pub trait ImmutableCloneableVector < T > for Sized ? {
286+ /// Allocating extension methods for slices containing `Clone` elements.
287+ pub trait CloneSliceAllocPrelude < T > for Sized ? {
288+ /// Copies `self` into a new `Vec`.
289+ fn to_vec ( & self ) -> Vec < T > ;
290+
304291 /// Partitions the vector into two vectors `(a, b)`, where all
305292 /// elements of `a` satisfy `f` and all elements of `b` do not.
306293 fn partitioned ( & self , f: |& T | -> bool) -> ( Vec < T > , Vec < T > ) ;
@@ -332,7 +319,16 @@ pub trait ImmutableCloneableVector<T> for Sized? {
332319 fn permutations ( & self ) -> Permutations < T > ;
333320}
334321
335- impl < T : Clone > ImmutableCloneableVector < T > for [ T ] {
322+ impl < T : Clone > CloneSliceAllocPrelude < T > for [ T ] {
323+ /// Returns a copy of `v`.
324+ #[ inline]
325+ fn to_vec ( & self ) -> Vec < T > {
326+ let mut vector = Vec :: with_capacity ( self . len ( ) ) ;
327+ vector. push_all ( self ) ;
328+ vector
329+ }
330+
331+
336332 #[ inline]
337333 fn partitioned ( & self , f: |& T | -> bool) -> ( Vec < T > , Vec < T > ) {
338334 let mut lefts = Vec :: new ( ) ;
@@ -562,9 +558,36 @@ fn merge_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) {
562558 }
563559}
564560
565- /// Extension methods for vectors such that their elements are
566- /// mutable.
567- pub trait MutableSliceAllocating < T > for Sized ? {
561+ /// Allocating extension methods for slices on Ord values.
562+ #[ experimental = "likely to merge with other traits" ]
563+ pub trait OrdSliceAllocPrelude < T > for Sized ? {
564+ /// Sorts the slice, in place.
565+ ///
566+ /// This is equivalent to `self.sort_by(|a, b| a.cmp(b))`.
567+ ///
568+ /// # Example
569+ ///
570+ /// ```rust
571+ /// let mut v = [-5i, 4, 1, -3, 2];
572+ ///
573+ /// v.sort();
574+ /// assert!(v == [-5i, -3, 1, 2, 4]);
575+ /// ```
576+ #[ experimental]
577+ fn sort ( & mut self ) ;
578+ }
579+
580+ impl < T : Ord > OrdSliceAllocPrelude < T > for [ T ] {
581+ #[ experimental]
582+ #[ inline]
583+ fn sort ( & mut self ) {
584+ self . sort_by ( |a, b| a. cmp ( b) )
585+ }
586+ }
587+
588+ /// Allocating extension methods for slices.
589+ #[ experimental = "likely to merge with other traits" ]
590+ pub trait SliceAllocPrelude < T > for Sized ? {
568591 /// Sorts the slice, in place, using `compare` to compare
569592 /// elements.
570593 ///
@@ -608,7 +631,7 @@ pub trait MutableSliceAllocating<T> for Sized? {
608631 fn move_from ( & mut self , src : Vec < T > , start : uint , end : uint ) -> uint ;
609632}
610633
611- impl < T > MutableSliceAllocating < T > for [ T ] {
634+ impl < T > SliceAllocPrelude < T > for [ T ] {
612635 #[ inline]
613636 fn sort_by ( & mut self , compare : |& T , & T | -> Ordering ) {
614637 merge_sort ( self , compare)
@@ -623,127 +646,6 @@ impl<T> MutableSliceAllocating<T> for [T] {
623646 }
624647}
625648
626- /// Methods for mutable vectors with orderable elements, such as
627- /// in-place sorting.
628- pub trait MutableOrdSlice < T > for Sized ? {
629- /// Sorts the slice, in place.
630- ///
631- /// This is equivalent to `self.sort_by(|a, b| a.cmp(b))`.
632- ///
633- /// # Example
634- ///
635- /// ```rust
636- /// let mut v = [-5i, 4, 1, -3, 2];
637- ///
638- /// v.sort();
639- /// assert!(v == [-5i, -3, 1, 2, 4]);
640- /// ```
641- fn sort ( & mut self ) ;
642-
643- /// Mutates the slice to the next lexicographic permutation.
644- ///
645- /// Returns `true` if successful and `false` if the slice is at the
646- /// last-ordered permutation.
647- ///
648- /// # Example
649- ///
650- /// ```rust
651- /// let v: &mut [_] = &mut [0i, 1, 2];
652- /// v.next_permutation();
653- /// let b: &mut [_] = &mut [0i, 2, 1];
654- /// assert!(v == b);
655- /// v.next_permutation();
656- /// let b: &mut [_] = &mut [1i, 0, 2];
657- /// assert!(v == b);
658- /// ```
659- fn next_permutation ( & mut self ) -> bool ;
660-
661- /// Mutates the slice to the previous lexicographic permutation.
662- ///
663- /// Returns `true` if successful and `false` if the slice is at the
664- /// first-ordered permutation.
665- ///
666- /// # Example
667- ///
668- /// ```rust
669- /// let v: &mut [_] = &mut [1i, 0, 2];
670- /// v.prev_permutation();
671- /// let b: &mut [_] = &mut [0i, 2, 1];
672- /// assert!(v == b);
673- /// v.prev_permutation();
674- /// let b: &mut [_] = &mut [0i, 1, 2];
675- /// assert!(v == b);
676- /// ```
677- fn prev_permutation ( & mut self ) -> bool ;
678- }
679-
680- impl < T : Ord > MutableOrdSlice < T > for [ T ] {
681- #[ inline]
682- fn sort ( & mut self ) {
683- self . sort_by ( |a, b| a. cmp ( b) )
684- }
685-
686- fn next_permutation ( & mut self ) -> bool {
687- // These cases only have 1 permutation each, so we can't do anything.
688- if self . len ( ) < 2 { return false ; }
689-
690- // Step 1: Identify the longest, rightmost weakly decreasing part of the vector
691- let mut i = self . len ( ) - 1 ;
692- while i > 0 && self [ i-1 ] >= self [ i] {
693- i -= 1 ;
694- }
695-
696- // If that is the entire vector, this is the last-ordered permutation.
697- if i == 0 {
698- return false ;
699- }
700-
701- // Step 2: Find the rightmost element larger than the pivot (i-1)
702- let mut j = self . len ( ) - 1 ;
703- while j >= i && self [ j] <= self [ i-1 ] {
704- j -= 1 ;
705- }
706-
707- // Step 3: Swap that element with the pivot
708- self . swap ( j, i-1 ) ;
709-
710- // Step 4: Reverse the (previously) weakly decreasing part
711- self [ mut i..] . reverse ( ) ;
712-
713- true
714- }
715-
716- fn prev_permutation ( & mut self ) -> bool {
717- // These cases only have 1 permutation each, so we can't do anything.
718- if self . len ( ) < 2 { return false ; }
719-
720- // Step 1: Identify the longest, rightmost weakly increasing part of the vector
721- let mut i = self . len ( ) - 1 ;
722- while i > 0 && self [ i-1 ] <= self [ i] {
723- i -= 1 ;
724- }
725-
726- // If that is the entire vector, this is the first-ordered permutation.
727- if i == 0 {
728- return false ;
729- }
730-
731- // Step 2: Reverse the weakly increasing part
732- self [ mut i..] . reverse ( ) ;
733-
734- // Step 3: Find the rightmost element equal to or bigger than the pivot (i-1)
735- let mut j = self . len ( ) - 1 ;
736- while j >= i && self [ j-1 ] < self [ i-1 ] {
737- j -= 1 ;
738- }
739-
740- // Step 4: Swap that element with the pivot
741- self . swap ( i-1 , j) ;
742-
743- true
744- }
745- }
746-
747649/// Unsafe operations
748650pub mod raw {
749651 pub use core:: slice:: raw:: { buf_as_slice, mut_buf_as_slice} ;
0 commit comments