Skip to content

Commit 33e7a1f

Browse files
committed
auto merge of #5145 : Kimundi/rust/incoming, r=catamorphism
2 parents 5fc0ecc + c2be2ec commit 33e7a1f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/libcore/to_str.rs

+43
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,53 @@ impl<A:ToStr,B:ToStr,C:ToStr> ToStr for (A, B, C) {
7474
}
7575
}
7676

77+
impl<A:ToStr> ToStr for &[A] {
78+
#[inline(always)]
79+
pure fn to_str(&self) -> ~str {
80+
unsafe {
81+
// FIXME #4568
82+
// Bleh -- not really unsafe
83+
// push_str and push_char
84+
let mut acc = ~"[", first = true;
85+
for self.each |elt| {
86+
unsafe {
87+
if first { first = false; }
88+
else { str::push_str(&mut acc, ~", "); }
89+
str::push_str(&mut acc, elt.to_str());
90+
}
91+
}
92+
str::push_char(&mut acc, ']');
93+
acc
94+
}
95+
}
96+
}
97+
7798
impl<A:ToStr> ToStr for ~[A] {
7899
#[inline(always)]
79100
pure fn to_str(&self) -> ~str {
80101
unsafe {
102+
// FIXME #4568
103+
// Bleh -- not really unsafe
104+
// push_str and push_char
105+
let mut acc = ~"[", first = true;
106+
for self.each |elt| {
107+
unsafe {
108+
if first { first = false; }
109+
else { str::push_str(&mut acc, ~", "); }
110+
str::push_str(&mut acc, elt.to_str());
111+
}
112+
}
113+
str::push_char(&mut acc, ']');
114+
acc
115+
}
116+
}
117+
}
118+
119+
impl<A:ToStr> ToStr for @[A] {
120+
#[inline(always)]
121+
pure fn to_str(&self) -> ~str {
122+
unsafe {
123+
// FIXME #4568
81124
// Bleh -- not really unsafe
82125
// push_str and push_char
83126
let mut acc = ~"[", first = true;

src/test/run-pass/vec-to_str.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main() {
12+
assert (~[0, 1]).to_str() == ~"[0, 1]";
13+
assert (&[1, 2]).to_str() == ~"[1, 2]";
14+
assert (@[2, 3]).to_str() == ~"[2, 3]";
15+
16+
let foo = ~[3, 4];
17+
let bar = &[4, 5];
18+
let baz = @[5, 6];
19+
20+
assert foo.to_str() == ~"[3, 4]";
21+
assert bar.to_str() == ~"[4, 5]";
22+
assert baz.to_str() == ~"[5, 6]";
23+
24+
}

0 commit comments

Comments
 (0)