[][src]Struct quick_protobuf::writer::Writer

pub struct Writer<W: WriterBackend> { /* 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: 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

impl<W: WriterBackend> Writer<W>[src]

pub fn new(w: W) -> Writer<W>[src]

Creates a new ProtobufWriter

pub fn write_u8(&mut self, byte: u8) -> Result<()>[src]

Writes a byte which is NOT internally coded as a varint

pub fn write_varint(&mut self, v: u64) -> Result<()>[src]

Writes a varint (compacted u64)

pub fn write_tag(&mut self, tag: u32) -> Result<()>[src]

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

pub fn write_int32(&mut self, v: i32) -> Result<()>[src]

Writes a int32 which is internally coded as a varint

pub fn write_int64(&mut self, v: i64) -> Result<()>[src]

Writes a int64 which is internally coded as a varint

pub fn write_uint32(&mut self, v: u32) -> Result<()>[src]

Writes a uint32 which is internally coded as a varint

pub fn write_uint64(&mut self, v: u64) -> Result<()>[src]

Writes a uint64 which is internally coded as a varint

pub fn write_sint32(&mut self, v: i32) -> Result<()>[src]

Writes a sint32 which is internally coded as a varint

pub fn write_sint64(&mut self, v: i64) -> Result<()>[src]

Writes a sint64 which is internally coded as a varint

pub fn write_fixed64(&mut self, v: u64) -> Result<()>[src]

Writes a fixed64 which is little endian coded u64

pub fn write_fixed32(&mut self, v: u32) -> Result<()>[src]

Writes a fixed32 which is little endian coded u32

pub fn write_sfixed64(&mut self, v: i64) -> Result<()>[src]

Writes a sfixed64 which is little endian coded i64

pub fn write_sfixed32(&mut self, v: i32) -> Result<()>[src]

Writes a sfixed32 which is little endian coded i32

pub fn write_float(&mut self, v: f32) -> Result<()>[src]

Writes a float

pub fn write_double(&mut self, v: f64) -> Result<()>[src]

Writes a double

pub fn write_bool(&mut self, v: bool) -> Result<()>[src]

Writes a bool 1 = true, 0 = false

pub fn write_enum(&mut self, v: i32) -> Result<()>[src]

Writes an enum converting it to a i32 first

pub fn write_bytes(&mut self, bytes: &[u8]) -> Result<()>[src]

Writes bytes: length first then the chunk of data

pub fn write_string(&mut self, s: &str) -> Result<()>[src]

Writes string: length first then the chunk of data

pub fn write_packed<M, F, S>(
    &mut self,
    v: &[M],
    write: F,
    size: &S
) -> Result<()> where
    F: FnMut(&mut Self, &M) -> Result<()>,
    S: Fn(&M) -> usize
[src]

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

pub fn write_packed_fixed<M>(&mut self, v: &[M]) -> Result<()>[src]

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

pub fn write_message<M: MessageWrite>(&mut self, m: &M) -> Result<()>[src]

Writes a message which implements MessageWrite

pub fn write_with_tag<F>(&mut self, tag: u32, write: F) -> Result<()> where
    F: FnMut(&mut Self) -> Result<()>, 
[src]

Writes another item prefixed with tag

pub fn write_packed_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
[src]

Writes tag then repeated field

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

pub fn write_packed_fixed_with_tag<M>(
    &mut self,
    tag: u32,
    v: &[M]
) -> Result<()>
[src]

Writes tag then repeated field

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

pub fn write_packed_fixed_size_with_tag<M>(
    &mut self,
    tag: u32,
    v: &[M],
    item_size: usize
) -> Result<()>
[src]

Writes tag then repeated field with fixed length item size

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

pub fn write_map<FK, FV>(
    &mut self,
    size: usize,
    tag_key: u32,
    write_key: FK,
    tag_val: u32,
    write_val: FV
) -> Result<()> where
    FK: FnMut(&mut Self) -> Result<()>,
    FV: FnMut(&mut Self) -> Result<()>, 
[src]

Write entire map

Auto Trait Implementations

impl<W> RefUnwindSafe for Writer<W> where
    W: RefUnwindSafe

impl<W> Send for Writer<W> where
    W: Send

impl<W> Sync for Writer<W> where
    W: Sync

impl<W> Unpin for Writer<W> where
    W: Unpin

impl<W> UnwindSafe for Writer<W> where
    W: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.