postcard

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package postcard encodes and decodes the Rust postcard wire format.

Integers are varints, little-endian base-128, and signed integers are zigzagged first, except for u8 and i8: postcard writes those as one raw byte. Go values of kind uint8 and int8 follow that rule, so uint8(200) encodes as "c8" and int8(-2) as "fe".

The 8-bit encoding changed in this v0 release: uint8 and int8 were previously written as a varint and a zigzag varint, which did not match Rust. A uint8 below 128 encodes to the same single byte either way, so records holding only small u8 tags are unaffected; a uint8 of 128 or more, and an int8 of anything but zero, encoded differently before and cannot be read back by this version.

Decoding accepts only canonical varints: a padded encoding such as "ac8200" for 300 is rejected with ErrOverlongVarint, so distinct byte strings never decode to the same value.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTrailingBytes is returned when Unmarshal does not consume all input.
	ErrTrailingBytes = errors.New("postcard: trailing bytes")
	// ErrOverlongVarint is returned for a varint that is not canonically
	// encoded: one padded with continuation bytes past the shortest form, or
	// one whose value does not fit in 64 bits.
	ErrOverlongVarint = errors.New("postcard: overlong varint")
)

Functions

func Marshal

func Marshal(v any) ([]byte, error)

Marshal encodes v in postcard format.

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal decodes postcard data into v.

Types

type Decoder

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

Decoder incrementally decodes postcard values.

func NewDecoder

func NewDecoder(b []byte) *Decoder

NewDecoder returns a decoder reading b.

func (*Decoder) Bool

func (d *Decoder) Bool() (bool, error)

Bool decodes a postcard bool.

func (*Decoder) BytesValue

func (d *Decoder) BytesValue() ([]byte, error)

BytesValue decodes a postcard byte sequence.

func (*Decoder) Decode

func (d *Decoder) Decode(v any) error

Decode decodes the next postcard value into v.

func (*Decoder) Done

func (d *Decoder) Done() bool

Done reports whether d consumed all input.

func (*Decoder) Int

func (d *Decoder) Int() (int64, error)

Int decodes a postcard signed integer. Use Decoder.Int8 for a Rust i8, which is not zigzag-encoded.

func (*Decoder) Int8 added in v0.1.1

func (d *Decoder) Int8() (int8, error)

Int8 decodes a postcard i8: one raw byte holding the two's complement.

func (*Decoder) RawBytes

func (d *Decoder) RawBytes(n int) ([]byte, error)

RawBytes decodes n bytes without a length prefix.

func (*Decoder) String

func (d *Decoder) String() (string, error)

String decodes a postcard string.

func (*Decoder) Uint

func (d *Decoder) Uint() (uint64, error)

Uint decodes a postcard unsigned integer. Use Decoder.Uint8 for a Rust u8, which is not varint-encoded.

func (*Decoder) Uint8 added in v0.1.1

func (d *Decoder) Uint8() (uint8, error)

Uint8 decodes a postcard u8: one raw byte.

type DecoderFrom

type DecoderFrom interface {
	DecodePostcard(*Decoder) error
}

DecoderFrom is implemented by values that decode themselves from d.

type Encoder

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

Encoder incrementally encodes postcard values.

func (*Encoder) Bool

func (e *Encoder) Bool(v bool)

Bool appends v as a postcard bool.

func (*Encoder) Bytes

func (e *Encoder) Bytes() []byte

Bytes returns a copy of the encoded bytes.

func (*Encoder) BytesValue

func (e *Encoder) BytesValue(b []byte)

BytesValue appends b as a postcard byte sequence.

func (*Encoder) Encode

func (e *Encoder) Encode(v any) error

Encode appends v to e.

func (*Encoder) Int

func (e *Encoder) Int(v int64)

Int appends v as a postcard signed integer. Use Encoder.Int8 for a Rust i8, which is not zigzag-encoded.

func (*Encoder) Int8 added in v0.1.1

func (e *Encoder) Int8(v int8)

Int8 appends v as a postcard i8: one raw byte holding the two's complement, not a zigzag varint.

func (*Encoder) RawBytes

func (e *Encoder) RawBytes(b []byte)

RawBytes appends b without a length prefix.

func (*Encoder) String

func (e *Encoder) String(s string)

String appends s as a postcard string.

func (*Encoder) Uint

func (e *Encoder) Uint(v uint64)

Uint appends v as a postcard unsigned integer. Use Encoder.Uint8 for a Rust u8, which is not varint-encoded.

func (*Encoder) Uint8 added in v0.1.1

func (e *Encoder) Uint8(v uint8)

Uint8 appends v as a postcard u8: one raw byte, not a varint.

type EncoderTo

type EncoderTo interface {
	EncodePostcard(*Encoder) error
}

EncoderTo is implemented by values that encode themselves to e.

type Marshaler

type Marshaler interface {
	MarshalPostcard() ([]byte, error)
}

Marshaler is implemented by values with a custom postcard representation.

Jump to

Keyboard shortcuts

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