Struct quick_protobuf::writer::Writer
[−]
[src]
pub struct Writer<W> { /* fields omitted */ }
A struct to write protobuf messages
Examples
// an automatically generated module which is in a separate file in general mod foo_bar { pub struct Foo<'a> { pub name: Option<Cow<'a, str>>, } pub struct Bar { pub id: Option<u32> } pub struct FooBar<'a> { pub foos: Vec<Foo<'a>>, pub bars: Vec<Bar>, } impl<'a> MessageWrite for FooBar<'a> { // implements // fn get_size(&self) -> usize { ... } // fn write_message<W: Write>(&self, r: &mut Writer<W>) -> Result<()> { ... } } } // FooBar is a message generated from a proto file // in parcicular it contains a `write_message` function use foo_bar::{FooBar, Foo, Bar}; use std::fs::File; use std::borrow::Cow; use quick_protobuf::{MessageWrite, Writer}; fn main() { // let's say we want to write to a file let mut file: File; let mut writer = Writer::new(&mut file); // manually generates a FooBar for the example let foobar = FooBar { foos: vec![Foo { name: Some(Cow::Borrowed("test!")) }, Foo { name: None }], bars: vec![Bar { id: Some(43) }, Bar { id: None }], }; // now using the generated module writer.write_message(&foobar).expect("Cannot write FooBar"); }
Methods
impl<W: Write> Writer<W>
[src]
fn new(w: W) -> Writer<W>
Creates a new ProtobufWriter
fn write_varint(&mut self, v: u64) -> Result<()>
Writes a varint
(compacted u64
)
fn write_tag(&mut self, tag: u32) -> Result<()>
Writes a tag, which represents both the field number and the wire type
fn write_int32(&mut self, v: i32) -> Result<()>
Writes a int32
which is internally coded as a varint
fn write_int64(&mut self, v: i64) -> Result<()>
Writes a int64
which is internally coded as a varint
fn write_uint32(&mut self, v: u32) -> Result<()>
Writes a uint32
which is internally coded as a varint
fn write_uint64(&mut self, v: u64) -> Result<()>
Writes a uint64
which is internally coded as a varint
fn write_sint32(&mut self, v: i32) -> Result<()>
Writes a sint32
which is internally coded as a varint
fn write_sint64(&mut self, v: i64) -> Result<()>
Writes a sint64
which is internally coded as a varint
fn write_fixed64(&mut self, v: u64) -> Result<()>
Writes a fixed64
which is little endian coded u64
fn write_fixed32(&mut self, v: u32) -> Result<()>
Writes a fixed32
which is little endian coded u32
fn write_sfixed64(&mut self, v: i64) -> Result<()>
Writes a sfixed64
which is little endian coded i64
fn write_sfixed32(&mut self, v: i32) -> Result<()>
Writes a sfixed32
which is little endian coded i32
fn write_float(&mut self, v: f32) -> Result<()>
Writes a float
fn write_double(&mut self, v: f64) -> Result<()>
Writes a double
fn write_bool(&mut self, v: bool) -> Result<()>
Writes a bool
1 = true, 0 = false
fn write_enum(&mut self, v: i32) -> Result<()>
Writes an enum
converting it to a i32
first
fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>
Writes bytes
: length first then the chunk of data
fn write_string(&mut self, s: &str) -> Result<()>
Writes string
: length first then the chunk of data
fn write_packed_repeated_field<M, F, S>(&mut self,
v: &[M],
write: F,
size: &S)
-> Result<()> where F: FnMut(&mut Self, &M) -> Result<()>, S: Fn(&M) -> usize
v: &[M],
write: F,
size: &S)
-> Result<()> where F: FnMut(&mut Self, &M) -> Result<()>, S: Fn(&M) -> usize
Writes packed repeated field: length first then the chunk of data
fn write_packed_fixed_size<M>(&mut self,
v: &[M],
item_size: usize)
-> Result<()>
v: &[M],
item_size: usize)
-> Result<()>
Writes packed repeated field when we know the size of items
item_size
is internally used to compute the total length
As the length is fixed (and the same as rust internal representation, we can directly dump
all data at once
fn write_message<M: MessageWrite>(&mut self, m: &M) -> Result<()>
Writes a message which implements MessageWrite
fn write_int32_with_tag(&mut self, tag: u32, v: i32) -> Result<()>
Writes tag then int32
fn write_int64_with_tag(&mut self, tag: u32, v: i64) -> Result<()>
Writes tag then int64
fn write_uint32_with_tag(&mut self, tag: u32, v: u32) -> Result<()>
Writes tag then uint32
fn write_uint64_with_tag(&mut self, tag: u32, v: u64) -> Result<()>
Writes tag then uint64
fn write_sint32_with_tag(&mut self, tag: u32, v: i32) -> Result<()>
Writes tag then sint32
fn write_sint64_with_tag(&mut self, tag: u32, v: i64) -> Result<()>
Writes tag then sint64
fn write_fixed64_with_tag(&mut self, tag: u32, v: u64) -> Result<()>
Writes tag then fixed64
fn write_fixed32_with_tag(&mut self, tag: u32, v: u32) -> Result<()>
Writes tag then fixed32
fn write_sfixed64_with_tag(&mut self, tag: u32, v: i64) -> Result<()>
Writes tag then sfixed64
fn write_sfixed32_with_tag(&mut self, tag: u32, v: i32) -> Result<()>
Writes tag then sfixed32
fn write_float_with_tag(&mut self, tag: u32, v: f32) -> Result<()>
Writes tag then float
fn write_double_with_tag(&mut self, tag: u32, v: f64) -> Result<()>
Writes tag then double
fn write_bool_with_tag(&mut self, tag: u32, v: bool) -> Result<()>
Writes tag then bool
fn write_bytes_with_tag(&mut self, tag: u32, bytes: &[u8]) -> Result<()>
Writes tag then bytes
fn write_string_with_tag(&mut self, tag: u32, s: &str) -> Result<()>
Writes tag then string
fn write_packed_repeated_field_with_tag<M, F, S>(&mut self,
tag: u32,
v: &[M],
write: F,
size: &S)
-> Result<()> where F: FnMut(&mut Self, &M) -> Result<()>, S: Fn(&M) -> usize
tag: u32,
v: &[M],
write: F,
size: &S)
-> Result<()> where F: FnMut(&mut Self, &M) -> Result<()>, S: Fn(&M) -> usize
Writes tag then repeated field
If array is empty, then do nothing (do not even write the tag)
fn write_packed_fixed_size_with_tag<M>(&mut self,
tag: u32,
v: &[M],
item_size: usize)
-> Result<()>
tag: u32,
v: &[M],
item_size: usize)
-> Result<()>
Writes tag then repeated field with fixed length item size
If array is empty, then do nothing (do not even write the tag)
fn write_message_with_tag<M: MessageWrite>(&mut self,
tag: u32,
m: &M)
-> Result<()>
tag: u32,
m: &M)
-> Result<()>
Writes tag then message
fn write_enum_with_tag(&mut self, tag: u32, v: i32) -> Result<()>
Writes tag then enum