1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use super::WriteOptions;
use svgparser::Stream;
pub trait FromStream: Sized {
type Err;
fn from_stream(s: Stream) -> Result<Self, Self::Err>;
fn from_data(data: &[u8]) -> Result<Self, Self::Err> {
FromStream::from_stream(Stream::new(data))
}
}
pub trait WriteBuffer {
fn write_buf_opt(&self, opt: &WriteOptions, buf: &mut Vec<u8>);
fn write_buf(&self, buf: &mut Vec<u8>) {
self.write_buf_opt(&WriteOptions::default(), buf);
}
}
pub trait WriteToString: WriteBuffer {
fn to_string_with_opt(&self, opt: &WriteOptions) -> String {
let mut out = Vec::with_capacity(32);
self.write_buf_opt(opt, &mut out);
String::from_utf8(out).unwrap()
}
}
macro_rules! impl_display {
($t:ty) => (
impl fmt::Display for $t {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut out = Vec::with_capacity(32);
self.write_buf(&mut out);
write!(f, "{}", String::from_utf8(out).unwrap())
}
}
impl WriteToString for $t {}
)
}