Skip to content

Commit 31e334e

Browse files
committed
remove need for external buffers
1 parent f6dd734 commit 31e334e

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

examples/derive_example.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct Provider {
2525
provider_code: u32,
2626
}
2727

28-
fn sqrt(_data: &Person, args: &[f64], input: f64) -> f64 {
28+
fn sqrt(_data: &Person, _args: &[f64], input: f64) -> f64 {
2929
input.sqrt()
3030
}
3131

@@ -47,11 +47,11 @@ fn main() {
4747
"{{provider}} {{provider_code + 4}} {{id}} {{name | toupper}} {{age | sqrt}} {{weight / 2.2 | round 2}}kg\n";
4848

4949
let env = Provider {
50-
provider: "apns".to_string(),
50+
provider: "john doe".to_string(),
5151
provider_code: 31,
5252
};
5353

54-
let bytecode = match compile(template, &env) {
54+
let mut bytecode = match compile(template, &env) {
5555
Ok(bc) => bc,
5656
Err(err) => {
5757
eprintln!("error compiling template: {}", err);
@@ -72,15 +72,10 @@ fn main() {
7272
});
7373
}
7474

75-
// reuse these allocations throughout the output process
76-
let mut buffer = String::with_capacity(8);
77-
let mut stack = Vec::with_capacity(8);
7875
let stdout = stdout();
7976
let mut stdout_lock = stdout.lock();
8077

8178
for person in group {
82-
bytecode
83-
.run_with(&person, &mut buffer, &mut stack, &mut stdout_lock)
84-
.unwrap();
79+
bytecode.run_with(&person, &mut stdout_lock).unwrap();
8580
}
8681
}

examples/example.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ fn main() {
139139
let template = "{{provider}} {{provider_code + 4}} {{id}} {{name | toupper}} {{age | sqrt}} {{weight / 2.2 | round 2}}kg\n";
140140

141141
let env = Provider {
142-
provider: "apns".to_string(),
142+
provider: "john doe".to_string(),
143143
provider_code: 31,
144144
};
145145

146-
let bytecode = match compile(template, &env) {
146+
let mut bytecode = match compile(template, &env) {
147147
Ok(bc) => bc,
148148
Err(err) => {
149149
eprintln!("error compiling template: {}", err);
@@ -164,15 +164,10 @@ fn main() {
164164
});
165165
}
166166

167-
// reuse these allocations throughout the output process
168-
let mut buffer = String::with_capacity(8);
169-
let mut stack = Vec::with_capacity(8);
170167
let stdout = stdout();
171168
let mut stdout_lock = stdout.lock();
172169

173170
for person in group {
174-
bytecode
175-
.run_with(&person, &mut buffer, &mut stack, &mut stdout_lock)
176-
.unwrap();
171+
bytecode.run_with(&person, &mut stdout_lock).unwrap();
177172
}
178173
}

src/bytecode.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ enum Instr<NumEnum, StrEnum, FilterEnum> {
3030
#[allow(unused)]
3131
#[derive(Debug)]
3232
pub struct Bytecode<NumEnum, StrEnum, FilterEnum> {
33+
buffer: String,
34+
stack: Vec<f64>,
3335
raw_text: String,
3436
instructions: Vec<Instr<NumEnum, StrEnum, FilterEnum>>,
3537
}
@@ -53,6 +55,8 @@ impl<
5355
env: &Env,
5456
) -> Result<Bytecode<NumEnum, StrEnum, FilterEnum>, String> {
5557
let mut ret_val = Bytecode {
58+
buffer: String::with_capacity(8),
59+
stack: Vec::with_capacity(8),
5660
raw_text: String::new(),
5761
instructions: vec![],
5862
};
@@ -65,12 +69,12 @@ impl<
6569
}
6670

6771
pub fn run_with(
68-
&self,
72+
&mut self,
6973
runner: &Runner<NumEnum, StrEnum, FilterEnum>,
70-
buffer: &mut String,
71-
stack: &mut Vec<f64>,
7274
output: &mut Write,
7375
) -> Result<(), ::std::io::Error> {
76+
let stack = &mut self.stack;
77+
let buffer = &mut self.buffer;
7478
for instr in &self.instructions {
7579
match *instr {
7680
Instr::PushImm(val) => stack.push(val),

0 commit comments

Comments
 (0)