Make opaque::Encoder append-only and make it infallible

This commit is contained in:
John Kåre Alsaker 2018-06-04 22:14:02 +02:00
parent 971f7d34d4
commit 14d3c6e8f4
12 changed files with 80 additions and 118 deletions

View file

@ -41,17 +41,16 @@ enum WireProtocol {
fn encode_json<T: Encodable>(val: &T, wr: &mut Cursor<Vec<u8>>) {
write!(wr, "{}", json::as_json(val));
}
fn encode_opaque<T: Encodable>(val: &T, wr: &mut Cursor<Vec<u8>>) {
fn encode_opaque<T: Encodable>(val: &T, wr: Vec<u8>) {
let mut encoder = opaque::Encoder::new(wr);
val.encode(&mut encoder);
}
pub fn main() {
let target = Foo{baz: false,};
let mut wr = Cursor::new(Vec::new());
let proto = WireProtocol::JSON;
match proto {
WireProtocol::JSON => encode_json(&target, &mut wr),
WireProtocol::Opaque => encode_opaque(&target, &mut wr)
WireProtocol::JSON => encode_json(&target, &mut Cursor::new(Vec::new())),
WireProtocol::Opaque => encode_opaque(&target, Vec::new())
}
}