When generating zero-copy view types for proto2 schemas, required scalar/string fields are emitted as bare defaulted values in the generated *View<'a> type. This makes it impossible for users to distinguish a field that was absent on the wire from a field that was explicitly present with its default value.
This matters for proto2 schemas where missing required fields should be rejected, or where downstream code needs to validate the input according to the schema.
syntax = "proto2";
package example;
message Tile {
message Layer {
required uint32 version = 15 [default = 1];
required string name = 1;
optional uint32 extent = 5 [default = 4096];
}
repeated Layer layers = 3;
}
The generated view shape is effectively:
pub struct LayerView<'a> {
pub version: u32,
pub name: &'a str,
pub extent: Option<u32>,
// ...
}
After LayerView::decode_view(...), these cases are indistinguishable:
version missing vs version explicitly encoded as 1
name missing vs name explicitly encoded as ""
Would it be possible to either:
- making
decode_view reject messages missing proto2 required fields (possibly with a strict codegen flag)
- exposing
required-field presence in generated view types, e.g. via generated has_* accessors or internal presence bits?
When generating zero-copy view types for proto2 schemas,
requiredscalar/string fields are emitted as bare defaulted values in the generated*View<'a>type. This makes it impossible for users to distinguish a field that was absent on the wire from a field that was explicitly present with its default value.This matters for proto2 schemas where missing required fields should be rejected, or where downstream code needs to validate the input according to the schema.
The generated view shape is effectively:
After
LayerView::decode_view(...), these cases are indistinguishable:versionmissing vsversionexplicitly encoded as 1namemissing vsnameexplicitly encoded as""Would it be possible to either:
decode_viewreject messages missingproto2required fields (possibly with astrictcodegen flag)required-fieldpresence in generated view types, e.g. via generatedhas_*accessors or internal presence bits?