Skip to content

Instantly share code, notes, and snippets.

Created December 27, 2013 01:06
Show Gist options
  • Save anonymous/8141034 to your computer and use it in GitHub Desktop.
Save anonymous/8141034 to your computer and use it in GitHub Desktop.
use output_stream::OutputStream;
use message::Message;
use wire_type::LengthDelimited;
pub struct OutputCounter {
byte_count: uint,
nested: ~[uint]
}
impl OutputCounter {
pub fn new() -> OutputCounter {
OutputCounter{ byte_count: 0, nested: ~[] }
}
}
impl OutputStream for OutputCounter {
fn write_byte(&mut self, _: u8) {
self.byte_count += 1;
}
fn write_f64(&mut self, _: f64) {
self.byte_count += 8;
}
fn write_nested(&mut self, field: uint, message: &Message) {
self.write_tag_and_wire_type(field as u64, LengthDelimited);
let prev_count = self.byte_count;
{
let stream = self as &mut OutputStream;
message.write_to_buffer(stream);
}
let nested_size = self.byte_count - prev_count;
self.write_u64(nested_size as u64);
self.nested.push(nested_size);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment