-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* deserialize helpers * use into_known to impl OpenProtoEnum * add serialize special case * Special case for compute_size as well * Use if let instead of for for optional fields * Only generate compute_grpc_slices_size for types that use grpc_slices
- Loading branch information
Showing
14 changed files
with
1,103 additions
and
4,002 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::io; | ||
|
||
use crate::{ | ||
ensure_split, | ||
ensure_wire_format, | ||
varint, | ||
wire_format, | ||
Message, | ||
PbBufferReader, | ||
PbBufferWriter, | ||
}; | ||
|
||
pub fn deserialize_packed<B: PbBufferReader, T: Message>( | ||
buf: &mut B, | ||
typ: wire_format::Type, | ||
expected_wire_format: wire_format::Type, | ||
msg_name: &'static str, | ||
field_number: usize, | ||
out: &mut Vec<T>, | ||
) -> io::Result<()> { | ||
match typ { | ||
wire_format::Type::LengthDelimited => { | ||
let len = varint::ensure_read(buf)?; | ||
let mut vals = ensure_split(buf, len as usize)?; | ||
while vals.has_remaining() { | ||
let mut val: T = Default::default(); | ||
val.deserialize(&mut vals)?; | ||
out.push(val); | ||
} | ||
}, | ||
_ => { | ||
ensure_wire_format(typ, expected_wire_format, msg_name, field_number)?; | ||
let mut val: T = Default::default(); | ||
val.deserialize(buf)?; | ||
out.push(val); | ||
}, | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn deserialize_length_delimited<B: PbBufferReader, T: Message>( | ||
buf: &mut B, | ||
typ: wire_format::Type, | ||
msg_name: &'static str, | ||
field_number: usize, | ||
) -> io::Result<T> { | ||
ensure_wire_format(typ, wire_format::Type::LengthDelimited, msg_name, field_number)?; | ||
let len = varint::ensure_read(buf)?; | ||
let mut next = ensure_split(buf, len as usize)?; | ||
let mut val: T = Default::default(); | ||
val.deserialize(&mut next)?; | ||
Ok(val) | ||
} | ||
|
||
pub fn deserialize_known_length<B: PbBufferReader, T: Message>( | ||
buf: &mut B, | ||
typ: wire_format::Type, | ||
expected_wire_format: wire_format::Type, | ||
msg_name: &'static str, | ||
field_number: usize, | ||
) -> io::Result<T> { | ||
ensure_wire_format(typ, expected_wire_format, msg_name, field_number)?; | ||
let mut val: T = Default::default(); | ||
val.deserialize(buf)?; | ||
Ok(val) | ||
} | ||
|
||
pub fn serialize_scalar<W: PbBufferWriter, T: Message>( | ||
w: &mut W, | ||
val: &T, | ||
field_number: u32, | ||
wire_format: wire_format::Type, | ||
) -> io::Result<()> { | ||
if *val != T::default() { | ||
wire_format::write(field_number, wire_format, w)?; | ||
if let wire_format::Type::LengthDelimited = wire_format { | ||
let l = val.compute_size(); | ||
varint::write(l as u64, w)?; | ||
} | ||
val.serialize(w)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn compute_size_scalar<T: Message>(val: &T, field_number: u32, wire_format: wire_format::Type) -> usize { | ||
let mut size = 0; | ||
if *val != T::default() { | ||
size += wire_format::serialized_length(field_number); | ||
let l = val.compute_size(); | ||
if let wire_format::Type::LengthDelimited = wire_format { | ||
size += varint::serialized_length(l as u64); | ||
} | ||
size += l; | ||
} | ||
size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ use bytes::buf::{ | |
}; | ||
|
||
pub mod erased; | ||
pub mod helpers; | ||
pub mod varint; | ||
pub mod wire_format; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.