Skip to content

Commit 3b9f2e7

Browse files
committed
Replace ~[T] to std::vec_ng::Vec<T>
See also rust-lang/rust#12861
1 parent f23560f commit 3b9f2e7

File tree

4 files changed

+124
-96
lines changed

4 files changed

+124
-96
lines changed

src/mustache/encoder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ extern crate serialize;
22
extern crate collections;
33

44
use std::str;
5+
use std::vec_ng::Vec;
56
use collections::hashmap::HashMap;
67

78
/// Represents template data.
89
#[deriving(Clone)]
910
pub enum Data {
1011
Str(~str),
1112
Bool(bool),
12-
Vec(~[Data]),
13+
Vec(Vec<Data>),
1314
Map(HashMap<~str, Data>),
1415
//Fun(fn(~str) -> ~str),
1516
}
1617

1718
pub struct Encoder {
18-
data: ~[Data],
19+
data: Vec<Data>,
1920
}
2021

2122
impl Encoder {
2223
pub fn new() -> Encoder {
23-
Encoder { data: ~[] }
24+
Encoder { data: Vec::new() }
2425
}
2526
}
2627

@@ -134,7 +135,7 @@ impl serialize::Encoder for Encoder {
134135
}
135136

136137
fn emit_seq(&mut self, _len: uint, f: |&mut Encoder|) {
137-
self.data.push(Vec(~[]));
138+
self.data.push(Vec(Vec::new()));
138139
f(self);
139140
}
140141

src/mustache/mustache.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern crate collections;
66

77
use std::io::File;
88
use std::str;
9+
use std::vec_ng::Vec;
910
use collections::hashmap::HashMap;
1011

1112
pub use parser::{Token, Parser};
@@ -25,8 +26,8 @@ pub struct Context {
2526

2627
pub struct Template {
2728
ctx: Context,
28-
tokens: ~[Token],
29-
partials: HashMap<~str, ~[Token]>
29+
tokens: Vec<Token>,
30+
partials: HashMap<~str, Vec<Token>>
3031
}
3132

3233
impl Context {
@@ -141,7 +142,7 @@ impl Template {
141142
// This should be `tokens: self.tokens,` but that's broken
142143
tokens: self.tokens.as_slice(),
143144
partials: self.partials.clone(),
144-
stack: ~[data],
145+
stack: vec!(data),
145146
indent: ~""
146147
})
147148
}
@@ -150,15 +151,15 @@ impl Template {
150151

151152
struct CompileContext<'a, T> {
152153
reader: &'a mut T,
153-
partials: HashMap<~str, ~[Token]>,
154+
partials: HashMap<~str, Vec<Token>>,
154155
otag: ~str,
155156
ctag: ~str,
156157
template_path: Path,
157158
template_extension: ~str,
158159
}
159160

160161
impl<'a, T: Iterator<char>> CompileContext<'a, T> {
161-
pub fn compile(&mut self) -> ~[Token] {
162+
pub fn compile(&mut self) -> Vec<Token> {
162163
let mut parser = Parser {
163164
reader: self.reader,
164165
ch: None,
@@ -169,11 +170,11 @@ impl<'a, T: Iterator<char>> CompileContext<'a, T> {
169170
state: parser::TEXT,
170171
otag: self.otag.to_owned(),
171172
ctag: self.ctag.to_owned(),
172-
otag_chars: self.otag.chars().collect::<~[char]>(),
173-
ctag_chars: self.ctag.chars().collect::<~[char]>(),
173+
otag_chars: self.otag.chars().collect(),
174+
ctag_chars: self.ctag.chars().collect(),
174175
tag_position: 0,
175-
tokens: ~[],
176-
partials: ~[],
176+
tokens: Vec::new(),
177+
partials: Vec::new(),
177178
};
178179

179180
parser.bump();
@@ -185,7 +186,7 @@ impl<'a, T: Iterator<char>> CompileContext<'a, T> {
185186

186187
if !self.partials.contains_key(name) {
187188
// Insert a placeholder so we don't recurse off to infinity.
188-
self.partials.insert(name.to_owned(), ~[]);
189+
self.partials.insert(name.to_owned(), Vec::new());
189190
match File::open(&path).read_to_end() {
190191
Ok(contents) => {
191192

@@ -222,8 +223,8 @@ impl<'a, T: Iterator<char>> CompileContext<'a, T> {
222223
struct RenderContext<'a> {
223224
ctx: Context,
224225
tokens: &'a [Token],
225-
partials: HashMap<~str, ~[Token]>,
226-
stack: ~[Data],
226+
partials: HashMap<~str, Vec<Token>>,
227+
stack: Vec<Data>,
227228
indent: ~str,
228229
}
229230

@@ -314,15 +315,15 @@ fn render_helper(ctx: &RenderContext) -> ~str {
314315
}
315316
},
316317
parser::ETag(ref path, _) => {
317-
match find(ctx.stack, *path) {
318+
match find(ctx.stack.as_slice(), path.as_slice()) {
318319
None => { }
319320
Some(value) => {
320321
output = output + ctx.indent + render_etag(value, ctx);
321322
}
322323
}
323324
}
324325
parser::UTag(ref path, _) => {
325-
match find(ctx.stack, *path) {
326+
match find(ctx.stack.as_slice(), path.as_slice()) {
326327
None => { }
327328
Some(value) => {
328329
output = output + ctx.indent + render_utag(value, ctx);
@@ -337,13 +338,13 @@ fn render_helper(ctx: &RenderContext) -> ~str {
337338
.. ctx.clone()
338339
};
339340

340-
output = output + match find(ctx.stack, *path) {
341+
output = output + match find(ctx.stack.as_slice(), path.as_slice()) {
341342
None => { render_helper(&ctx) }
342343
Some(value) => { render_inverted_section(value, &ctx) }
343344
};
344345
}
345346
parser::Section(ref path, false, ref children, ref otag, _, ref src, _, ref ctag) => {
346-
match find(ctx.stack, *path) {
347+
match find(ctx.stack.as_slice(), path.as_slice()) {
347348
None => { }
348349
Some(value) => {
349350
output = output + render_section(
@@ -426,15 +427,15 @@ fn render_section(value: Data,
426427
Bool(true) => render_helper(ctx),
427428
Bool(false) => ~"",
428429
Vec(vs) => {
429-
vs.map(|v| {
430-
let mut stack = ctx.stack.to_owned();
430+
vs.move_iter().map(|v| {
431+
let mut stack = ctx.stack.clone();
431432
stack.push(v.clone());
432433

433434
render_helper(&RenderContext { stack: stack, .. (*ctx).clone() })
434-
}).concat()
435+
}).collect::<Vec<~str>>().concat()
435436
}
436437
Map(_) => {
437-
let mut stack = ctx.stack.to_owned();
438+
let mut stack = ctx.stack.clone();
438439
stack.push(value);
439440

440441
render_helper(&RenderContext { stack: stack, .. (*ctx).clone() })

src/mustache/parser.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
1+
use std::vec_ng::Vec;
22
use std::char;
33
use std::mem;
44

55

66
#[deriving(Clone)]
77
pub enum Token {
88
Text(~str),
9-
ETag(~[~str], ~str),
10-
UTag(~[~str], ~str),
11-
Section(~[~str], bool, ~[Token], ~str, ~str, ~str, ~str, ~str),
12-
IncompleteSection(~[~str], bool, ~str, bool),
9+
ETag(Vec<~str>, ~str),
10+
UTag(Vec<~str>, ~str),
11+
Section(Vec<~str>, bool, Vec<Token>, ~str, ~str, ~str, ~str, ~str),
12+
IncompleteSection(Vec<~str>, bool, ~str, bool),
1313
Partial(~str, ~str, ~str),
1414
}
1515

@@ -31,11 +31,11 @@ pub struct Parser<'a, T> {
3131
state: ParserState,
3232
otag: ~str,
3333
ctag: ~str,
34-
otag_chars: ~[char],
35-
ctag_chars: ~[char],
34+
otag_chars: Vec<char>,
35+
ctag_chars: Vec<char>,
3636
tag_position: uint,
37-
tokens: ~[Token],
38-
partials: ~[~str],
37+
tokens: Vec<Token>,
38+
partials: Vec<~str>,
3939
}
4040

4141
pub enum ParserState { TEXT, OTAG, TAG, CTAG }
@@ -88,7 +88,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
8888

8989
match self.state {
9090
TEXT => {
91-
if ch == self.otag_chars[0] {
91+
if ch == *self.otag_chars.get(0) {
9292
if self.otag_chars.len() > 1 {
9393
self.tag_position = 1;
9494
self.state = OTAG;
@@ -102,7 +102,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
102102
self.bump();
103103
}
104104
OTAG => {
105-
if ch == self.otag_chars[self.tag_position] {
105+
if ch == *self.otag_chars.get(self.tag_position) {
106106
if self.tag_position == self.otag_chars.len() - 1 {
107107
self.add_text();
108108
curly_brace_tag = false;
@@ -128,7 +128,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
128128
curly_brace_tag = false;
129129
self.content.push_char(ch);
130130
self.bump();
131-
} else if ch == self.ctag_chars[0] {
131+
} else if ch == *self.ctag_chars.get(0) {
132132
if self.ctag_chars.len() > 1 {
133133
self.tag_position = 1;
134134
self.state = CTAG;
@@ -143,7 +143,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
143143
}
144144
}
145145
CTAG => {
146-
if ch == self.ctag_chars[self.tag_position] {
146+
if ch == *self.ctag_chars.get(self.tag_position) {
147147
if self.tag_position == self.ctag_chars.len() - 1 {
148148
self.add_tag();
149149
self.state = TEXT;
@@ -156,7 +156,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
156156
} else {
157157
fail!("character {} is not part of CTAG: {}",
158158
ch,
159-
self.ctag_chars[self.tag_position]);
159+
*self.ctag_chars.get(self.tag_position));
160160
}
161161
}
162162
}
@@ -205,14 +205,14 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
205205
}
206206
}
207207

208-
// If the last token ends with a newline (or there is no previous
209-
// token), then this token is standalone.
210-
if self.tokens.len() == 0 { return StandAlone; }
208+
match self.tokens.last() {
209+
// If the last token ends with a newline (or there is no previous
210+
// token), then this token is standalone.
211+
None => { StandAlone }
211212

212-
match self.tokens[self.tokens.len() - 1] {
213-
IncompleteSection(_, _, _, true) => { StandAlone }
213+
Some(&IncompleteSection(_, _, _, true)) => { StandAlone }
214214

215-
Text(ref s) if !s.is_empty() => {
215+
Some(&Text(ref s)) if !s.is_empty() => {
216216
// Look for the last newline character that may have whitespace
217217
// following it.
218218
match s.rfind(|c:char| c == '\n' || !char::is_whitespace(c)) {
@@ -235,7 +235,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
235235
}
236236
}
237237
}
238-
_ => Normal,
238+
Some(_) => Normal,
239239
}
240240
}
241241

@@ -321,7 +321,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
321321
let name = name.split_terminator('.')
322322
.map(|x| x.to_owned())
323323
.collect();
324-
let mut children: ~[Token] = ~[];
324+
let mut children: Vec<Token> = Vec::new();
325325
326326
loop {
327327
if self.tokens.len() == 0 {
@@ -335,7 +335,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
335335
children.reverse();
336336
337337
// Collect all the children's sources.
338-
let mut srcs = ~[];
338+
let mut srcs = Vec::new();
339339
for child in children.iter() {
340340
match *child {
341341
Text(ref s)
@@ -418,7 +418,7 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
418418
// an empty name.
419419
let name = self.check_content(content);
420420
let name = if name == ~"." {
421-
~[]
421+
Vec::new()
422422
} else {
423423
name.split_terminator('.')
424424
.map(|x| x.to_owned())
@@ -465,15 +465,15 @@ impl<'a, T: Iterator<char>> Parser<'a, T> {
465465
fn not_otag(&mut self) {
466466
let mut i = 0;
467467
while i < self.tag_position {
468-
self.content.push_char(self.otag_chars[i]);
468+
self.content.push_char(*self.otag_chars.get(i));
469469
i += 1;
470470
}
471471
}
472472
473473
fn not_ctag(&mut self) {
474474
let mut i = 0;
475475
while i < self.tag_position {
476-
self.content.push_char(self.ctag_chars[i]);
476+
self.content.push_char(*self.ctag_chars.get(i));
477477
i += 1;
478478
}
479479
}

0 commit comments

Comments
 (0)