54 lines
942 B
Rust
54 lines
942 B
Rust
// output trait and some possibilities for outputs
|
|
pub trait Output {
|
|
fn write(&mut self, data : &str);
|
|
}
|
|
|
|
|
|
pub struct DummyOutput {
|
|
out : String
|
|
}
|
|
|
|
|
|
impl Output for DummyOutput {
|
|
fn write(&mut self, data : &str) {
|
|
self.out += data;
|
|
}
|
|
}
|
|
|
|
|
|
impl DummyOutput {
|
|
pub fn print(&mut self) {
|
|
println!("{}", self.out);
|
|
}
|
|
|
|
pub fn spool(self) -> String {
|
|
self.out
|
|
}
|
|
|
|
pub fn new() -> DummyOutput {
|
|
DummyOutput {
|
|
out : String::new()
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
pub struct StringFillerOutput<'s> {
|
|
string : &'s mut String
|
|
}
|
|
|
|
|
|
impl<'s> Output for StringFillerOutput<'s> {
|
|
fn write(&mut self, data : &str) {
|
|
*self.string += data;
|
|
}
|
|
}
|
|
|
|
impl<'a> StringFillerOutput<'a> {
|
|
pub fn new(string : &'a mut String) -> StringFillerOutput<'a> {
|
|
// TODO: reserve() to make this faster than dummyoutput
|
|
StringFillerOutput {
|
|
string
|
|
}
|
|
}
|
|
} |