mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-19 08:53:12 +02:00
Make protobuf exercise slightly easier (#1605)
As @djmitche suggested in https://github.com/google/comprehensive-rust/pull/1591#pullrequestreview-1782012715 it might be a little easier for the students if we give them a prefix of the code required for `parse_field`. That's exactly what we do here.
This commit is contained in:
parent
8e4bb60023
commit
dce30e0e49
@ -48,11 +48,9 @@ What remains for you is to implement the `parse_field` function and the
|
|||||||
|
|
||||||
|
|
||||||
{{#include exercise.rs:parse_field }}
|
{{#include exercise.rs:parse_field }}
|
||||||
// 1. Read and unpack the tag, which is a varint.
|
_ => todo!("Based on the wire type, build a Field, consuming as many bytes as necessary.")
|
||||||
// 2. Based on the wire type, build a Field, consuming as many bytes as
|
};
|
||||||
// necessary.
|
todo!("Return the field, and any un-consumed bytes.")
|
||||||
// 3. Return the field, and any un-consumed bytes.
|
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{{#include exercise.rs:parse_message }}
|
{{#include exercise.rs:parse_message }}
|
||||||
|
@ -135,10 +135,10 @@ fn unpack_tag(tag: u64) -> Result<(u64, WireType), Error> {
|
|||||||
// ANCHOR: parse_field
|
// ANCHOR: parse_field
|
||||||
/// Parse a field, returning the remaining bytes
|
/// Parse a field, returning the remaining bytes
|
||||||
fn parse_field(data: &[u8]) -> Result<(Field, &[u8]), Error> {
|
fn parse_field(data: &[u8]) -> Result<(Field, &[u8]), Error> {
|
||||||
// ANCHOR_END: parse_field
|
|
||||||
let (tag, remainder) = parse_varint(data)?;
|
let (tag, remainder) = parse_varint(data)?;
|
||||||
let (field_num, wire_type) = unpack_tag(tag)?;
|
let (field_num, wire_type) = unpack_tag(tag)?;
|
||||||
let (fieldvalue, remainder) = match wire_type {
|
let (fieldvalue, remainder) = match wire_type {
|
||||||
|
// ANCHOR_END: parse_field
|
||||||
WireType::Varint => {
|
WireType::Varint => {
|
||||||
let (value, remainder) = parse_varint(remainder)?;
|
let (value, remainder) = parse_varint(remainder)?;
|
||||||
(FieldValue::Varint(value), remainder)
|
(FieldValue::Varint(value), remainder)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user