1
0
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:
Martin Huschenbett 2023-12-20 16:43:47 +01:00 committed by GitHub
parent 8e4bb60023
commit dce30e0e49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 6 deletions

View File

@ -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 }}

View File

@ -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)