Documentation ¶
Index ¶
- func Decode(value Decodeable, encoded []byte)
- func Encode(value Encodeable) []byte
- type Decodeable
- type Decoder
- func (pd Decoder) DecodeBool() bool
- func (pd Decoder) DecodeByteSlice() []byte
- func (pd Decoder) DecodeCollection(setSize func(int), decodeElem func(int))
- func (pd Decoder) DecodeInt(bytes uint) int64
- func (pd Decoder) DecodeOption(hasValue *bool, valuePointer Decodeable)
- func (pd Decoder) DecodeString() string
- func (pd Decoder) DecodeUint(bytes uint) uint64
- func (pd Decoder) DecodeUintCompact() uint64
- func (pd Decoder) Read(bytes []byte)
- func (pd Decoder) ReadOneByte() byte
- type Encodeable
- type Encoder
- func (pe Encoder) EncodeBool(b bool)
- func (pe Encoder) EncodeByteSlice(value []byte)
- func (pe Encoder) EncodeCollection(length int, encodeElem func(int))
- func (pe Encoder) EncodeInt(value int64, bytes int)
- func (pe Encoder) EncodeOption(hasValue bool, value Encodeable)
- func (pe Encoder) EncodeString(value string)
- func (pe Encoder) EncodeUint(value uint64, bytes int)
- func (pe Encoder) EncodeUintCompact(v uint64)
- func (pe Encoder) PushByte(b byte)
- func (pe Encoder) Write(bytes []byte)
- type OptionBool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
func Decode(value Decodeable, encoded []byte)
func Encode ¶
func Encode(value Encodeable) []byte
Types ¶
type Decodeable ¶
type Decodeable interface { // ParityDecode populates this structure from a stream (overwriting the current contents), return false on failure ParityDecode(decoder Decoder) }
Decodeable is an interface that defines a custom encoding rules for a data type. Should be defined for pointers to structs. See OptionBool for an example implementation.
type Decoder ¶
Decoder - a wraper around a Reader that allows decoding data items from a stream. Unlike Rust implementations, decoder methods do not return success state, but just panic on error. Since decoding failue is an "unexpected" error, this approach should be justified.
func (Decoder) DecodeBool ¶
func (Decoder) DecodeByteSlice ¶
func (Decoder) DecodeCollection ¶
See []int16 in tests as an example
func (Decoder) DecodeOption ¶
func (pd Decoder) DecodeOption(hasValue *bool, valuePointer Decodeable)
DecodeOption decodes a optionally available value into a boolean presence field and a value.
func (Decoder) DecodeString ¶
func (Decoder) DecodeUint ¶
func (Decoder) DecodeUintCompact ¶
DecodeUintCompact decodes a compact-encoded integer. See EncodeUintCompact method.
func (Decoder) Read ¶
Read reads bytes from a stream into a buffer and panics if cannot read the required number of bytes.
func (Decoder) ReadOneByte ¶
ReadOneByte reads a next byte from the stream. Named so to avoid a linter warning about a clash with io.ByteReader.ReadByte
type Encodeable ¶
type Encodeable interface { // ParityEncode encodes and write this structure into a stream ParityEncode(encoder Encoder) }
Encodeable is an interface that defines a custom encoding rules for a data type. Should be defined for structs (not pointers to them). See OptionBool for an example implementation.
type Encoder ¶
Encoder is a wrapper around a Writer that allows encoding data items to a stream.
func (Encoder) EncodeBool ¶
func (Encoder) EncodeByteSlice ¶
func (Encoder) EncodeCollection ¶
See []int16 in tests as an example
func (Encoder) EncodeOption ¶
func (pe Encoder) EncodeOption(hasValue bool, value Encodeable)
EncodeOption stores optionally present value to the stream.
func (Encoder) EncodeString ¶
func (Encoder) EncodeUint ¶
func (Encoder) EncodeUintCompact ¶
EncodeUintCompact writes an unsigned integer to the stream using the compact encoding. A typical usage is storing the length of a collection. Definition of compact encoding: 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
xx xx xx 00 (0 ... 2**6 - 1) (u8) yL yL yL 01 / yH yH yH yL (2**6 ... 2**14 - 1) (u8, u16) low LH high zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM (2**14 ... 2**30 - 1) (u16, u32) low LMMH high nn nn nn 11 [ / zz zz zz zz ]{4 + n} (2**30 ... 2**536 - 1) (u32, u64, u128, U256, U512, U520) straight LE-encoded
Rust implementation: see impl<'a> Encode for CompactRef<'a, u64>
type OptionBool ¶
type OptionBool struct {
// contains filtered or unexported fields
}
OptionBool is a structure that can store a boolean or a missing value. Note that encoding rules are slightly different from other "Option" fields.
func NewOptionBool ¶
func NewOptionBool(value bool) OptionBool
NewOptionBool creates an OptionBool with a value.
func NewOptionBoolEmpty ¶
func NewOptionBoolEmpty() OptionBool
NewOptionBoolEmpty creates an OptionBool without a value.
func (*OptionBool) ParityDecode ¶
func (o *OptionBool) ParityDecode(decoder Decoder)
ParityDecode implements decoding for OptionBool as per Rust implementation.
func (OptionBool) ParityEncode ¶
func (o OptionBool) ParityEncode(encoder Encoder)
ParityEncode implements encoding for OptionBool as per Rust implementation.