From cfafc1b7377d34d8c60db7cd386836d39b80af41 Mon Sep 17 00:00:00 2001 From: Aaron Turon Date: Sun, 2 Nov 2014 17:04:32 -0800 Subject: [PATCH] Prelude: rename and consolidate extension traits This commit renames a number of extension traits for slices and string slices, now that they have been refactored for DST. In many cases, multiple extension traits could now be consolidated. Further consolidation will be possible with generalized where clauses. The renamings are consistent with the [new `-Prelude` suffix](https://github.com/rust-lang/rfcs/pull/344). There are probably a few more candidates for being renamed this way, but that is left for API stabilization of the relevant modules. Because this renames traits, it is a: [breaking-change] However, I do not expect any code that currently uses the standard library to actually break. Closes #17917 --- src/etc/unicode.py | 10 +- src/libcollections/hash/mod.rs | 2 +- src/libcollections/hash/sip.rs | 2 +- src/libcollections/slice.rs | 210 +++--------- src/libcollections/str.rs | 16 +- src/libcollections/string.rs | 8 +- src/libcollections/vec.rs | 4 +- src/libcore/char.rs | 2 +- src/libcore/fmt/float.rs | 4 +- src/libcore/fmt/mod.rs | 4 +- src/libcore/fmt/num.rs | 2 +- src/libcore/prelude.rs | 6 +- src/libcore/slice.rs | 529 +++++++++++++++++------------ src/libcore/str.rs | 13 +- src/libgraphviz/maybe_owned_vec.rs | 13 +- src/libnative/io/process.rs | 2 +- src/libregex/vm.rs | 2 +- src/librlibc/lib.rs | 4 +- src/librustrt/args.rs | 2 +- src/librustrt/c_str.rs | 6 +- src/libstd/ascii.rs | 6 +- src/libstd/collections/hash/set.rs | 2 +- src/libstd/dynamic_lib.rs | 6 +- src/libstd/io/buffered.rs | 4 +- src/libstd/io/comm_adapters.rs | 2 +- src/libstd/io/extensions.rs | 4 +- src/libstd/io/fs.rs | 4 +- src/libstd/io/mem.rs | 4 +- src/libstd/io/mod.rs | 4 +- src/libstd/io/net/ip.rs | 4 +- src/libstd/io/stdio.rs | 4 +- src/libstd/macros.rs | 2 +- src/libstd/num/strconv.rs | 4 +- src/libstd/os.rs | 10 +- src/libstd/path/mod.rs | 6 +- src/libstd/path/posix.rs | 6 +- src/libstd/path/windows.rs | 4 +- src/libstd/prelude.rs | 13 +- src/libstd/rand/os.rs | 4 +- src/libstd/rand/reader.rs | 2 +- src/libstd/rt/backtrace.rs | 10 +- src/libunicode/lib.rs | 2 +- src/libunicode/normalize.rs | 2 +- src/libunicode/tables.rs | 12 +- src/libunicode/u_str.rs | 8 +- 45 files changed, 483 insertions(+), 487 deletions(-) diff --git a/src/etc/unicode.py b/src/etc/unicode.py index 52e02febd96..fb003f47c3e 100755 --- a/src/etc/unicode.py +++ b/src/etc/unicode.py @@ -293,7 +293,7 @@ def emit_bsearch_range_table(f): f.write(""" fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool { use core::cmp::{Equal, Less, Greater}; - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; r.binary_search(|&(lo,hi)| { if lo <= c && c <= hi { Equal } else if hi < c { Less } @@ -351,7 +351,7 @@ def emit_conversions_module(f, lowerupper, upperlower): f.write("pub mod conversions {") f.write(""" use core::cmp::{Equal, Less, Greater}; - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; use core::tuple::Tuple2; use core::option::{Option, Some, None}; use core::slice; @@ -390,7 +390,7 @@ def emit_conversions_module(f, lowerupper, upperlower): def emit_grapheme_module(f, grapheme_table, grapheme_cats): f.write("""pub mod grapheme { - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; use core::slice; #[allow(non_camel_case_types)] @@ -430,7 +430,7 @@ def emit_grapheme_module(f, grapheme_table, grapheme_cats): def emit_charwidth_module(f, width_table): f.write("pub mod charwidth {\n") f.write(" use core::option::{Option, Some, None};\n") - f.write(" use core::slice::ImmutableSlice;\n") + f.write(" use core::slice::SlicePrelude;\n") f.write(" use core::slice;\n") f.write(""" fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 { @@ -530,7 +530,7 @@ def emit_norm_module(f, canon, compat, combine, norm_props): f.write(""" fn bsearch_range_value_table(c: char, r: &'static [(char, char, u8)]) -> u8 { use core::cmp::{Equal, Less, Greater}; - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; use core::slice; match r.binary_search(|&(lo, hi, _)| { if lo <= c && c <= hi { Equal } diff --git a/src/libcollections/hash/mod.rs b/src/libcollections/hash/mod.rs index 6c1a4019bd8..43faaac3952 100644 --- a/src/libcollections/hash/mod.rs +++ b/src/libcollections/hash/mod.rs @@ -290,7 +290,7 @@ mod tests { use core::kinds::Sized; use std::mem; - use slice::ImmutableSlice; + use slice::SlicePrelude; use super::{Hash, Hasher, Writer}; struct MyWriterHasher; diff --git a/src/libcollections/hash/sip.rs b/src/libcollections/hash/sip.rs index dd105c44ad3..788ea13678f 100644 --- a/src/libcollections/hash/sip.rs +++ b/src/libcollections/hash/sip.rs @@ -273,7 +273,7 @@ mod tests { use str::Str; use string::String; - use slice::{AsSlice, ImmutableSlice}; + use slice::{AsSlice, SlicePrelude}; use vec::Vec; use super::super::{Hash, Writer}; diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index e4af5795e1c..0d9a166c7ff 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -42,10 +42,10 @@ //! //! ## Traits //! -//! A number of traits add methods that allow you to accomplish tasks with slices. -//! These traits include `ImmutableSlice`, which is defined for `&[T]` types, -//! `MutableSlice`, defined for `&mut [T]` types, and `Slice` and `SliceMut` -//! which are defined for `[T]`. +//! A number of traits add methods that allow you to accomplish tasks +//! with slices, the most important being `SlicePrelude`. Other traits +//! apply only to slices of elements satisfying certain bounds (like +//! `Ord`). //! //! An example is the `slice` method which enables slicing syntax `[a..b]` that //! returns an immutable "view" into a `Vec` or another slice from the index @@ -99,11 +99,11 @@ use core::iter::{range_step, MultiplicativeIterator}; use vec::Vec; -pub use core::slice::{Chunks, AsSlice, ImmutableSlice, ImmutablePartialEqSlice}; -pub use core::slice::{ImmutableOrdSlice, MutableSlice, Items, MutItems}; +pub use core::slice::{Chunks, AsSlice, SlicePrelude, PartialEqSlicePrelude}; +pub use core::slice::{OrdSlicePrelude, SlicePrelude, Items, MutItems}; pub use core::slice::{ImmutableIntSlice, MutableIntSlice}; pub use core::slice::{MutSplits, MutChunks, Splits}; -pub use core::slice::{bytes, mut_ref_slice, ref_slice, MutableCloneableSlice}; +pub use core::slice::{bytes, mut_ref_slice, ref_slice, CloneSlicePrelude}; pub use core::slice::{Found, NotFound}; // Functional utilities @@ -266,29 +266,13 @@ impl Iterator> for Permutations { } } -/// Extension methods for vector slices with cloneable elements -pub trait CloneableVector for Sized? { - /// Copies `self` into a new `Vec`. - fn to_vec(&self) -> Vec; -} - -impl CloneableVector for [T] { - /// Returns a copy of `v`. - #[inline] - fn to_vec(&self) -> Vec { - let mut vector = Vec::with_capacity(self.len()); - vector.push_all(self); - vector - } -} - -#[experimental] -pub trait BoxedSlice { +/// Extension methods for boxed slices. +pub trait BoxedSlicePrelude { /// Convert `self` into a vector without clones or allocation. fn into_vec(self) -> Vec; } -impl BoxedSlice for Box<[T]> { +impl BoxedSlicePrelude for Box<[T]> { #[experimental] fn into_vec(mut self) -> Vec { unsafe { @@ -299,8 +283,11 @@ impl BoxedSlice for Box<[T]> { } } -/// Extension methods for vectors containing `Clone` elements. -pub trait ImmutableCloneableVector for Sized? { +/// Allocating extension methods for slices containing `Clone` elements. +pub trait CloneSliceAllocPrelude for Sized? { + /// Copies `self` into a new `Vec`. + fn to_vec(&self) -> Vec; + /// Partitions the vector into two vectors `(a, b)`, where all /// elements of `a` satisfy `f` and all elements of `b` do not. fn partitioned(&self, f: |&T| -> bool) -> (Vec, Vec); @@ -332,7 +319,16 @@ pub trait ImmutableCloneableVector for Sized? { fn permutations(&self) -> Permutations; } -impl ImmutableCloneableVector for [T] { +impl CloneSliceAllocPrelude for [T] { + /// Returns a copy of `v`. + #[inline] + fn to_vec(&self) -> Vec { + let mut vector = Vec::with_capacity(self.len()); + vector.push_all(self); + vector + } + + #[inline] fn partitioned(&self, f: |&T| -> bool) -> (Vec, Vec) { let mut lefts = Vec::new(); @@ -562,9 +558,36 @@ fn merge_sort(v: &mut [T], compare: |&T, &T| -> Ordering) { } } -/// Extension methods for vectors such that their elements are -/// mutable. -pub trait MutableSliceAllocating for Sized? { +/// Allocating extension methods for slices on Ord values. +#[experimental = "likely to merge with other traits"] +pub trait OrdSliceAllocPrelude for Sized? { + /// Sorts the slice, in place. + /// + /// This is equivalent to `self.sort_by(|a, b| a.cmp(b))`. + /// + /// # Example + /// + /// ```rust + /// let mut v = [-5i, 4, 1, -3, 2]; + /// + /// v.sort(); + /// assert!(v == [-5i, -3, 1, 2, 4]); + /// ``` + #[experimental] + fn sort(&mut self); +} + +impl OrdSliceAllocPrelude for [T] { + #[experimental] + #[inline] + fn sort(&mut self) { + self.sort_by(|a, b| a.cmp(b)) + } +} + +/// Allocating extension methods for slices. +#[experimental = "likely to merge with other traits"] +pub trait SliceAllocPrelude for Sized? { /// Sorts the slice, in place, using `compare` to compare /// elements. /// @@ -608,7 +631,7 @@ pub trait MutableSliceAllocating for Sized? { fn move_from(&mut self, src: Vec, start: uint, end: uint) -> uint; } -impl MutableSliceAllocating for [T] { +impl SliceAllocPrelude for [T] { #[inline] fn sort_by(&mut self, compare: |&T, &T| -> Ordering) { merge_sort(self, compare) @@ -623,127 +646,6 @@ impl MutableSliceAllocating for [T] { } } -/// Methods for mutable vectors with orderable elements, such as -/// in-place sorting. -pub trait MutableOrdSlice for Sized? { - /// Sorts the slice, in place. - /// - /// This is equivalent to `self.sort_by(|a, b| a.cmp(b))`. - /// - /// # Example - /// - /// ```rust - /// let mut v = [-5i, 4, 1, -3, 2]; - /// - /// v.sort(); - /// assert!(v == [-5i, -3, 1, 2, 4]); - /// ``` - fn sort(&mut self); - - /// Mutates the slice to the next lexicographic permutation. - /// - /// Returns `true` if successful and `false` if the slice is at the - /// last-ordered permutation. - /// - /// # Example - /// - /// ```rust - /// let v: &mut [_] = &mut [0i, 1, 2]; - /// v.next_permutation(); - /// let b: &mut [_] = &mut [0i, 2, 1]; - /// assert!(v == b); - /// v.next_permutation(); - /// let b: &mut [_] = &mut [1i, 0, 2]; - /// assert!(v == b); - /// ``` - fn next_permutation(&mut self) -> bool; - - /// Mutates the slice to the previous lexicographic permutation. - /// - /// Returns `true` if successful and `false` if the slice is at the - /// first-ordered permutation. - /// - /// # Example - /// - /// ```rust - /// let v: &mut [_] = &mut [1i, 0, 2]; - /// v.prev_permutation(); - /// let b: &mut [_] = &mut [0i, 2, 1]; - /// assert!(v == b); - /// v.prev_permutation(); - /// let b: &mut [_] = &mut [0i, 1, 2]; - /// assert!(v == b); - /// ``` - fn prev_permutation(&mut self) -> bool; -} - -impl MutableOrdSlice for [T] { - #[inline] - fn sort(&mut self) { - self.sort_by(|a, b| a.cmp(b)) - } - - fn next_permutation(&mut self) -> bool { - // These cases only have 1 permutation each, so we can't do anything. - if self.len() < 2 { return false; } - - // Step 1: Identify the longest, rightmost weakly decreasing part of the vector - let mut i = self.len() - 1; - while i > 0 && self[i-1] >= self[i] { - i -= 1; - } - - // If that is the entire vector, this is the last-ordered permutation. - if i == 0 { - return false; - } - - // Step 2: Find the rightmost element larger than the pivot (i-1) - let mut j = self.len() - 1; - while j >= i && self[j] <= self[i-1] { - j -= 1; - } - - // Step 3: Swap that element with the pivot - self.swap(j, i-1); - - // Step 4: Reverse the (previously) weakly decreasing part - self[mut i..].reverse(); - - true - } - - fn prev_permutation(&mut self) -> bool { - // These cases only have 1 permutation each, so we can't do anything. - if self.len() < 2 { return false; } - - // Step 1: Identify the longest, rightmost weakly increasing part of the vector - let mut i = self.len() - 1; - while i > 0 && self[i-1] <= self[i] { - i -= 1; - } - - // If that is the entire vector, this is the first-ordered permutation. - if i == 0 { - return false; - } - - // Step 2: Reverse the weakly increasing part - self[mut i..].reverse(); - - // Step 3: Find the rightmost element equal to or bigger than the pivot (i-1) - let mut j = self.len() - 1; - while j >= i && self[j-1] < self[i-1] { - j -= 1; - } - - // Step 4: Swap that element with the pivot - self.swap(i-1, j); - - true - } -} - /// Unsafe operations pub mod raw { pub use core::slice::raw::{buf_as_slice, mut_buf_as_slice}; diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index cdca0d10eed..ac840503aa2 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -56,8 +56,8 @@ use core::fmt; use core::cmp; use core::iter::AdditiveIterator; use core::kinds::Sized; -use core::prelude::{Char, Clone, Eq, Equiv, ImmutableSlice}; -use core::prelude::{Iterator, MutableSlice, None, Option, Ord, Ordering}; +use core::prelude::{Char, Clone, Eq, Equiv}; +use core::prelude::{Iterator, SlicePrelude, None, Option, Ord, Ordering}; use core::prelude::{PartialEq, PartialOrd, Result, AsSlice, Some, Tuple2}; use core::prelude::{range}; @@ -73,8 +73,8 @@ pub use core::str::{CharSplitsN, AnyLines, MatchIndices, StrSplits}; pub use core::str::{Utf16CodeUnits, eq_slice, is_utf8, is_utf16, Utf16Items}; pub use core::str::{Utf16Item, ScalarValue, LoneSurrogate, utf16_items}; pub use core::str::{truncate_utf16_at_nul, utf8_char_width, CharRange}; -pub use core::str::{Str, StrSlice}; -pub use unicode::str::{UnicodeStrSlice, Words, Graphemes, GraphemeIndices}; +pub use core::str::{Str, StrPrelude}; +pub use unicode::str::{UnicodeStrPrelude, Words, Graphemes, GraphemeIndices}; /* Section: Creating a string @@ -790,10 +790,10 @@ mod tests { use std::iter::{Iterator, DoubleEndedIterator}; use super::*; - use std::slice::{AsSlice, ImmutableSlice}; + use std::slice::{AsSlice, SlicePrelude}; use string::String; use vec::Vec; - use slice::CloneableVector; + use slice::CloneSliceAllocPrelude; use unicode::char::UnicodeChar; @@ -2240,8 +2240,8 @@ mod bench { use test::black_box; use super::*; use std::iter::{Iterator, DoubleEndedIterator}; - use std::str::StrSlice; - use std::slice::ImmutableSlice; + use std::str::StrPrelude; + use std::slice::SlicePrelude; #[bench] fn char_iterator(b: &mut Bencher) { diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index b3c83ba5559..b32760818b7 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -23,7 +23,7 @@ use core::ops; use core::raw::Slice as RawSlice; use hash; -use slice::CloneableVector; +use slice::CloneSliceAllocPrelude; use str; use str::{CharRange, StrAllocating, MaybeOwned, Owned}; use str::Slice as MaybeOwnedSlice; // So many `Slice`s... @@ -815,7 +815,7 @@ pub mod raw { /// * A raw pointer is dereferenced and transmuted to `&[u8]`; /// * The slice is not checked to see whether it contains valid UTF-8. pub unsafe fn from_buf_len(buf: *const u8, len: uint) -> String { - use slice::CloneableVector; + use slice::CloneSliceAllocPrelude; let slice: &[u8] = mem::transmute(Slice { data: buf, len: len, @@ -851,10 +851,10 @@ mod tests { use test::Bencher; use str; - use str::{Str, StrSlice, Owned}; + use str::{Str, StrPrelude, Owned}; use super::{as_string, String}; use vec::Vec; - use slice::CloneableVector; + use slice::CloneSliceAllocPrelude; #[test] fn test_as_string() { diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 4b6921ed0c0..39f636a2438 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -27,7 +27,7 @@ use core::ptr; use core::raw::Slice as RawSlice; use core::uint; -use slice::{CloneableVector}; +use slice::{CloneSliceAllocPrelude}; /// An owned, growable vector. /// @@ -1389,7 +1389,7 @@ pub fn as_vec<'a, T>(x: &'a [T]) -> DerefVec<'a, T> { pub mod raw { use super::Vec; use core::ptr; - use core::slice::MutableSlice; + use core::slice::SlicePrelude; /// Constructs a vector from an unsafe pointer to a buffer. /// diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 0fab0e96fe6..f769eea377a 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -18,7 +18,7 @@ use mem::transmute; use option::{None, Option, Some}; use iter::range_step; -use slice::ImmutableSlice; +use slice::SlicePrelude; // UTF-8 ranges and tags for encoding characters static TAG_CONT: u8 = 0b1000_0000u8; diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index f51d3948757..a6e5b0cff55 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -16,8 +16,8 @@ use iter::{range, DoubleEndedIterator}; use num::{Float, FPNaN, FPInfinite, ToPrimitive, Primitive}; use num::{Zero, One, cast}; use result::Ok; -use slice::{mod, ImmutableSlice, MutableSlice}; -use str::StrSlice; +use slice::{mod, SlicePrelude}; +use str::StrPrelude; /// A flag that specifies whether to use exponential (scientific) notation. pub enum ExponentFormat { diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 013ed999b03..081f373b831 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -21,9 +21,9 @@ use option::{Option, Some, None}; use ops::Deref; use result::{Ok, Err}; use result; -use slice::{AsSlice, ImmutableSlice}; +use slice::{AsSlice, SlicePrelude}; use slice; -use str::StrSlice; +use str::StrPrelude; use str; pub use self::num::radix; diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index 190e1ecea59..e4a6c1a9758 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -17,7 +17,7 @@ use fmt; use iter::DoubleEndedIterator; use num::{Int, cast, zero}; -use slice::{ImmutableSlice, MutableSlice}; +use slice::SlicePrelude; /// A type that represents a specific radix #[doc(hidden)] diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index 4b08d4f3391..996f2e56ad3 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -57,9 +57,9 @@ pub use num::{Primitive, Int, ToPrimitive, FromPrimitive}; pub use option::{Option, Some, None}; pub use ptr::RawPtr; pub use result::{Result, Ok, Err}; -pub use str::{Str, StrSlice}; +pub use str::{Str, StrPrelude}; pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4}; pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8}; pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12}; -pub use slice::{ImmutablePartialEqSlice, ImmutableOrdSlice}; -pub use slice::{AsSlice, ImmutableSlice, MutableSlice}; +pub use slice::{PartialEqSlicePrelude, OrdSlicePrelude}; +pub use slice::{AsSlice, SlicePrelude}; diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 3cc904162a1..eaa52c99c4a 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -57,9 +57,9 @@ use raw::Slice as RawSlice; // Extension traits // -/// Extension methods for immutable slices. -#[unstable = "may merge with other traits; region parameter may disappear"] -pub trait ImmutableSlice for Sized? { +/// Extension methods for slices. +#[unstable = "may merge with other traits"] +pub trait SlicePrelude for Sized? { /// Returns a subslice spanning the interval [`start`, `end`). /// /// Fails when the end of the new slice lies beyond the end of the @@ -256,216 +256,12 @@ pub trait ImmutableSlice for Sized? { #[inline] #[experimental = "not triaged yet"] fn is_empty(&self) -> bool { self.len() == 0 } -} -#[unstable] -impl ImmutableSlice for [T] { - #[inline] - fn slice(&self, start: uint, end: uint) -> &[T] { - assert!(start <= end); - assert!(end <= self.len()); - unsafe { - transmute(RawSlice { - data: self.as_ptr().offset(start as int), - len: (end - start) - }) - } - } - - #[inline] - fn slice_from(&self, start: uint) -> &[T] { - self.slice(start, self.len()) - } - - #[inline] - fn slice_to(&self, end: uint) -> &[T] { - self.slice(0, end) - } - - #[inline] - fn split_at(&self, mid: uint) -> (&[T], &[T]) { - (self[..mid], self[mid..]) - } - - #[inline] - fn iter<'a>(&'a self) -> Items<'a, T> { - unsafe { - let p = self.as_ptr(); - if mem::size_of::() == 0 { - Items{ptr: p, - end: (p as uint + self.len()) as *const T, - marker: marker::ContravariantLifetime::<'a>} - } else { - Items{ptr: p, - end: p.offset(self.len() as int), - marker: marker::ContravariantLifetime::<'a>} - } - } - } - - #[inline] - fn split<'a>(&'a self, pred: |&T|: 'a -> bool) -> Splits<'a, T> { - Splits { - v: self, - pred: pred, - finished: false - } - } - - #[inline] - fn splitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN> { - SplitsN { - iter: self.split(pred), - count: n, - invert: false - } - } - - #[inline] - fn rsplitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN> { - SplitsN { - iter: self.split(pred), - count: n, - invert: true - } - } - - #[inline] - fn windows(&self, size: uint) -> Windows { - assert!(size != 0); - Windows { v: self, size: size } - } - - #[inline] - fn chunks(&self, size: uint) -> Chunks { - assert!(size != 0); - Chunks { v: self, size: size } - } - - #[inline] - fn get(&self, index: uint) -> Option<&T> { - if index < self.len() { Some(&self[index]) } else { None } - } - - #[inline] - fn head(&self) -> Option<&T> { - if self.len() == 0 { None } else { Some(&self[0]) } - } - - #[inline] - fn tail(&self) -> &[T] { self[1..] } - - #[inline] - fn init(&self) -> &[T] { - self[..self.len() - 1] - } - - #[inline] - fn last(&self) -> Option<&T> { - if self.len() == 0 { None } else { Some(&self[self.len() - 1]) } - } - - #[inline] - unsafe fn unsafe_get(&self, index: uint) -> &T { - transmute(self.repr().data.offset(index as int)) - } - - #[inline] - fn as_ptr(&self) -> *const T { - self.repr().data - } - - #[unstable] - fn binary_search(&self, f: |&T| -> Ordering) -> BinarySearchResult { - let mut base : uint = 0; - let mut lim : uint = self.len(); - - while lim != 0 { - let ix = base + (lim >> 1); - match f(&self[ix]) { - Equal => return Found(ix), - Less => { - base = ix + 1; - lim -= 1; - } - Greater => () - } - lim >>= 1; - } - return NotFound(base); - } - - #[inline] - fn len(&self) -> uint { self.repr().len } -} - - - -impl ops::Slice for [T] { - #[inline] - fn as_slice_<'a>(&'a self) -> &'a [T] { - self - } - - #[inline] - fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] { - self.slice_or_fail(start, &self.len()) - } - - #[inline] - fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] { - self.slice_or_fail(&0, end) - } - #[inline] - fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] { - assert!(*start <= *end); - assert!(*end <= self.len()); - unsafe { - transmute(RawSlice { - data: self.as_ptr().offset(*start as int), - len: (*end - *start) - }) - } - } -} - -impl ops::SliceMut for [T] { - #[inline] - fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] { - self - } - - #[inline] - fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] { - let len = &self.len(); - self.slice_or_fail_mut(start, len) - } - - #[inline] - fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] { - self.slice_or_fail_mut(&0, end) - } - #[inline] - fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] { - assert!(*start <= *end); - assert!(*end <= self.len()); - unsafe { - transmute(RawSlice { - data: self.as_ptr().offset(*start as int), - len: (*end - *start) - }) - } - } -} - -/// Extension methods for slices such that their elements are -/// mutable. -#[experimental = "may merge with other traits; may lose region param; needs review"] -pub trait MutableSlice for Sized? { /// Returns a mutable reference to the element at the given index, /// or `None` if the index is out of bounds #[unstable = "waiting on final error conventions"] fn get_mut<'a>(&'a mut self, index: uint) -> Option<&'a mut T>; + /// Work with `self` as a mut slice. /// Primarily intended for getting a &mut [T] from a [T, ..N]. fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T]; @@ -626,8 +422,146 @@ pub trait MutableSlice for Sized? { fn as_mut_ptr(&mut self) -> *mut T; } -#[experimental = "trait is experimental"] -impl MutableSlice for [T] { +#[unstable] +impl SlicePrelude for [T] { + #[inline] + fn slice(&self, start: uint, end: uint) -> &[T] { + assert!(start <= end); + assert!(end <= self.len()); + unsafe { + transmute(RawSlice { + data: self.as_ptr().offset(start as int), + len: (end - start) + }) + } + } + + #[inline] + fn slice_from(&self, start: uint) -> &[T] { + self.slice(start, self.len()) + } + + #[inline] + fn slice_to(&self, end: uint) -> &[T] { + self.slice(0, end) + } + + #[inline] + fn split_at(&self, mid: uint) -> (&[T], &[T]) { + (self[..mid], self[mid..]) + } + + #[inline] + fn iter<'a>(&'a self) -> Items<'a, T> { + unsafe { + let p = self.as_ptr(); + if mem::size_of::() == 0 { + Items{ptr: p, + end: (p as uint + self.len()) as *const T, + marker: marker::ContravariantLifetime::<'a>} + } else { + Items{ptr: p, + end: p.offset(self.len() as int), + marker: marker::ContravariantLifetime::<'a>} + } + } + } + + #[inline] + fn split<'a>(&'a self, pred: |&T|: 'a -> bool) -> Splits<'a, T> { + Splits { + v: self, + pred: pred, + finished: false + } + } + + #[inline] + fn splitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN> { + SplitsN { + iter: self.split(pred), + count: n, + invert: false + } + } + + #[inline] + fn rsplitn<'a>(&'a self, n: uint, pred: |&T|: 'a -> bool) -> SplitsN> { + SplitsN { + iter: self.split(pred), + count: n, + invert: true + } + } + + #[inline] + fn windows(&self, size: uint) -> Windows { + assert!(size != 0); + Windows { v: self, size: size } + } + + #[inline] + fn chunks(&self, size: uint) -> Chunks { + assert!(size != 0); + Chunks { v: self, size: size } + } + + #[inline] + fn get(&self, index: uint) -> Option<&T> { + if index < self.len() { Some(&self[index]) } else { None } + } + + #[inline] + fn head(&self) -> Option<&T> { + if self.len() == 0 { None } else { Some(&self[0]) } + } + + #[inline] + fn tail(&self) -> &[T] { self[1..] } + + #[inline] + fn init(&self) -> &[T] { + self[..self.len() - 1] + } + + #[inline] + fn last(&self) -> Option<&T> { + if self.len() == 0 { None } else { Some(&self[self.len() - 1]) } + } + + #[inline] + unsafe fn unsafe_get(&self, index: uint) -> &T { + transmute(self.repr().data.offset(index as int)) + } + + #[inline] + fn as_ptr(&self) -> *const T { + self.repr().data + } + + #[unstable] + fn binary_search(&self, f: |&T| -> Ordering) -> BinarySearchResult { + let mut base : uint = 0; + let mut lim : uint = self.len(); + + while lim != 0 { + let ix = base + (lim >> 1); + match f(&self[ix]) { + Equal => return Found(ix), + Less => { + base = ix + 1; + lim -= 1; + } + Greater => () + } + lim >>= 1; + } + return NotFound(base); + } + + #[inline] + fn len(&self) -> uint { self.repr().len } + #[inline] fn get_mut(&mut self, index: uint) -> Option<&mut T> { if index < self.len() { Some(&mut self[index]) } else { None } @@ -764,9 +698,66 @@ impl MutableSlice for [T] { } } +impl ops::Slice for [T] { + #[inline] + fn as_slice_<'a>(&'a self) -> &'a [T] { + self + } + + #[inline] + fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] { + self.slice_or_fail(start, &self.len()) + } + + #[inline] + fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] { + self.slice_or_fail(&0, end) + } + #[inline] + fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] { + assert!(*start <= *end); + assert!(*end <= self.len()); + unsafe { + transmute(RawSlice { + data: self.as_ptr().offset(*start as int), + len: (*end - *start) + }) + } + } +} + +impl ops::SliceMut for [T] { + #[inline] + fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] { + self + } + + #[inline] + fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] { + let len = &self.len(); + self.slice_or_fail_mut(start, len) + } + + #[inline] + fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] { + self.slice_or_fail_mut(&0, end) + } + #[inline] + fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] { + assert!(*start <= *end); + assert!(*end <= self.len()); + unsafe { + transmute(RawSlice { + data: self.as_ptr().offset(*start as int), + len: (*end - *start) + }) + } + } +} + /// Extension methods for slices containing `PartialEq` elements. #[unstable = "may merge with other traits"] -pub trait ImmutablePartialEqSlice for Sized? { +pub trait PartialEqSlicePrelude for Sized? { /// Find the first index containing a matching value. fn position_elem(&self, t: &T) -> Option; @@ -784,7 +775,7 @@ pub trait ImmutablePartialEqSlice for Sized? { } #[unstable = "trait is unstable"] -impl ImmutablePartialEqSlice for [T] { +impl PartialEqSlicePrelude for [T] { #[inline] fn position_elem(&self, x: &T) -> Option { self.iter().position(|y| *x == *y) @@ -815,7 +806,7 @@ impl ImmutablePartialEqSlice for [T] { /// Extension methods for slices containing `Ord` elements. #[unstable = "may merge with other traits"] -pub trait ImmutableOrdSlice for Sized? { +pub trait OrdSlicePrelude for Sized? { /// Binary search a sorted slice for a given element. /// /// If the value is found then `Found` is returned, containing the @@ -842,19 +833,119 @@ pub trait ImmutableOrdSlice for Sized? { /// ``` #[unstable = "name likely to change"] fn binary_search_elem(&self, x: &T) -> BinarySearchResult; + + /// Mutates the slice to the next lexicographic permutation. + /// + /// Returns `true` if successful and `false` if the slice is at the + /// last-ordered permutation. + /// + /// # Example + /// + /// ```rust + /// let v: &mut [_] = &mut [0i, 1, 2]; + /// v.next_permutation(); + /// let b: &mut [_] = &mut [0i, 2, 1]; + /// assert!(v == b); + /// v.next_permutation(); + /// let b: &mut [_] = &mut [1i, 0, 2]; + /// assert!(v == b); + /// ``` + #[experimental] + fn next_permutation(&mut self) -> bool; + + /// Mutates the slice to the previous lexicographic permutation. + /// + /// Returns `true` if successful and `false` if the slice is at the + /// first-ordered permutation. + /// + /// # Example + /// + /// ```rust + /// let v: &mut [_] = &mut [1i, 0, 2]; + /// v.prev_permutation(); + /// let b: &mut [_] = &mut [0i, 2, 1]; + /// assert!(v == b); + /// v.prev_permutation(); + /// let b: &mut [_] = &mut [0i, 1, 2]; + /// assert!(v == b); + /// ``` + #[experimental] + fn prev_permutation(&mut self) -> bool; } #[unstable = "trait is unstable"] -impl ImmutableOrdSlice for [T] { +impl OrdSlicePrelude for [T] { #[unstable] fn binary_search_elem(&self, x: &T) -> BinarySearchResult { self.binary_search(|p| p.cmp(x)) } + + #[experimental] + fn next_permutation(&mut self) -> bool { + // These cases only have 1 permutation each, so we can't do anything. + if self.len() < 2 { return false; } + + // Step 1: Identify the longest, rightmost weakly decreasing part of the vector + let mut i = self.len() - 1; + while i > 0 && self[i-1] >= self[i] { + i -= 1; + } + + // If that is the entire vector, this is the last-ordered permutation. + if i == 0 { + return false; + } + + // Step 2: Find the rightmost element larger than the pivot (i-1) + let mut j = self.len() - 1; + while j >= i && self[j] <= self[i-1] { + j -= 1; + } + + // Step 3: Swap that element with the pivot + self.swap(j, i-1); + + // Step 4: Reverse the (previously) weakly decreasing part + self[mut i..].reverse(); + + true + } + + #[experimental] + fn prev_permutation(&mut self) -> bool { + // These cases only have 1 permutation each, so we can't do anything. + if self.len() < 2 { return false; } + + // Step 1: Identify the longest, rightmost weakly increasing part of the vector + let mut i = self.len() - 1; + while i > 0 && self[i-1] <= self[i] { + i -= 1; + } + + // If that is the entire vector, this is the first-ordered permutation. + if i == 0 { + return false; + } + + // Step 2: Reverse the weakly increasing part + self[mut i..].reverse(); + + // Step 3: Find the rightmost element equal to or bigger than the pivot (i-1) + let mut j = self.len() - 1; + while j >= i && self[j-1] < self[i-1] { + j -= 1; + } + + // Step 4: Swap that element with the pivot + self.swap(i-1, j); + + true + } } -/// Trait for &[T] where T is Cloneable +/// Extension methods for slices on Clone elements #[unstable = "may merge with other traits"] -pub trait MutableCloneableSlice for Sized? { +pub trait CloneSlicePrelude for Sized? { /// Copies as many elements from `src` as it can into `self` (the /// shorter of `self.len()` and `src.len()`). Returns the number /// of elements copied. @@ -862,7 +953,7 @@ pub trait MutableCloneableSlice for Sized? { /// # Example /// /// ```rust - /// use std::slice::MutableCloneableSlice; + /// use std::slice::CloneSlicePrelude; /// /// let mut dst = [0i, 0, 0]; /// let src = [1i, 2]; @@ -878,7 +969,7 @@ pub trait MutableCloneableSlice for Sized? { } #[unstable = "trait is unstable"] -impl MutableCloneableSlice for [T] { +impl CloneSlicePrelude for [T] { #[inline] fn clone_from_slice(&mut self, src: &[T]) -> uint { let min = cmp::min(self.len(), src.len()); @@ -1517,7 +1608,7 @@ pub mod raw { pub mod bytes { use kinds::Sized; use ptr; - use slice::{ImmutableSlice, MutableSlice}; + use slice::SlicePrelude; /// A trait for operations on mutable `[u8]`s. pub trait MutableByteVector for Sized? { diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 175f9f3f577..4c1bfb61709 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -28,8 +28,7 @@ use kinds::Sized; use num::{CheckedMul, Saturating}; use option::{Option, None, Some}; use raw::Repr; -use slice::ImmutableSlice; -use slice; +use slice::{mod, SlicePrelude}; use uint; /* @@ -1056,8 +1055,8 @@ pub mod raw { use mem; use ptr::RawPtr; use raw::Slice; - use slice::{ImmutableSlice}; - use str::{is_utf8, StrSlice}; + use slice::SlicePrelude; + use str::{is_utf8, StrPrelude}; /// Converts a slice of bytes to a string slice without checking /// that the string contains valid UTF-8. @@ -1120,7 +1119,7 @@ pub mod traits { use iter::Iterator; use option::{Option, Some}; use ops; - use str::{Str, StrSlice, eq_slice}; + use str::{Str, StrPrelude, eq_slice}; // NOTE(stage0): remove impl after a snapshot #[cfg(stage0)] @@ -1240,7 +1239,7 @@ impl<'a> Str for &'a str { } /// Methods for string slices -pub trait StrSlice for Sized? { +pub trait StrPrelude for Sized? { /// Returns true if one string contains another /// /// # Arguments @@ -1891,7 +1890,7 @@ fn slice_error_fail(s: &str, begin: uint, end: uint) -> ! { begin, end, s); } -impl StrSlice for str { +impl StrPrelude for str { #[inline] fn contains(&self, needle: &str) -> bool { self.find_str(needle).is_some() diff --git a/src/libgraphviz/maybe_owned_vec.rs b/src/libgraphviz/maybe_owned_vec.rs index 3a89d8b3f81..2c516affeb2 100644 --- a/src/libgraphviz/maybe_owned_vec.rs +++ b/src/libgraphviz/maybe_owned_vec.rs @@ -12,7 +12,7 @@ use std::default::Default; use std::fmt; use std::iter::FromIterator; use std::path::BytesContainer; -use std::slice; +use std::slice::{mod, Permutations}; // Note 1: It is not clear whether the flexibility of providing both // the `Growable` and `FixedLen` variants is sufficiently useful. @@ -137,11 +137,19 @@ impl<'a,T:fmt::Show> fmt::Show for MaybeOwnedVector<'a,T> { } } -impl<'a,T:Clone> CloneableVector for MaybeOwnedVector<'a,T> { +impl<'a,T:Clone> CloneSliceAllocPrelude for MaybeOwnedVector<'a,T> { /// Returns a copy of `self`. fn to_vec(&self) -> Vec { self.as_slice().to_vec() } + + fn partitioned(&self, f: |&T| -> bool) -> (Vec, Vec) { + self.as_slice().partitioned(f) + } + + fn permutations(&self) -> Permutations { + self.as_slice().permutations() + } } impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> { @@ -153,7 +161,6 @@ impl<'a, T: Clone> Clone for MaybeOwnedVector<'a, T> { } } - impl<'a, T> Default for MaybeOwnedVector<'a, T> { fn default() -> MaybeOwnedVector<'a, T> { Growable(Vec::new()) diff --git a/src/libnative/io/process.rs b/src/libnative/io/process.rs index fed4a46b9df..30c916f3303 100644 --- a/src/libnative/io/process.rs +++ b/src/libnative/io/process.rs @@ -296,7 +296,7 @@ fn spawn_process_os(cfg: ProcessConfig, use std::mem; use std::iter::Iterator; - use std::str::StrSlice; + use std::str::StrPrelude; if cfg.gid.is_some() || cfg.uid.is_some() { return Err(IoError { diff --git a/src/libregex/vm.rs b/src/libregex/vm.rs index ce06828e764..aa18a65dc58 100644 --- a/src/libregex/vm.rs +++ b/src/libregex/vm.rs @@ -35,7 +35,7 @@ use std::cmp; use std::mem; -use std::slice::MutableSlice; +use std::slice::SlicePrelude; use compile::{ Program, Match, OneChar, CharClass, Any, EmptyBegin, EmptyEnd, EmptyWordBoundary, diff --git a/src/librlibc/lib.rs b/src/librlibc/lib.rs index dd88eb3251b..12824318775 100644 --- a/src/librlibc/lib.rs +++ b/src/librlibc/lib.rs @@ -108,8 +108,8 @@ pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: uint) -> i32 { #[cfg(test)] mod test { - use core::str::StrSlice; - use core::slice::{MutableSlice, ImmutableSlice}; + use core::str::StrPrelude; + use core::slice::{SlicePrelude}; use super::{memcmp, memset, memcpy, memmove}; diff --git a/src/librustrt/args.rs b/src/librustrt/args.rs index 20a63f655b8..5eecc0a53e0 100644 --- a/src/librustrt/args.rs +++ b/src/librustrt/args.rs @@ -47,7 +47,7 @@ mod imp { use core::prelude::*; use alloc::boxed::Box; - use collections::slice::CloneableVector; + use collections::slice::CloneSliceAllocPrelude; use collections::vec::Vec; use core::mem; use core::slice; diff --git a/src/librustrt/c_str.rs b/src/librustrt/c_str.rs index b7a2c8f9473..68c2d2031c4 100644 --- a/src/librustrt/c_str.rs +++ b/src/librustrt/c_str.rs @@ -76,9 +76,9 @@ use collections::hash; use core::fmt; use core::kinds::{Sized, marker}; use core::mem; -use core::prelude::{Clone, Drop, Eq, ImmutableSlice, Iterator}; -use core::prelude::{MutableSlice, None, Option, Ordering, PartialEq}; -use core::prelude::{PartialOrd, RawPtr, Some, StrSlice, range}; +use core::prelude::{Clone, Drop, Eq, Iterator}; +use core::prelude::{SlicePrelude, None, Option, Ordering, PartialEq}; +use core::prelude::{PartialOrd, RawPtr, Some, StrPrelude, range}; use core::ptr; use core::raw::Slice; use core::slice; diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 2953b60e674..ff83027d280 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -19,8 +19,8 @@ use fmt; use iter::Iterator; use mem; use option::{Option, Some, None}; -use slice::{ImmutableSlice, MutableSlice, AsSlice}; -use str::{Str, StrSlice}; +use slice::{SlicePrelude, AsSlice}; +use str::{Str, StrPrelude}; use string::{mod, String}; use to_string::IntoStr; use vec::Vec; @@ -578,7 +578,7 @@ mod tests { use prelude::*; use super::*; use char::from_u32; - use str::StrSlice; + use str::StrPrelude; macro_rules! v2ascii ( ( [$($e:expr),*]) => (&[$(Ascii{chr:$e}),*]); diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 688036d22dd..cde862a1d17 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -594,7 +594,7 @@ mod test_set { use prelude::*; use super::HashSet; - use slice::ImmutablePartialEqSlice; + use slice::PartialEqSlicePrelude; #[test] fn test_disjoint() { diff --git a/src/libstd/dynamic_lib.rs b/src/libstd/dynamic_lib.rs index 4c133fc7397..e3dfa8cabee 100644 --- a/src/libstd/dynamic_lib.rs +++ b/src/libstd/dynamic_lib.rs @@ -28,7 +28,7 @@ use option::*; use os; use path::{Path,GenericPath}; use result::*; -use slice::{AsSlice,ImmutableSlice}; +use slice::{AsSlice,SlicePrelude}; use str; use string::String; use vec::Vec; @@ -284,8 +284,8 @@ pub mod dl { use os; use ptr; use result::{Ok, Err, Result}; - use slice::ImmutableSlice; - use str::StrSlice; + use slice::SlicePrelude; + use str::StrPrelude; use str; use string::String; use vec::Vec; diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 49c688da31c..f5c6c15857a 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -18,7 +18,7 @@ use iter::ExactSize; use ops::Drop; use option::{Some, None, Option}; use result::{Ok, Err}; -use slice::{ImmutableSlice, MutableSlice}; +use slice::{SlicePrelude}; use slice; use vec::Vec; @@ -376,7 +376,7 @@ mod test { use super::super::{IoResult, EndOfFile}; use super::super::mem::{MemReader, MemWriter, BufReader}; use self::test::Bencher; - use str::StrSlice; + use str::StrPrelude; /// A type, free to create, primarily intended for benchmarking creation of /// wrappers that, just for construction, don't need a Reader/Writer that diff --git a/src/libstd/io/comm_adapters.rs b/src/libstd/io/comm_adapters.rs index c925208c3ee..6b4a952f909 100644 --- a/src/libstd/io/comm_adapters.rs +++ b/src/libstd/io/comm_adapters.rs @@ -14,7 +14,7 @@ use comm::{Sender, Receiver}; use io; use option::{None, Some}; use result::{Ok, Err}; -use slice::{bytes, CloneableVector, ImmutableSlice}; +use slice::{bytes, CloneSliceAllocPrelude, SlicePrelude}; use super::{Buffer, Reader, Writer, IoResult}; use vec::Vec; diff --git a/src/libstd/io/extensions.rs b/src/libstd/io/extensions.rs index 6d0b8ebc3d9..07aa25bc044 100644 --- a/src/libstd/io/extensions.rs +++ b/src/libstd/io/extensions.rs @@ -22,7 +22,7 @@ use num::Int; use option::{Option, Some, None}; use ptr::RawPtr; use result::{Ok, Err}; -use slice::{ImmutableSlice, AsSlice}; +use slice::{SlicePrelude, AsSlice}; /// An iterator that reads a single byte on each iteration, /// until `.read_byte()` returns `EndOfFile`. @@ -150,7 +150,7 @@ pub fn u64_to_be_bytes(n: u64, size: uint, f: |v: &[u8]| -> T) -> T { /// 32-bit value is parsed. pub fn u64_from_be_bytes(data: &[u8], start: uint, size: uint) -> u64 { use ptr::{copy_nonoverlapping_memory}; - use slice::MutableSlice; + use slice::SlicePrelude; assert!(size <= 8u); diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index c8524676a6d..c95d2c3f328 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -70,7 +70,7 @@ use path; use result::{Err, Ok}; use rt::rtio::LocalIo; use rt::rtio; -use slice::ImmutableSlice; +use slice::SlicePrelude; use string::String; use vec::Vec; @@ -951,7 +951,7 @@ mod test { use path::Path; use io; use ops::Drop; - use str::StrSlice; + use str::StrPrelude; macro_rules! check( ($e:expr) => ( match $e { diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs index 60104365817..51935862600 100644 --- a/src/libstd/io/mem.rs +++ b/src/libstd/io/mem.rs @@ -17,7 +17,7 @@ use option::None; use result::{Err, Ok}; use io; use io::{Reader, Writer, Seek, Buffer, IoError, SeekStyle, IoResult}; -use slice::{mod, AsSlice, ImmutableSlice}; +use slice::{mod, AsSlice, SlicePrelude}; use vec::Vec; const BUF_CAPACITY: uint = 128; @@ -341,7 +341,7 @@ mod test { use io::*; use io; use self::test::Bencher; - use str::StrSlice; + use str::StrPrelude; #[test] fn test_mem_writer() { diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index ebf541a63da..da5286b16ae 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -236,8 +236,8 @@ use os; use boxed::Box; use result::{Ok, Err, Result}; use rt::rtio; -use slice::{AsSlice, ImmutableSlice}; -use str::{Str, StrSlice}; +use slice::{AsSlice, SlicePrelude}; +use str::{Str, StrPrelude}; use str; use string::String; use uint; diff --git a/src/libstd/io/net/ip.rs b/src/libstd/io/net/ip.rs index b4e9f7502e4..2a2d978ef49 100644 --- a/src/libstd/io/net/ip.rs +++ b/src/libstd/io/net/ip.rs @@ -22,8 +22,8 @@ use io::net; use iter::Iterator; use option::{Option, None, Some}; use result::{Ok, Err}; -use str::StrSlice; -use slice::{MutableCloneableSlice, MutableSlice, ImmutableSlice}; +use str::StrPrelude; +use slice::{CloneSlicePrelude, SlicePrelude}; use vec::Vec; pub type Port = u16; diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 5fd4faff6d2..7bae67c0aa6 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -41,8 +41,8 @@ use rt; use rt::local::Local; use rt::task::Task; use rt::rtio::{DontClose, IoFactory, LocalIo, RtioFileStream, RtioTTY}; -use slice::ImmutableSlice; -use str::StrSlice; +use slice::SlicePrelude; +use str::StrPrelude; use uint; // And so begins the tale of acquiring a uv handle to a stdio stream on all diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 55e364b1961..d82147947de 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -330,7 +330,7 @@ macro_rules! try ( #[macro_export] macro_rules! vec[ ($($x:expr),*) => ({ - use std::slice::BoxedSlice; + use std::slice::BoxedSlicePrelude; let xs: ::std::boxed::Box<[_]> = box [$($x),*]; xs.into_vec() }); diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 7a02d8d77b0..612090a3a51 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -20,8 +20,8 @@ use num; use num::{Int, Bounded}; use num::{Float, FPNaN, FPInfinite, ToPrimitive}; use option::{None, Option, Some}; -use slice::{ImmutableSlice, MutableSlice, CloneableVector}; -use str::StrSlice; +use slice::{SlicePrelude, CloneSliceAllocPrelude}; +use str::StrPrelude; use string::String; use vec::Vec; diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 9846f7b653e..0042a3ae205 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -46,9 +46,9 @@ use path::{Path, GenericPath, BytesContainer}; use ptr::RawPtr; use ptr; use result::{Err, Ok, Result}; -use slice::{AsSlice, ImmutableSlice, MutableSlice, ImmutablePartialEqSlice}; -use slice::CloneableVector; -use str::{Str, StrSlice, StrAllocating}; +use slice::{AsSlice, SlicePrelude, PartialEqSlicePrelude}; +use slice::CloneSliceAllocPrelude; +use str::{Str, StrPrelude, StrAllocating}; use string::String; use to_string::ToString; use sync::atomic::{AtomicInt, INIT_ATOMIC_INT, SeqCst}; @@ -146,9 +146,9 @@ pub mod windows { use option::{None, Option}; use option; use os::TMPBUF_SZ; - use slice::{MutableSlice, ImmutableSlice}; + use slice::{SlicePrelude}; use string::String; - use str::StrSlice; + use str::StrPrelude; use vec::Vec; pub fn fill_utf16_buf_and_decode(f: |*mut u16, DWORD| -> DWORD) diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 8949a881c9d..e55933c4262 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -74,10 +74,10 @@ use fmt; use iter::Iterator; use option::{Option, None, Some}; use str; -use str::{MaybeOwned, Str, StrSlice}; +use str::{MaybeOwned, Str, StrPrelude}; use string::String; -use slice::{AsSlice, CloneableVector}; -use slice::{ImmutablePartialEqSlice, ImmutableSlice}; +use slice::{AsSlice, CloneSliceAllocPrelude}; +use slice::{PartialEqSlicePrelude, SlicePrelude}; use vec::Vec; /// Typedef for POSIX file paths. diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index 60cfa7a13de..a927e091840 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -20,8 +20,8 @@ use iter::{DoubleEndedIterator, AdditiveIterator, Extendable, Iterator, Map}; use option::{Option, None, Some}; use str::Str; use str; -use slice::{CloneableVector, Splits, AsSlice, VectorVector, - ImmutablePartialEqSlice, ImmutableSlice}; +use slice::{CloneSliceAllocPrelude, Splits, AsSlice, VectorVector, + PartialEqSlicePrelude, SlicePrelude}; use vec::Vec; use super::{BytesContainer, GenericPath, GenericPathUnsafe}; @@ -444,7 +444,7 @@ mod tests { use super::*; use mem; use str; - use str::StrSlice; + use str::StrPrelude; macro_rules! t( (s: $path:expr, $exp:expr) => ( diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 3ef142a2e82..c5f84244928 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -22,8 +22,8 @@ use io::Writer; use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Iterator, Map}; use mem; use option::{Option, Some, None}; -use slice::{AsSlice, ImmutableSlice}; -use str::{CharSplits, Str, StrAllocating, StrVector, StrSlice}; +use slice::{AsSlice, SlicePrelude}; +use str::{CharSplits, Str, StrAllocating, StrVector, StrPrelude}; use string::String; use unicode::char::UnicodeChar; use vec::Vec; diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index 4dcdde6726a..449d3a14bc9 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -76,18 +76,15 @@ #[doc(no_inline)] pub use ptr::{RawPtr, RawMutPtr}; #[doc(no_inline)] pub use result::{Result, Ok, Err}; #[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek}; -#[doc(no_inline)] pub use str::{Str, StrVector, StrSlice}; -#[doc(no_inline)] pub use str::{IntoMaybeOwned, StrAllocating, UnicodeStrSlice}; +#[doc(no_inline)] pub use str::{Str, StrVector, StrPrelude}; +#[doc(no_inline)] pub use str::{IntoMaybeOwned, StrAllocating, UnicodeStrPrelude}; #[doc(no_inline)] pub use to_string::{ToString, IntoStr}; #[doc(no_inline)] pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4}; #[doc(no_inline)] pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8}; #[doc(no_inline)] pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12}; -#[doc(no_inline)] pub use slice::{CloneableVector, ImmutableCloneableVector}; -#[doc(no_inline)] pub use slice::{MutableCloneableSlice, MutableOrdSlice}; -#[doc(no_inline)] pub use slice::{ImmutableSlice, MutableSlice}; -#[doc(no_inline)] pub use slice::{ImmutablePartialEqSlice, ImmutableOrdSlice}; -#[doc(no_inline)] pub use slice::{AsSlice, VectorVector, BoxedSlice}; -#[doc(no_inline)] pub use slice::MutableSliceAllocating; +#[doc(no_inline)] pub use slice::{SlicePrelude, AsSlice, CloneSlicePrelude}; +#[doc(no_inline)] pub use slice::{VectorVector, PartialEqSlicePrelude, OrdSlicePrelude}; +#[doc(no_inline)] pub use slice::{CloneSliceAllocPrelude, OrdSliceAllocPrelude, SliceAllocPrelude}; #[doc(no_inline)] pub use string::String; #[doc(no_inline)] pub use vec::Vec; diff --git a/src/libstd/rand/os.rs b/src/libstd/rand/os.rs index b7b08581230..b6b66e593a2 100644 --- a/src/libstd/rand/os.rs +++ b/src/libstd/rand/os.rs @@ -69,7 +69,7 @@ mod imp { use rand::Rng; use result::{Ok}; use self::libc::{c_int, size_t}; - use slice::{ImmutableSlice, MutableSlice}; + use slice::{SlicePrelude}; /// A random number generator that retrieves randomness straight from /// the operating system. Platform sources: @@ -137,7 +137,7 @@ mod imp { use result::{Ok, Err}; use self::libc::{DWORD, BYTE, LPCSTR, BOOL}; use self::libc::types::os::arch::extra::{LONG_PTR}; - use slice::{ImmutableSlice, MutableSlice}; + use slice::{SlicePrelude}; type HCRYPTPROV = LONG_PTR; diff --git a/src/libstd/rand/reader.rs b/src/libstd/rand/reader.rs index c1bb6970f71..c8ed9805215 100644 --- a/src/libstd/rand/reader.rs +++ b/src/libstd/rand/reader.rs @@ -13,7 +13,7 @@ use io::Reader; use rand::Rng; use result::{Ok, Err}; -use slice::ImmutableSlice; +use slice::SlicePrelude; /// An RNG that reads random bytes straight from a `Reader`. This will /// work best with an infinite reader, but this is not required. diff --git a/src/libstd/rt/backtrace.rs b/src/libstd/rt/backtrace.rs index 5d7aa0509c5..b08b92b8587 100644 --- a/src/libstd/rt/backtrace.rs +++ b/src/libstd/rt/backtrace.rs @@ -18,7 +18,7 @@ use iter::Iterator; use option::{Some, None}; use os; use result::{Ok, Err}; -use str::StrSlice; +use str::StrPrelude; use sync::atomic; use unicode::char::UnicodeChar; @@ -255,7 +255,7 @@ mod imp { pub fn write(w: &mut Writer) -> IoResult<()> { use iter::{Iterator, range}; use result; - use slice::{MutableSlice}; + use slice::{SlicePrelude}; extern { fn backtrace(buf: *mut *mut libc::c_void, @@ -394,7 +394,7 @@ mod imp { use path::GenericPath; use ptr::RawPtr; use ptr; - use slice::{ImmutableSlice, MutableSlice}; + use slice::{SlicePrelude}; //////////////////////////////////////////////////////////////////////// // libbacktrace.h API @@ -666,8 +666,8 @@ mod imp { use path::Path; use result::{Ok, Err}; use rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT}; - use slice::ImmutableSlice; - use str::StrSlice; + use slice::SlicePrelude; + use str::StrPrelude; use dynamic_lib::DynamicLibrary; #[allow(non_snake_case)] diff --git a/src/libunicode/lib.rs b/src/libunicode/lib.rs index b84aec77a09..0db0ffd5cb4 100644 --- a/src/libunicode/lib.rs +++ b/src/libunicode/lib.rs @@ -73,7 +73,7 @@ pub mod char { } pub mod str { - pub use u_str::{UnicodeStrSlice, Words, Graphemes, GraphemeIndices}; + pub use u_str::{UnicodeStrPrelude, Words, Graphemes, GraphemeIndices}; } // this lets us use #[deriving(Clone)] diff --git a/src/libunicode/normalize.rs b/src/libunicode/normalize.rs index 76a9476d1fc..ad36215c11b 100644 --- a/src/libunicode/normalize.rs +++ b/src/libunicode/normalize.rs @@ -16,7 +16,7 @@ use core::cmp::{Equal, Less, Greater}; use core::option::{Option, Some, None}; use core::slice; -use core::slice::ImmutableSlice; +use core::slice::SlicePrelude; use tables::normalization::{canonical_table, compatibility_table, composition_table}; fn bsearch_table(c: char, r: &'static [(char, &'static [T])]) -> Option<&'static [T]> { diff --git a/src/libunicode/tables.rs b/src/libunicode/tables.rs index 87ee3220ee5..212502fd181 100644 --- a/src/libunicode/tables.rs +++ b/src/libunicode/tables.rs @@ -13,12 +13,12 @@ #![allow(missing_doc, non_uppercase_statics, non_snake_case)] /// The version of [Unicode](http://www.unicode.org/) -/// that the `UnicodeChar` and `UnicodeStrSlice` traits are based on. +/// that the `UnicodeChar` and `UnicodeStrPrelude` traits are based on. pub const UNICODE_VERSION: (uint, uint, uint) = (7, 0, 0); fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool { use core::cmp::{Equal, Less, Greater}; - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; r.binary_search(|&(lo,hi)| { if lo <= c && c <= hi { Equal } else if hi < c { Less } @@ -6242,7 +6242,7 @@ pub mod normalization { fn bsearch_range_value_table(c: char, r: &'static [(char, char, u8)]) -> u8 { use core::cmp::{Equal, Less, Greater}; - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; use core::slice; match r.binary_search(|&(lo, hi, _)| { if lo <= c && c <= hi { Equal } @@ -6367,7 +6367,7 @@ pub mod normalization { pub mod conversions { use core::cmp::{Equal, Less, Greater}; - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; use core::tuple::Tuple2; use core::option::{Option, Some, None}; use core::slice; @@ -6935,7 +6935,7 @@ pub mod conversions { pub mod charwidth { use core::option::{Option, Some, None}; - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; use core::slice; fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 { @@ -7134,7 +7134,7 @@ pub mod charwidth { } pub mod grapheme { - use core::slice::ImmutableSlice; + use core::slice::SlicePrelude; use core::slice; #[allow(non_camel_case_types)] diff --git a/src/libunicode/u_str.rs b/src/libunicode/u_str.rs index 9e3830c1f60..50f257c9c85 100644 --- a/src/libunicode/u_str.rs +++ b/src/libunicode/u_str.rs @@ -18,11 +18,11 @@ */ use core::cmp; -use core::slice::ImmutableSlice; +use core::slice::SlicePrelude; use core::iter::{Filter, AdditiveIterator, Iterator, DoubleEndedIterator}; use core::kinds::Sized; use core::option::{Option, None, Some}; -use core::str::{CharSplits, StrSlice}; +use core::str::{CharSplits, StrPrelude}; use u_char; use u_char::UnicodeChar; use tables::grapheme::GraphemeCat; @@ -32,7 +32,7 @@ pub type Words<'a> = Filter<'a, &'a str, CharSplits<'a, extern "Rust" fn(char) -> bool>>; /// Methods for Unicode string slices -pub trait UnicodeStrSlice for Sized? { +pub trait UnicodeStrPrelude for Sized? { /// Returns an iterator over the /// [grapheme clusters](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) /// of the string. @@ -129,7 +129,7 @@ pub trait UnicodeStrSlice for Sized? { fn trim_right<'a>(&'a self) -> &'a str; } -impl UnicodeStrSlice for str { +impl UnicodeStrPrelude for str { #[inline] fn graphemes(&self, is_extended: bool) -> Graphemes { Graphemes { string: self, extended: is_extended, cat: None, catb: None }