pub struct Writer<W: WriterBackend> { /* private fields */ }
Expand description

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: WriterBackend>(&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::borrow::Cow;
use quick_protobuf::Writer;

fn main() {
    // let mut r = File::create("...").expect("Cannot create file");
    // for the sake of example, we'll use a simpler struct which impl `Write`
    let mut r = Vec::new();
    let mut writer = Writer::new(&mut r);

    // 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");
}

Implementations

Creates a new ProtobufWriter

Writes a byte which is NOT internally coded as a varint

Writes a varint (compacted u64)

Writes a tag, which represents both the field number and the wire type

Writes a int32 which is internally coded as a varint

Writes a int64 which is internally coded as a varint

Writes a uint32 which is internally coded as a varint

Writes a uint64 which is internally coded as a varint

Writes a sint32 which is internally coded as a varint

Writes a sint64 which is internally coded as a varint

Writes a fixed64 which is little endian coded u64

Writes a fixed32 which is little endian coded u32

Writes a sfixed64 which is little endian coded i64

Writes a sfixed32 which is little endian coded i32

Writes a float

Writes a double

Writes a bool 1 = true, 0 = false

Writes an enum converting it to a i32 first

Writes bytes: length first then the chunk of data

Writes string: length first then the chunk of data

Writes packed repeated field: length first then the chunk of data

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

Writes a message which implements MessageWrite

Writes another item prefixed with tag

Writes tag then repeated field

If array is empty, then do nothing (do not even write the tag)

Writes tag then repeated field

If array is empty, then do nothing (do not even write the tag)

Writes tag then repeated field with fixed length item size

If array is empty, then do nothing (do not even write the tag)

Write entire map

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.