codec

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package codec is the single source of layout truth for fixed-size wire types: a generic, bounds-checked Encode/Decode pair (raw memcpy of the struct, little-endian host layout) and a Cursor writer for variable-size encoders.

Wire type contract

The memcpy format is only sound for POD structs with a frozen layout. Every type passed to Encode/Decode/Put must therefore:

  1. Contain no pointer, slice, map, string, chan, func, or interface field at any nesting depth (enforced by TestWireTypesArePOD).
  2. Keep its size and every field offset equal to the golden constants in guard_test.go. Any layout change fails CI until the constants are bumped deliberately (enforced by TestWireTypeLayoutGolden).

Style rule for wire structs

Declare layout, don't inherit it: order fields largest-first (8-byte fields, then 4-byte, then smaller) and spell out any padding explicitly as `_ [N]byte` so the byte image is what the source says, not what the compiler chose. New wire types must be added to the registry in guard_test.go before they are published on the bus.

Index

Constants

This section is empty.

Variables

View Source
var ErrBufferTooSmall = errors.New("buffer too small")

ErrBufferTooSmall is returned when the provided buffer cannot hold (Encode) or provide (Decode) the full wire representation of the value. The message deliberately carries no package prefix: it predates this package (event/command re-export the same value) and is pinned by the msglog JSONL golden in core/msgbus.

Functions

func AppendJSONBool

func AppendJSONBool(dst []byte, v bool) []byte

AppendJSONBool appends true or false.

func AppendJSONFloat

func AppendJSONFloat(dst []byte, f float64) []byte

AppendJSONFloat appends a float64 in shortest round-trip decimal form.

func AppendJSONInt

func AppendJSONInt(dst []byte, n int64) []byte

AppendJSONInt appends a signed integer in decimal.

func AppendJSONString

func AppendJSONString(dst []byte, s string) []byte

AppendJSONString appends a JSON-encoded string (including quotes) to dst. Alloc-free when dst has enough capacity.

func AppendJSONUint

func AppendJSONUint(dst []byte, n uint64) []byte

AppendJSONUint appends an unsigned integer in decimal.

func Decode

func Decode[T any](buf []byte) (T, error)

Decode copies buf into a new local value of T (bounds-checked memcpy). The returned value does not alias buf, so it stays valid after the buffer is released, and the copy works from unaligned source offsets.

func Encode

func Encode[T any](buf []byte, v *T) error

Encode writes the raw memory of *v into buf (bounds-checked memcpy). T must be a registered POD wire type (see guard_test.go): no pointers, slices, maps, strings, chans, funcs, or interfaces anywhere in the struct.

func Put

func Put[T any](c *Cursor, v *T)

Put appends the raw memory of the POD wire type *v at the cursor. Same layout contract as Encode.

func Size

func Size[T any]() int

Size returns the wire size of T: its in-memory size including padding. The layout is a frozen contract enforced by the guard tests in this package.

Types

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

Cursor is a bounds-checked sequential writer over a byte slice with a sticky error, replacing hand-maintained `pos += 8` offset arithmetic in variable-size encoders. All integer writes are little-endian (the wire byte order). After the last write, check Err() once.

func NewCursor

func NewCursor(buf []byte) Cursor

NewCursor returns a Cursor writing into buf starting at offset 0.

func (*Cursor) Err

func (c *Cursor) Err() error

Err returns the sticky error (nil, or ErrBufferTooSmall once any write did not fit). Writes after a failure are no-ops.

func (*Cursor) Pos

func (c *Cursor) Pos() int

Pos returns the number of bytes written so far.

func (*Cursor) PutBytes

func (c *Cursor) PutBytes(b []byte)

PutBytes copies b and advances the cursor.

func (*Cursor) PutString

func (c *Cursor) PutString(s string)

PutString copies s and advances the cursor.

func (*Cursor) PutUint32

func (c *Cursor) PutUint32(v uint32)

PutUint32 writes v little-endian and advances the cursor.

func (*Cursor) PutUint64

func (c *Cursor) PutUint64(v uint64)

PutUint64 writes v little-endian and advances the cursor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL