Documentation ¶
Index ¶
- Variables
- func DecInt64(b []byte) int64
- func DecUint64(b []byte) uint64
- func EncInt64(i int64) (b []byte)
- func EncUint64(i uint64) (b []byte)
- func Marshal(v interface{}) []byte
- func MarshalAll(v ...interface{}) (b []byte)
- func ReadObject(r io.Reader, obj interface{}, maxLen uint64) error
- func ReadPrefix(r io.Reader, maxLen uint64) ([]byte, error)
- func Unmarshal(b []byte, v interface{}) (err error)
- func WriteObject(w io.Writer, obj interface{}) error
- func WritePrefix(w io.Writer, data []byte) error
- type ReadWriter
- type Reader
- type SiaMarshaler
- type SiaUnmarshaler
- type Writer
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoData = errors.New("no data")
)
Functions ¶
func DecInt64 ¶
DecInt64 decodes a slice of 8 bytes into an int64. If len(b) < 8, the slice is padded with zeros.
func DecUint64 ¶
DecUint64 decodes a slice of 8 bytes into a uint64. If len(b) < 8, the slice is padded with zeros.
func Marshal ¶
func Marshal(v interface{}) []byte
Marshal encodes a value as a byte slice. The encoding rules are as follows:
Most types are encoded as their binary representation.
Integers are little-endian, and are always encoded as 8 bytes, i.e. their int64 or uint64 equivalent.
Booleans are encoded as one byte, either zero (false) or non-zero (true).
Nil pointers are represented by a zero.
Valid pointers are prefaced by a non-zero, followed by the dereferenced value.
Variable-length types, such as strings and slices, are prefaced by 8 bytes containing their length.
Slices and structs are simply the concatenation of their encoded elements. Byte slices are not subject to the 8-byte integer rule; they are encoded as their literal representation, one byte per byte. The ordering of struct fields is determined by their type definition. For example:
type foo struct { S string I int } Marshal(foo{"bar", 3}) = append(Marshal("bar"), Marshal(3)...)
func MarshalAll ¶
func MarshalAll(v ...interface{}) (b []byte)
MarshalAll marshals all of its inputs and returns their concatenation.
func ReadObject ¶
ReadObject reads and decodes a length-prefixed and marshalled object.
func ReadPrefix ¶
ReadPrefix reads an 8-byte length prefixes, followed by the number of bytes specified in the prefix. The operation is aborted if the prefix exceeds a specified maximum length.
func Unmarshal ¶
Unmarshal decodes a byte slice into the provided interface. The interface must be a pointer. The decoding rules are the inverse of those described under Marshal.
func WriteObject ¶
WriteObject encodes an object and prepends it with a 4-byte length before writing it.
Types ¶
type ReadWriter ¶ added in v0.3.0
A ReadWriter can both read and write objects.
type Reader ¶ added in v0.3.0
A Reader can decode an object from its input. maxLen specifies the maximum length of the object being decoded.
type SiaMarshaler ¶
type SiaMarshaler interface {
MarshalSia() []byte
}
A Marshaler can be encoded as a byte slice. Marshaler and Unmarshaler are separate interfaces because Unmarshaler must have a pointer receiver, while Marshaler does not.
type SiaUnmarshaler ¶
An Unmarshaler can be decoded from a byte slice. UnmarshalSia may be passed a byte slice containing more than one encoded type. It should return the number of bytes used to decode itself. UnmarshalSia can panic; the panic will be caught by Unmarshal. Alternatively, if UnmarshalSia returns a negative value, Unmarshal will always fail.