Skip to content

Commit 12b7b44

Browse files
committed
auto merge of #19378 : japaric/rust/no-as-slice, r=alexcrichton
Now that we have an overloaded comparison (`==`) operator, and that `Vec`/`String` deref to `[T]`/`str` on method calls, many `as_slice()`/`as_mut_slice()`/`to_string()` calls have become redundant. This patch removes them. These were the most common patterns: - `assert_eq(test_output.as_slice(), "ground truth")` -> `assert_eq(test_output, "ground truth")` - `assert_eq(test_output, "ground truth".to_string())` -> `assert_eq(test_output, "ground truth")` - `vec.as_mut_slice().sort()` -> `vec.sort()` - `vec.as_slice().slice(from, to)` -> `vec.slice(from_to)` --- Note that e.g. `a_string.push_str(b_string.as_slice())` has been left untouched in this PR, since we first need to settle down whether we want to favor the `&*b_string` or the `b_string[]` notation. This is rebased on top of #19167 cc @alexcrichton @aturon
2 parents 6f4c11b + f569d5c commit 12b7b44

Some content is hidden

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

116 files changed

+956
-990
lines changed

src/liballoc/arc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ mod tests {
521521
#[test]
522522
fn show_arc() {
523523
let a = Arc::new(5u32);
524-
assert!(format!("{}", a).as_slice() == "5")
524+
assert!(format!("{}", a) == "5")
525525
}
526526

527527
// Make sure deriving works with Arc<T>

src/liballoc/boxed.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ mod test {
169169
let b = box Test as Box<Any>;
170170
let a_str = a.to_str();
171171
let b_str = b.to_str();
172-
assert_eq!(a_str.as_slice(), "Box<Any>");
173-
assert_eq!(b_str.as_slice(), "Box<Any>");
172+
assert_eq!(a_str, "Box<Any>");
173+
assert_eq!(b_str, "Box<Any>");
174174

175175
let a = &8u as &Any;
176176
let b = &Test as &Any;
177177
let s = format!("{}", a);
178-
assert_eq!(s.as_slice(), "&Any");
178+
assert_eq!(s, "&Any");
179179
let s = format!("{}", b);
180-
assert_eq!(s.as_slice(), "&Any");
180+
assert_eq!(s, "&Any");
181181
}
182182
}

src/libcollections/binary_heap.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl<T: Ord> BinaryHeap<T> {
485485
let mut end = q.len();
486486
while end > 1 {
487487
end -= 1;
488-
q.data.as_mut_slice().swap(0, end);
488+
q.data.swap(0, end);
489489
q.siftdown_range(0, end)
490490
}
491491
q.into_vec()
@@ -769,8 +769,8 @@ mod tests {
769769
v.sort();
770770
data.sort();
771771

772-
assert_eq!(v.as_slice(), data.as_slice());
773-
assert_eq!(heap.into_sorted_vec().as_slice(), data.as_slice());
772+
assert_eq!(v, data);
773+
assert_eq!(heap.into_sorted_vec(), data);
774774
}
775775

776776
#[test]
@@ -812,7 +812,7 @@ mod tests {
812812
fn test_from_iter() {
813813
let xs = vec!(9u, 8, 7, 6, 5, 4, 3, 2, 1);
814814

815-
let mut q: BinaryHeap<uint> = xs.as_slice().iter().rev().map(|&x| x).collect();
815+
let mut q: BinaryHeap<uint> = xs.iter().rev().map(|&x| x).collect();
816816

817817
for &x in xs.iter() {
818818
assert_eq!(q.pop().unwrap(), x);

src/libcollections/bit.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1692,10 +1692,10 @@ mod tests {
16921692
#[test]
16931693
fn test_to_str() {
16941694
let zerolen = Bitv::new();
1695-
assert_eq!(zerolen.to_string().as_slice(), "");
1695+
assert_eq!(zerolen.to_string(), "");
16961696

16971697
let eightbits = Bitv::with_capacity(8u, false);
1698-
assert_eq!(eightbits.to_string().as_slice(), "00000000")
1698+
assert_eq!(eightbits.to_string(), "00000000")
16991699
}
17001700

17011701
#[test]
@@ -1718,7 +1718,7 @@ mod tests {
17181718
let mut b = bitv::Bitv::with_capacity(2, false);
17191719
b.set(0, true);
17201720
b.set(1, false);
1721-
assert_eq!(b.to_string().as_slice(), "10");
1721+
assert_eq!(b.to_string(), "10");
17221722
}
17231723

17241724
#[test]
@@ -2029,7 +2029,7 @@ mod tests {
20292029
fn test_from_bytes() {
20302030
let bitv = from_bytes(&[0b10110110, 0b00000000, 0b11111111]);
20312031
let str = format!("{}{}{}", "10110110", "00000000", "11111111");
2032-
assert_eq!(bitv.to_string().as_slice(), str.as_slice());
2032+
assert_eq!(bitv.to_string(), str);
20332033
}
20342034

20352035
#[test]
@@ -2048,7 +2048,7 @@ mod tests {
20482048
fn test_from_bools() {
20492049
let bools = vec![true, false, true, true];
20502050
let bitv: Bitv = bools.iter().map(|n| *n).collect();
2051-
assert_eq!(bitv.to_string().as_slice(), "1011");
2051+
assert_eq!(bitv.to_string(), "1011");
20522052
}
20532053

20542054
#[test]
@@ -2207,7 +2207,7 @@ mod tests {
22072207

22082208
let expected = [3, 5, 11, 77];
22092209
let actual = a.intersection(&b).collect::<Vec<uint>>();
2210-
assert_eq!(actual.as_slice(), expected.as_slice());
2210+
assert_eq!(actual, expected);
22112211
}
22122212

22132213
#[test]
@@ -2226,7 +2226,7 @@ mod tests {
22262226

22272227
let expected = [1, 5, 500];
22282228
let actual = a.difference(&b).collect::<Vec<uint>>();
2229-
assert_eq!(actual.as_slice(), expected.as_slice());
2229+
assert_eq!(actual, expected);
22302230
}
22312231

22322232
#[test]
@@ -2247,7 +2247,7 @@ mod tests {
22472247

22482248
let expected = [1, 5, 11, 14, 220];
22492249
let actual = a.symmetric_difference(&b).collect::<Vec<uint>>();
2250-
assert_eq!(actual.as_slice(), expected.as_slice());
2250+
assert_eq!(actual, expected);
22512251
}
22522252

22532253
#[test]
@@ -2272,7 +2272,7 @@ mod tests {
22722272

22732273
let expected = [1, 3, 5, 9, 11, 13, 19, 24, 160, 200];
22742274
let actual = a.union(&b).collect::<Vec<uint>>();
2275-
assert_eq!(actual.as_slice(), expected.as_slice());
2275+
assert_eq!(actual, expected);
22762276
}
22772277

22782278
#[test]
@@ -2660,7 +2660,7 @@ mod tests {
26602660
s.insert(10);
26612661
s.insert(50);
26622662
s.insert(2);
2663-
assert_eq!("{1, 2, 10, 50}".to_string(), s.to_string());
2663+
assert_eq!("{1, 2, 10, 50}", s.to_string());
26642664
}
26652665

26662666
fn rng() -> rand::IsaacRng {

src/libcollections/btree/node.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -158,43 +158,43 @@ impl <K, V> Node<K, V> {
158158
/// Swap the given key-value pair with the key-value pair stored in the node's index,
159159
/// without checking bounds.
160160
pub unsafe fn unsafe_swap(&mut self, index: uint, key: &mut K, val: &mut V) {
161-
mem::swap(self.keys.as_mut_slice().unsafe_mut(index), key);
162-
mem::swap(self.vals.as_mut_slice().unsafe_mut(index), val);
161+
mem::swap(self.keys.unsafe_mut(index), key);
162+
mem::swap(self.vals.unsafe_mut(index), val);
163163
}
164164

165165
/// Get the node's key mutably without any bounds checks.
166166
pub unsafe fn unsafe_key_mut(&mut self, index: uint) -> &mut K {
167-
self.keys.as_mut_slice().unsafe_mut(index)
167+
self.keys.unsafe_mut(index)
168168
}
169169

170170
/// Get the node's value at the given index
171171
pub fn val(&self, index: uint) -> Option<&V> {
172-
self.vals.as_slice().get(index)
172+
self.vals.get(index)
173173
}
174174

175175
/// Get the node's value at the given index
176176
pub fn val_mut(&mut self, index: uint) -> Option<&mut V> {
177-
self.vals.as_mut_slice().get_mut(index)
177+
self.vals.get_mut(index)
178178
}
179179

180180
/// Get the node's value mutably without any bounds checks.
181181
pub unsafe fn unsafe_val_mut(&mut self, index: uint) -> &mut V {
182-
self.vals.as_mut_slice().unsafe_mut(index)
182+
self.vals.unsafe_mut(index)
183183
}
184184

185185
/// Get the node's edge at the given index
186186
pub fn edge(&self, index: uint) -> Option<&Node<K,V>> {
187-
self.edges.as_slice().get(index)
187+
self.edges.get(index)
188188
}
189189

190190
/// Get the node's edge mutably at the given index
191191
pub fn edge_mut(&mut self, index: uint) -> Option<&mut Node<K,V>> {
192-
self.edges.as_mut_slice().get_mut(index)
192+
self.edges.get_mut(index)
193193
}
194194

195195
/// Get the node's edge mutably without any bounds checks.
196196
pub unsafe fn unsafe_edge_mut(&mut self, index: uint) -> &mut Node<K,V> {
197-
self.edges.as_mut_slice().unsafe_mut(index)
197+
self.edges.unsafe_mut(index)
198198
}
199199

200200
/// Pop an edge off the end of the node
@@ -281,8 +281,8 @@ impl <K, V> Node<K, V> {
281281
pub fn iter<'a>(&'a self) -> Traversal<'a, K, V> {
282282
let is_leaf = self.is_leaf();
283283
Traversal {
284-
elems: self.keys.as_slice().iter().zip(self.vals.as_slice().iter()),
285-
edges: self.edges.as_slice().iter(),
284+
elems: self.keys.iter().zip(self.vals.iter()),
285+
edges: self.edges.iter(),
286286
head_is_edge: true,
287287
tail_is_edge: true,
288288
has_edges: !is_leaf,
@@ -292,8 +292,8 @@ impl <K, V> Node<K, V> {
292292
pub fn iter_mut<'a>(&'a mut self) -> MutTraversal<'a, K, V> {
293293
let is_leaf = self.is_leaf();
294294
MutTraversal {
295-
elems: self.keys.as_slice().iter().zip(self.vals.as_mut_slice().iter_mut()),
296-
edges: self.edges.as_mut_slice().iter_mut(),
295+
elems: self.keys.iter().zip(self.vals.iter_mut()),
296+
edges: self.edges.iter_mut(),
297297
head_is_edge: true,
298298
tail_is_edge: true,
299299
has_edges: !is_leaf,
@@ -477,8 +477,8 @@ fn split<T>(left: &mut Vec<T>) -> Vec<T> {
477477
let left_len = len - right_len;
478478
let mut right = Vec::with_capacity(left.capacity());
479479
unsafe {
480-
let left_ptr = left.as_slice().unsafe_get(left_len) as *const _;
481-
let right_ptr = right.as_mut_slice().as_mut_ptr();
480+
let left_ptr = left.unsafe_get(left_len) as *const _;
481+
let right_ptr = right.as_mut_ptr();
482482
ptr::copy_nonoverlapping_memory(right_ptr, left_ptr, right_len);
483483
left.set_len(left_len);
484484
right.set_len(right_len);

src/libcollections/btree/set.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ mod test {
580580

581581
let set_str = format!("{}", set);
582582

583-
assert!(set_str == "{1, 2}".to_string());
584-
assert_eq!(format!("{}", empty), "{}".to_string());
583+
assert!(set_str == "{1, 2}");
584+
assert_eq!(format!("{}", empty), "{}");
585585
}
586586
}

src/libcollections/dlist.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ mod tests {
926926
let mut m = list_from(v.as_slice());
927927
m.prepend(list_from(u.as_slice()));
928928
check_links(&m);
929-
u.extend(v.as_slice().iter().map(|&b| b));
929+
u.extend(v.iter().map(|&b| b));
930930
assert_eq!(u.len(), m.len());
931931
for elt in u.into_iter() {
932932
assert_eq!(m.pop_front(), Some(elt))
@@ -1133,7 +1133,7 @@ mod tests {
11331133
spawn(proc() {
11341134
check_links(&n);
11351135
let a: &[_] = &[&1,&2,&3];
1136-
assert_eq!(a, n.iter().collect::<Vec<&int>>().as_slice());
1136+
assert_eq!(a, n.iter().collect::<Vec<&int>>());
11371137
});
11381138
}
11391139

@@ -1224,12 +1224,12 @@ mod tests {
12241224
#[test]
12251225
fn test_show() {
12261226
let list: DList<int> = range(0i, 10).collect();
1227-
assert!(list.to_string().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1227+
assert!(list.to_string() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
12281228

12291229
let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
12301230
.map(|&s| s)
12311231
.collect();
1232-
assert!(list.to_string().as_slice() == "[just, one, test, more]");
1232+
assert!(list.to_string() == "[just, one, test, more]");
12331233
}
12341234

12351235
#[cfg(test)]

src/libcollections/enum_set.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ mod test {
288288
#[test]
289289
fn test_show() {
290290
let mut e = EnumSet::new();
291-
assert_eq!("{}", e.to_string().as_slice());
291+
assert_eq!("{}", e.to_string());
292292
e.insert(A);
293-
assert_eq!("{A}", e.to_string().as_slice());
293+
assert_eq!("{A}", e.to_string());
294294
e.insert(C);
295-
assert_eq!("{A, C}", e.to_string().as_slice());
295+
assert_eq!("{A, C}", e.to_string());
296296
}
297297

298298
#[test]

src/libcollections/ring_buf.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1246,15 +1246,15 @@ mod tests {
12461246
}
12471247
{
12481248
let b: &[_] = &[&0,&1,&2,&3,&4];
1249-
assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), b);
1249+
assert_eq!(d.iter().collect::<Vec<&int>>(), b);
12501250
}
12511251

12521252
for i in range(6i, 9) {
12531253
d.push_front(i);
12541254
}
12551255
{
12561256
let b: &[_] = &[&8,&7,&6,&0,&1,&2,&3,&4];
1257-
assert_eq!(d.iter().collect::<Vec<&int>>().as_slice(), b);
1257+
assert_eq!(d.iter().collect::<Vec<&int>>(), b);
12581258
}
12591259

12601260
let mut it = d.iter();
@@ -1277,14 +1277,14 @@ mod tests {
12771277
}
12781278
{
12791279
let b: &[_] = &[&4,&3,&2,&1,&0];
1280-
assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), b);
1280+
assert_eq!(d.iter().rev().collect::<Vec<&int>>(), b);
12811281
}
12821282

12831283
for i in range(6i, 9) {
12841284
d.push_front(i);
12851285
}
12861286
let b: &[_] = &[&4,&3,&2,&1,&0,&6,&7,&8];
1287-
assert_eq!(d.iter().rev().collect::<Vec<&int>>().as_slice(), b);
1287+
assert_eq!(d.iter().rev().collect::<Vec<&int>>(), b);
12881288
}
12891289

12901290
#[test]
@@ -1495,12 +1495,12 @@ mod tests {
14951495
#[test]
14961496
fn test_show() {
14971497
let ringbuf: RingBuf<int> = range(0i, 10).collect();
1498-
assert!(format!("{}", ringbuf).as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1498+
assert!(format!("{}", ringbuf) == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
14991499

15001500
let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter()
15011501
.map(|&s| s)
15021502
.collect();
1503-
assert!(format!("{}", ringbuf).as_slice() == "[just, one, test, more]");
1503+
assert!(format!("{}", ringbuf) == "[just, one, test, more]");
15041504
}
15051505

15061506
#[test]

0 commit comments

Comments
 (0)