Files
go/cmd/zapgen/testdata/pong_zap.hpp.golden
zeekay d9bcc8711f The generator can say what the chains actually put on the wire
A list's encoding was one thing for every element type: a run of
{length, payload} pairs. The Lux chains write four different runs — a
run of numbers, a run of fixed-width byte records, a run of struct
payloads back to back, and a run of relative pointers to payloads
written elsewhere — and none of them was that one. So both chains that
have a C++ port wrote their own accessors by hand, and the generator
could not have replaced them.

The shape is now derived from the element type rather than fixed. A
number is a run of numbers at its own width; bytes_fixed[N] is those
runs back to back; a struct with no tail is its payload back to back at
its own width; a struct WITH a tail cannot sit in a run at all, because
its tail has nowhere to go between two neighbours, so it becomes a
pointer. The schema never spells any of this, which is what stops two
authors spelling one list two ways. bytes and text have no width, so a
run of them is refused rather than invented.

Order is the wire, too. Every payload a struct points at is written
before the struct's own object, because SetList needs an offset that
already exists — the order the reference has always written in, and the
one the emitted builder had backwards. An empty list now writes nothing
at all: the alignment pad that a start-then-discard would leave behind
moves every byte after it.

Three entry points per struct, because there are three places a struct
sits: New for a message of its own, Append for an object inside somebody
else's message, Encode for one element of an inline run. A nested struct
field is the nested type's own Input, not a pre-built buffer the caller
hands over unchecked.

Readers take the stride the schema knows, so a lying length word is
refused once instead of at every element, and a list of records answers
with the record's type instead of an offset the caller has to know.

Both backends emit version 2. That is the header every vector in the
chain corpus opens with, and the one the hardened runtime writes; a
generated builder that wrote version 1 wrote a header no chain has.

The Go runtime gains the pointer list it was missing — AddObjectPtr,
ObjectPtr and the strided read — so both backends can express the same
wire rather than one of them refusing.

Proven: one schema, both backends, byte-identical output over all four
list shapes, a bytes tail and a pointer list.
2026-09-06 09:09:54 -07:00

89 lines
2.8 KiB
Plaintext

// Code generated by zapgen; DO NOT EDIT.
// source: echo.zap
// SPDX-License-Identifier: BSD-3-Clause-Eco
#pragma once
#include <array>
#include <cstdint>
#include <cstring>
#include <expected>
#include <span>
#include <vector>
#include <zap/zap.hpp>
namespace echo {
class Pong;
// Field byte offsets for Pong, and the size of its fixed section.
inline constexpr std::int64_t kPongSeqOff = 0;
inline constexpr std::int64_t kPongSize = 8;
// Pong is a zero-copy view into a ZAP-encoded Pong message. It borrows the
// bytes it was built over, which must outlive it, and copies no field.
class Pong {
public:
Pong() = default;
explicit Pong(zap::Object o) : o_(o) {}
bool is_null() const { return o_.is_null(); }
zap::Object object() const { return o_; }
std::uint64_t Seq() const { return o_.u64(kPongSeqOff); }
private:
zap::Object o_;
};
// WrapPong parses b and returns a typed view over it, or says why b is not a
// message. The view borrows b.
inline std::expected<Pong, zap::Error> WrapPong(std::span<const std::uint8_t> b) {
auto m = zap::Message::parse(b);
if (!m) return std::unexpected(m.error());
return Pong(m->root());
}
// The value records, declared before they are defined.
struct PongInput;
// PongInput collects the field values for NewPong. A borrowed field is a view:
// it must outlive the call, not the buffer the call returns.
struct PongInput {
std::uint64_t Seq{};
};
// The write side, declared before it is defined so a struct may point at
// one that comes later in the schema.
inline std::vector<std::uint8_t> NewPong(const PongInput& in);
inline std::int64_t AppendPong(zap::Builder& b, const PongInput& in);
inline std::array<std::uint8_t, static_cast<std::size_t>(kPongSize)> EncodePong(const PongInput& in);
// EncodePong lays one Pong payload out as an inline list element:
// the fixed section alone, unaligned, exactly as wide as the stride.
inline std::array<std::uint8_t, static_cast<std::size_t>(kPongSize)> EncodePong(const PongInput& in) {
std::array<std::uint8_t, static_cast<std::size_t>(kPongSize)> e{};
zap::store_u64(e.data() + kPongSeqOff, in.Seq);
return e;
}
// AppendPong writes one Pong object into b — every payload it points at first,
// then the object itself — and returns the object's offset.
inline std::int64_t AppendPong(zap::Builder& b, const PongInput& in) {
auto ob = b.start_object(kPongSize);
ob.set_u64(kPongSeqOff, in.Seq);
return ob.finish();
}
// NewPong writes a ZAP-encoded Pong message into a fresh buffer and returns it.
inline std::vector<std::uint8_t> NewPong(const PongInput& in) {
zap::Builder b(256, zap::kVersion2);
auto ob = b.start_object(kPongSize);
ob.set_u64(kPongSeqOff, in.Seq);
ob.finish_as_root();
return b.finish();
}
} // namespace echo