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 ¶
- Variables
- func Marshal(v any) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- type Decoder
- func (d *Decoder) Bool() (bool, error)
- func (d *Decoder) BytesValue() ([]byte, error)
- func (d *Decoder) Decode(v any) error
- func (d *Decoder) Done() bool
- func (d *Decoder) Int() (int64, error)
- func (d *Decoder) Int8() (int8, error)
- func (d *Decoder) RawBytes(n int) ([]byte, error)
- func (d *Decoder) String() (string, error)
- func (d *Decoder) Uint() (uint64, error)
- func (d *Decoder) Uint8() (uint8, error)
- type DecoderFrom
- type Encoder
- func (e *Encoder) Bool(v bool)
- func (e *Encoder) Bytes() []byte
- func (e *Encoder) BytesValue(b []byte)
- func (e *Encoder) Encode(v any) error
- func (e *Encoder) Int(v int64)
- func (e *Encoder) Int8(v int8)
- func (e *Encoder) RawBytes(b []byte)
- func (e *Encoder) String(s string)
- func (e *Encoder) Uint(v uint64)
- func (e *Encoder) Uint8(v uint8)
- type EncoderTo
- type Marshaler
Constants ¶
This section is empty.
Variables ¶
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 ¶
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder incrementally decodes postcard values.
func (*Decoder) BytesValue ¶
BytesValue decodes a postcard byte sequence.
func (*Decoder) Int ¶
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
Int8 decodes a postcard i8: one raw byte holding the two's complement.
func (*Decoder) Uint ¶
Uint decodes a postcard unsigned integer. Use Decoder.Uint8 for a Rust u8, which is not varint-encoded.
type DecoderFrom ¶
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) BytesValue ¶
BytesValue appends b as a postcard byte sequence.
func (*Encoder) Int ¶
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
Int8 appends v as a postcard i8: one raw byte holding the two's complement, not a zigzag varint.
func (*Encoder) Uint ¶
Uint appends v as a postcard unsigned integer. Use Encoder.Uint8 for a Rust u8, which is not varint-encoded.