Skip to content

Commit a31c226

Browse files
committed
---
yaml --- r: 44798 b: refs/heads/master c: c2be2ec h: refs/heads/master v: v3
1 parent 2dff9be commit a31c226

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 061a2237230d3abcdb30ecb8987e5de17e67a58e
2+
refs/heads/master: c2be2ec42df4381ad805d81ff964215c3ce414b3
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d9689399d091c3265f00434a69c551a61c28dc
55
refs/heads/try: ef355f6332f83371e4acf04fc4eb940ab41d78d3

trunk/src/libcore/to_str.rs

Lines changed: 43 additions & 0 deletions
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;

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

Lines changed: 24 additions & 0 deletions
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)