Trait pbf::ProtoRead

source ·
pub trait ProtoRead {
    // Required method
    fn read(&mut self, tag: u64, pbf: &mut Protobuf);
}
Expand description

The ProtoRead trait is used to read a protobuf message. This crate forces the user to implement this trait in order to read a protobuf message.

§Example

Using OSM File Format BlobHeader as an example:

message BlobHeader {
    required string type = 1;
    optional bytes indexdata = 2;
    required int32 datasize = 3;
}

The user would implement the ProtoRead trait for the BlobHeader struct.

use pbf::{ProtoRead, Protobuf};

struct BlobHeader {
    r#type: String,
    indexdata: Vec<u8>,
    datasize: i32,
}
impl ProtoRead for BlobHeader {
   fn read(&mut self, tag: u64, pbf: &mut Protobuf) {
      match tag {
         1 => self.r#type = pbf.read_string(),
         2 => self.indexdata = pbf.read_bytes(),
         3 => self.datasize = pbf.read_varint::<i32>(),
         _ => unreachable!(),
      }
   }
}

Required Methods§

source

fn read(&mut self, tag: u64, pbf: &mut Protobuf)

The read method is used to read a field from a protobuf message. The tag parameter is used to determine which field to read into the struct. The pbf parameter is used to read the data in the appropriate format.

§Example

Using OSM File Format BlobHeader as an example:

message BlobHeader {
    required string type = 1;
    optional bytes indexdata = 2;
    required int32 datasize = 3;
}

An example read implementation for a BlobHeader struct:

use pbf::{ProtoRead, Protobuf};

struct BlobHeader {
    r#type: String,
    indexdata: Vec<u8>,
    datasize: i32,
}
impl ProtoRead for BlobHeader {
   fn read(&mut self, tag: u64, pbf: &mut Protobuf) {
      match tag {
         1 => self.r#type = pbf.read_string(),
         2 => self.indexdata = pbf.read_bytes(),
         3 => self.datasize = pbf.read_varint::<i32>(),
         _ => unreachable!(),
      }
   }
}

Implementors§