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_core::{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(),
_ => unreachable!(),
}
}
}
Or let the derive macro do it for you:
ⓘ
use pbf_derive::ProtoRead;
#[derive(ProtoRead)]
struct BlobHeader {
#[pbf(tag = 1)]
r#type: String,
indexdata: Vec<u8>,
datasize: i32,
}
Required Methods§
Sourcefn read(&mut self, tag: u64, pbf: &mut Protobuf)
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_core::{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(),
_ => unreachable!(),
}
}
}
Or let the derive macro do it for you:
ⓘ
use pbf_derive::ProtoRead;
#[derive(ProtoRead)]
struct BlobHeader {
#[pbf(tag = 1)]
r#type: String,
indexdata: Vec<u8>,
datasize: i32,
}