Esc

Entry_Codec

structextensionsince 0.36.1Source
  • macOS: Entry_Codec works on macOS
  • Linux: Entry_Codec works on Linux
  • Windows: Entry_Codec works on Windows
  • Wasm: Entry_Codec works on Wasm

Declaration

Entry_Codec :: struct(K: Type, V: Type, key_kind: Scalar, value_kind: Scalar) {
    key: K;    value: V = #run entry_default(V);    invalid_enum: bool;    proto_storage: *Storage_Block;    proto_has_required :: false;

    proto_check :: (entry: *Entry_Codec, r: *Reader) -> bool {
        #if value_kind == .MESSAGE return check_message(entry.value, r);
        else return true;
    }

    proto_size :: (entry: *Entry_Codec, cache: *[..] u32) -> s64 {
        slot := cache.count;

        array_add(cache, 0);
        n := size_field(entry.key, key_kind, 8, cache, true) + size_field(entry.value, value_kind, 16 + cast(s64) scalar_wire(value_kind), cache, true);

        cache.data[slot] = cast(u32) min(n, 2147483648);

        return min(n, 2147483648);
    }

    proto_encode :: (entry: *Entry_Codec, w: *Writer) {
        encode_field(w, entry.key, key_kind, 8 + cast(s64) scalar_wire(key_kind), true);
        encode_field(w, entry.value, value_kind, 16 + cast(s64) scalar_wire(value_kind), true);
    }

    proto_free :: (entry: *Entry_Codec) {
        free_field(*entry.key, key_kind);
        free_field(*entry.value, value_kind);
    }

    proto_merge :: (entry: *Entry_Codec, r: *Reader) -> bool {
        suppressed := r.suppress_enum_unknown;

        #if value_kind == .CLOSED_ENUM r.suppress_enum_unknown = true;
        defer r.suppress_enum_unknown = suppressed;

        while remaining(r) > 0 {
            tag := read_tag(r);

            if r.error.kind != .NONE return false;

            if tag == 8 + scalar_wire(key_kind) {
                if !read_scalar(r, *entry.key, key_kind) return field_error(r, "key");
            } else if tag == 16 + scalar_wire(value_kind) {
                #if value_kind == .MESSAGE {
                    if !entry.value entry.value = new_message(r, type_of(entry.value.*));
                    if !read_message(r, entry.value) return field_error(r, "value");
                } else {
                    if !read_scalar(r, *entry.value, value_kind) return field_error(r, "value");
                    #if value_kind == .CLOSED_ENUM if r.enum_rejected entry.invalid_enum = true;
                }
            } else if !skip(r, tag) return false;
        }

        return true;
    }
}

Shared map-entry codec; it never stores unknown fields or emits synthetic schema types.

See also