Skip to content

Instantly share code, notes, and snippets.

@Kimundi
Forked from anonymous/output_counter.rs
Last active January 1, 2016 11:59
Show Gist options
  • Save Kimundi/8141115 to your computer and use it in GitHub Desktop.
Save Kimundi/8141115 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_message(counter: &mut OutputCounter, msg: &Message) {
message.write_to_buffer(counter as &mut OutputStream);
}
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;
{
write_message(self, message);
}
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