Skip to content

Commit 385c39a

Browse files
committed
auto merge of #16332 : brson/rust/slicestab, r=aturon
This implements some of the recommendations from https://github.com/rust-lang/meeting-minutes/blob/master/Meeting-API-review-2014-08-06.md. Explanation in commits.
2 parents 9d45d63 + fce442e commit 385c39a

Some content is hidden

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

65 files changed

+450
-210
lines changed

src/compiletest/header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ fn parse_exec_env(line: &str) -> Option<(String, String)> {
233233
parse_name_value_directive(line, "exec-env").map(|nv| {
234234
// nv is either FOO or FOO=BAR
235235
let mut strs: Vec<String> = nv.as_slice()
236-
.splitn('=', 1)
236+
.splitn(1, '=')
237237
.map(|s| s.to_string())
238238
.collect();
239239

src/etc/unicode.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,12 @@ 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::ImmutableVector;
297-
use core::option::None;
298-
r.bsearch(|&(lo,hi)| {
296+
use core::slice::ImmutableSlice;
297+
r.binary_search(|&(lo,hi)| {
299298
if lo <= c && c <= hi { Equal }
300299
else if hi < c { Less }
301300
else { Greater }
302-
}) != None
301+
}).found().is_some()
303302
}\n
304303
""")
305304

@@ -352,9 +351,10 @@ def emit_conversions_module(f, lowerupper, upperlower):
352351
f.write("pub mod conversions {")
353352
f.write("""
354353
use core::cmp::{Equal, Less, Greater};
355-
use core::slice::ImmutableVector;
354+
use core::slice::ImmutableSlice;
356355
use core::tuple::Tuple2;
357356
use core::option::{Option, Some, None};
357+
use core::slice;
358358
359359
pub fn to_lower(c: char) -> char {
360360
match bsearch_case_table(c, LuLl_table) {
@@ -371,11 +371,14 @@ def emit_conversions_module(f, lowerupper, upperlower):
371371
}
372372
373373
fn bsearch_case_table(c: char, table: &'static [(char, char)]) -> Option<uint> {
374-
table.bsearch(|&(key, _)| {
374+
match table.binary_search(|&(key, _)| {
375375
if c == key { Equal }
376376
else if key < c { Less }
377377
else { Greater }
378-
})
378+
}) {
379+
slice::Found(i) => Some(i),
380+
slice::NotFound(_) => None,
381+
}
379382
}
380383
381384
""")
@@ -387,8 +390,8 @@ def emit_conversions_module(f, lowerupper, upperlower):
387390

388391
def emit_grapheme_module(f, grapheme_table, grapheme_cats):
389392
f.write("""pub mod grapheme {
390-
use core::option::{Some, None};
391-
use core::slice::ImmutableVector;
393+
use core::slice::ImmutableSlice;
394+
use core::slice;
392395
393396
#[allow(non_camel_case_types)]
394397
#[deriving(Clone)]
@@ -400,16 +403,16 @@ def emit_grapheme_module(f, grapheme_table, grapheme_cats):
400403
401404
fn bsearch_range_value_table(c: char, r: &'static [(char, char, GraphemeCat)]) -> GraphemeCat {
402405
use core::cmp::{Equal, Less, Greater};
403-
match r.bsearch(|&(lo, hi, _)| {
406+
match r.binary_search(|&(lo, hi, _)| {
404407
if lo <= c && c <= hi { Equal }
405408
else if hi < c { Less }
406409
else { Greater }
407410
}) {
408-
Some(idx) => {
411+
slice::Found(idx) => {
409412
let (_, _, cat) = r[idx];
410413
cat
411414
}
412-
None => GC_Any
415+
slice::NotFound(_) => GC_Any
413416
}
414417
}
415418
@@ -427,20 +430,21 @@ def emit_grapheme_module(f, grapheme_table, grapheme_cats):
427430
def emit_charwidth_module(f, width_table):
428431
f.write("pub mod charwidth {\n")
429432
f.write(" use core::option::{Option, Some, None};\n")
430-
f.write(" use core::slice::ImmutableVector;\n")
433+
f.write(" use core::slice::ImmutableSlice;\n")
434+
f.write(" use core::slice;\n")
431435
f.write("""
432436
fn bsearch_range_value_table(c: char, is_cjk: bool, r: &'static [(char, char, u8, u8)]) -> u8 {
433437
use core::cmp::{Equal, Less, Greater};
434-
match r.bsearch(|&(lo, hi, _, _)| {
438+
match r.binary_search(|&(lo, hi, _, _)| {
435439
if lo <= c && c <= hi { Equal }
436440
else if hi < c { Less }
437441
else { Greater }
438442
}) {
439-
Some(idx) => {
443+
slice::Found(idx) => {
440444
let (_, _, r_ncjk, r_cjk) = r[idx];
441445
if is_cjk { r_cjk } else { r_ncjk }
442446
}
443-
None => 1
447+
slice::NotFound(_) => 1
444448
}
445449
}
446450
""")
@@ -525,19 +529,19 @@ def comp_pfun(char):
525529

526530
f.write("""
527531
fn bsearch_range_value_table(c: char, r: &'static [(char, char, u8)]) -> u8 {
528-
use core::option::{Some, None};
529532
use core::cmp::{Equal, Less, Greater};
530-
use core::slice::ImmutableVector;
531-
match r.bsearch(|&(lo, hi, _)| {
533+
use core::slice::ImmutableSlice;
534+
use core::slice;
535+
match r.binary_search(|&(lo, hi, _)| {
532536
if lo <= c && c <= hi { Equal }
533537
else if hi < c { Less }
534538
else { Greater }
535539
}) {
536-
Some(idx) => {
540+
slice::Found(idx) => {
537541
let (_, _, result) = r[idx];
538542
result
539543
}
540-
None => 0
544+
slice::NotFound(_) => 0
541545
}
542546
}\n
543547
""")

src/libcollections/hash/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ mod tests {
296296
use std::prelude::*;
297297
use std::mem;
298298

299-
use slice::ImmutableVector;
299+
use slice::ImmutableSlice;
300300
use super::{Hash, Hasher, Writer};
301301

302302
struct MyWriterHasher;

src/libcollections/hash/sip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ mod tests {
275275

276276
use str::Str;
277277
use string::String;
278-
use slice::{Vector, ImmutableVector};
278+
use slice::{Slice, ImmutableSlice};
279279
use vec::Vec;
280280

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

src/libcollections/ringbuf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ pub struct MutItems<'a, T> {
362362

363363
impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
364364
#[inline]
365+
#[allow(deprecated)] // mut_shift_ref
365366
fn next(&mut self) -> Option<&'a mut T> {
366367
if self.nelts == 0 {
367368
return None;
@@ -384,6 +385,7 @@ impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
384385

385386
impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
386387
#[inline]
388+
#[allow(deprecated)] // mut_shift_ref
387389
fn next_back(&mut self) -> Option<&'a mut T> {
388390
if self.nelts == 0 {
389391
return None;

src/libcollections/slice.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ represents iteration over a slice.
4545
## Traits
4646
4747
A number of traits add methods that allow you to accomplish tasks with slices.
48-
These traits include `ImmutableVector`, which is defined for `&[T]` types,
49-
and `MutableVector`, defined for `&mut [T]` types.
48+
These traits include `ImmutableSlice`, which is defined for `&[T]` types,
49+
and `MutableSlice`, defined for `&mut [T]` types.
5050
5151
An example is the method `.slice(a, b)` that returns an immutable "view" into
5252
a `Vec` or another slice from the index interval `[a, b)`:
@@ -98,10 +98,11 @@ use {Collection, MutableSeq};
9898
use vec::Vec;
9999

100100
pub use core::slice::{ref_slice, mut_ref_slice, Splits, Windows};
101-
pub use core::slice::{Chunks, Vector, ImmutableVector, ImmutableEqVector};
102-
pub use core::slice::{ImmutableOrdVector, MutableVector, Items, MutItems};
101+
pub use core::slice::{Chunks, Slice, ImmutableSlice, ImmutablePartialEqSlice};
102+
pub use core::slice::{ImmutableOrdSlice, MutableSlice, Items, MutItems};
103103
pub use core::slice::{MutSplits, MutChunks};
104-
pub use core::slice::{bytes, MutableCloneableVector};
104+
pub use core::slice::{bytes, MutableCloneableSlice};
105+
pub use core::slice::{BinarySearchResult, Found, NotFound};
105106

106107
// Functional utilities
107108

@@ -116,7 +117,7 @@ pub trait VectorVector<T> {
116117
fn connect_vec(&self, sep: &T) -> Vec<T>;
117118
}
118119

119-
impl<'a, T: Clone, V: Vector<T>> VectorVector<T> for &'a [V] {
120+
impl<'a, T: Clone, V: Slice<T>> VectorVector<T> for &'a [V] {
120121
fn concat_vec(&self) -> Vec<T> {
121122
let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len());
122123
let mut result = Vec::with_capacity(size);
@@ -558,7 +559,7 @@ fn merge_sort<T>(v: &mut [T], compare: |&T, &T| -> Ordering) {
558559

559560
/// Extension methods for vectors such that their elements are
560561
/// mutable.
561-
pub trait MutableVectorAllocating<'a, T> {
562+
pub trait MutableSliceAllocating<'a, T> {
562563
/// Sort the vector, in place, using `compare` to compare
563564
/// elements.
564565
///
@@ -604,7 +605,7 @@ pub trait MutableVectorAllocating<'a, T> {
604605
fn move_from(self, src: Vec<T>, start: uint, end: uint) -> uint;
605606
}
606607

607-
impl<'a,T> MutableVectorAllocating<'a, T> for &'a mut [T] {
608+
impl<'a,T> MutableSliceAllocating<'a, T> for &'a mut [T] {
608609
#[inline]
609610
fn sort_by(self, compare: |&T, &T| -> Ordering) {
610611
merge_sort(self, compare)
@@ -621,7 +622,7 @@ impl<'a,T> MutableVectorAllocating<'a, T> for &'a mut [T] {
621622

622623
/// Methods for mutable vectors with orderable elements, such as
623624
/// in-place sorting.
624-
pub trait MutableOrdVector<T> {
625+
pub trait MutableOrdSlice<T> {
625626
/// Sort the vector, in place.
626627
///
627628
/// This is equivalent to `self.sort_by(|a, b| a.cmp(b))`.
@@ -667,7 +668,7 @@ pub trait MutableOrdVector<T> {
667668
fn prev_permutation(self) -> bool;
668669
}
669670

670-
impl<'a, T: Ord> MutableOrdVector<T> for &'a mut [T] {
671+
impl<'a, T: Ord> MutableOrdSlice<T> for &'a mut [T] {
671672
#[inline]
672673
fn sort(self) {
673674
self.sort_by(|a,b| a.cmp(b))

src/libcollections/str.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ mod tests {
894894
use {Collection, MutableSeq};
895895

896896
use super::*;
897-
use std::slice::{Vector, ImmutableVector};
897+
use std::slice::{Slice, ImmutableSlice};
898898
use string::String;
899899
use vec::Vec;
900900

@@ -1812,38 +1812,38 @@ mod tests {
18121812
fn test_splitn_char_iterator() {
18131813
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
18141814

1815-
let split: Vec<&str> = data.splitn(' ', 3).collect();
1815+
let split: Vec<&str> = data.splitn(3, ' ').collect();
18161816
assert_eq!(split, vec!["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
18171817

1818-
let split: Vec<&str> = data.splitn(|c: char| c == ' ', 3).collect();
1818+
let split: Vec<&str> = data.splitn(3, |c: char| c == ' ').collect();
18191819
assert_eq!(split, vec!["\nMäry", "häd", "ä", "little lämb\nLittle lämb\n"]);
18201820

18211821
// Unicode
1822-
let split: Vec<&str> = data.splitn('ä', 3).collect();
1822+
let split: Vec<&str> = data.splitn(3, 'ä').collect();
18231823
assert_eq!(split, vec!["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
18241824

1825-
let split: Vec<&str> = data.splitn(|c: char| c == 'ä', 3).collect();
1825+
let split: Vec<&str> = data.splitn(3, |c: char| c == 'ä').collect();
18261826
assert_eq!(split, vec!["\nM", "ry h", "d ", " little lämb\nLittle lämb\n"]);
18271827
}
18281828

18291829
#[test]
18301830
fn test_rsplitn_char_iterator() {
18311831
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
18321832

1833-
let mut split: Vec<&str> = data.rsplitn(' ', 3).collect();
1833+
let mut split: Vec<&str> = data.rsplitn(3, ' ').collect();
18341834
split.reverse();
18351835
assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
18361836

1837-
let mut split: Vec<&str> = data.rsplitn(|c: char| c == ' ', 3).collect();
1837+
let mut split: Vec<&str> = data.rsplitn(3, |c: char| c == ' ').collect();
18381838
split.reverse();
18391839
assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
18401840

18411841
// Unicode
1842-
let mut split: Vec<&str> = data.rsplitn('ä', 3).collect();
1842+
let mut split: Vec<&str> = data.rsplitn(3, 'ä').collect();
18431843
split.reverse();
18441844
assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
18451845

1846-
let mut split: Vec<&str> = data.rsplitn(|c: char| c == 'ä', 3).collect();
1846+
let mut split: Vec<&str> = data.rsplitn(3, |c: char| c == 'ä').collect();
18471847
split.reverse();
18481848
assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
18491849
}

src/libcollections/string.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ use core::default::Default;
1818
use core::fmt;
1919
use core::mem;
2020
use core::ptr;
21-
use core::raw::Slice;
21+
// FIXME: ICE's abound if you import the `Slice` type while importing `Slice` trait
22+
use RawSlice = core::raw::Slice;
23+
use core::slice::Slice;
2224

2325
use {Collection, Mutable, MutableSeq};
2426
use hash;
2527
use str;
26-
use str::{CharRange, StrAllocating, MaybeOwned, Owned, Slice};
28+
use str::{CharRange, StrAllocating, MaybeOwned, Owned};
29+
use MaybeOwnedSlice = str::Slice; // So many `Slice`s...
2730
use vec::Vec;
2831

2932
/// A growable string stored as a UTF-8 encoded buffer.
@@ -130,15 +133,15 @@ impl String {
130133
/// ```
131134
pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> MaybeOwned<'a> {
132135
if str::is_utf8(v) {
133-
return Slice(unsafe { mem::transmute(v) })
136+
return MaybeOwnedSlice(unsafe { mem::transmute(v) })
134137
}
135138

136139
static TAG_CONT_U8: u8 = 128u8;
137140
static REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8
138141
let mut i = 0;
139142
let total = v.len();
140143
fn unsafe_get(xs: &[u8], i: uint) -> u8 {
141-
unsafe { *xs.unsafe_ref(i) }
144+
unsafe { *xs.unsafe_get(i) }
142145
}
143146
fn safe_get(xs: &[u8], i: uint, total: uint) -> u8 {
144147
if i >= total {
@@ -496,7 +499,7 @@ impl String {
496499
unsafe {
497500
// Attempt to not use an intermediate buffer by just pushing bytes
498501
// directly onto this string.
499-
let slice = Slice {
502+
let slice = RawSlice {
500503
data: self.vec.as_ptr().offset(cur_len as int),
501504
len: 4,
502505
};

src/libcollections/trie.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ macro_rules! iterator_impl {
926926
// such thing as invalid pointers and memory unsafety. The
927927
// reason is performance, without doing this we can get the
928928
// bench_iter_large microbenchmark down to about 30000 ns/iter
929-
// (using .unsafe_ref to index self.stack directly, 38000
929+
// (using .unsafe_get to index self.stack directly, 38000
930930
// ns/iter with [] checked indexing), but this smashes that down
931931
// to 13500 ns/iter.
932932
//

0 commit comments

Comments
 (0)