Skip to content

Commit cfafc1b

Browse files
committed
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](rust-lang/rfcs#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 rust-lang#17917
1 parent e84e7a0 commit cfafc1b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+483
-487
lines changed

src/etc/unicode.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def emit_bsearch_range_table(f):
293293
f.write("""
294294
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
295295
use core::cmp::{Equal, Less, Greater};
296-
use core::slice::ImmutableSlice;
296+
use core::slice::SlicePrelude;
297297
r.binary_search(|&(lo,hi)| {
298298
if lo <= c && c <= hi { Equal }
299299
else if hi < c { Less }
@@ -351,7 +351,7 @@ def emit_conversions_module(f, lowerupper, upperlower):
351351
f.write("pub mod conversions {")
352352
f.write("""
353353
use core::cmp::{Equal, Less, Greater};
354-
use core::slice::ImmutableSlice;
354+
use core::slice::SlicePrelude;
355355
use core::tuple::Tuple2;
356356
use core::option::{Option, Some, None};
357357
use core::slice;
@@ -390,7 +390,7 @@ def emit_conversions_module(f, lowerupper, upperlower):
390390

391391
def emit_grapheme_module(f, grapheme_table, grapheme_cats):
392392
f.write("""pub mod grapheme {
393-
use core::slice::ImmutableSlice;
393+
use core::slice::SlicePrelude;
394394
use core::slice;
395395
396396
#[allow(non_camel_case_types)]
@@ -430,7 +430,7 @@ def emit_grapheme_module(f, grapheme_table, grapheme_cats):
430430
def emit_charwidth_module(f, width_table):
431431
f.write("pub mod charwidth {\n")
432432
f.write(" use core::option::{Option, Some, None};\n")
433-
f.write(" use core::slice::ImmutableSlice;\n")
433+
f.write(" use core::slice::SlicePrelude;\n")
434434
f.write(" use core::slice;\n")
435435
f.write("""
436436
fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
@@ -530,7 +530,7 @@ def comp_pfun(char):
530530
f.write("""
531531
fn bsearch_range_value_table(c: char, r: &'static [(char, char, u8)]) -> u8 {
532532
use core::cmp::{Equal, Less, Greater};
533-
use core::slice::ImmutableSlice;
533+
use core::slice::SlicePrelude;
534534
use core::slice;
535535
match r.binary_search(|&(lo, hi, _)| {
536536
if lo <= c && c <= hi { Equal }

src/libcollections/hash/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ mod tests {
290290
use core::kinds::Sized;
291291
use std::mem;
292292

293-
use slice::ImmutableSlice;
293+
use slice::SlicePrelude;
294294
use super::{Hash, Hasher, Writer};
295295

296296
struct MyWriterHasher;

src/libcollections/hash/sip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ mod tests {
273273

274274
use str::Str;
275275
use string::String;
276-
use slice::{AsSlice, ImmutableSlice};
276+
use slice::{AsSlice, SlicePrelude};
277277
use vec::Vec;
278278

279279
use super::super::{Hash, Writer};

src/libcollections/slice.rs

+56-154
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
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

100100
use 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};
104104
pub use core::slice::{ImmutableIntSlice, MutableIntSlice};
105105
pub 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};
107107
pub 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
748650
pub mod raw {
749651
pub use core::slice::raw::{buf_as_slice, mut_buf_as_slice};

src/libcollections/str.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ use core::fmt;
5656
use core::cmp;
5757
use core::iter::AdditiveIterator;
5858
use core::kinds::Sized;
59-
use core::prelude::{Char, Clone, Eq, Equiv, ImmutableSlice};
60-
use core::prelude::{Iterator, MutableSlice, None, Option, Ord, Ordering};
59+
use core::prelude::{Char, Clone, Eq, Equiv};
60+
use core::prelude::{Iterator, SlicePrelude, None, Option, Ord, Ordering};
6161
use core::prelude::{PartialEq, PartialOrd, Result, AsSlice, Some, Tuple2};
6262
use core::prelude::{range};
6363

@@ -73,8 +73,8 @@ pub use core::str::{CharSplitsN, AnyLines, MatchIndices, StrSplits};
7373
pub use core::str::{Utf16CodeUnits, eq_slice, is_utf8, is_utf16, Utf16Items};
7474
pub use core::str::{Utf16Item, ScalarValue, LoneSurrogate, utf16_items};
7575
pub use core::str::{truncate_utf16_at_nul, utf8_char_width, CharRange};
76-
pub use core::str::{Str, StrSlice};
77-
pub use unicode::str::{UnicodeStrSlice, Words, Graphemes, GraphemeIndices};
76+
pub use core::str::{Str, StrPrelude};
77+
pub use unicode::str::{UnicodeStrPrelude, Words, Graphemes, GraphemeIndices};
7878

7979
/*
8080
Section: Creating a string
@@ -790,10 +790,10 @@ mod tests {
790790
use std::iter::{Iterator, DoubleEndedIterator};
791791

792792
use super::*;
793-
use std::slice::{AsSlice, ImmutableSlice};
793+
use std::slice::{AsSlice, SlicePrelude};
794794
use string::String;
795795
use vec::Vec;
796-
use slice::CloneableVector;
796+
use slice::CloneSliceAllocPrelude;
797797

798798
use unicode::char::UnicodeChar;
799799

@@ -2240,8 +2240,8 @@ mod bench {
22402240
use test::black_box;
22412241
use super::*;
22422242
use std::iter::{Iterator, DoubleEndedIterator};
2243-
use std::str::StrSlice;
2244-
use std::slice::ImmutableSlice;
2243+
use std::str::StrPrelude;
2244+
use std::slice::SlicePrelude;
22452245

22462246
#[bench]
22472247
fn char_iterator(b: &mut Bencher) {

0 commit comments

Comments
 (0)