Documentation ¶
Index ¶
- Constants
- Variables
- func FromBinary(v interface{}, buf []byte) error
- func Marshal(v interface{}, buf []byte, rem int) ([]byte, int, error)
- func MarshalBool(x bool, buf []byte, rem int) ([]byte, int, error)
- func MarshalBytes(v []byte, buf []byte, rem int) ([]byte, int, error)
- func MarshalF32(x float32, buf []byte, rem int) ([]byte, int, error)
- func MarshalF64(x float64, buf []byte, rem int) ([]byte, int, error)
- func MarshalI16(x int16, buf []byte, rem int) ([]byte, int, error)
- func MarshalI32(x int32, buf []byte, rem int) ([]byte, int, error)
- func MarshalI64(x int64, buf []byte, rem int) ([]byte, int, error)
- func MarshalI8(x int8, buf []byte, rem int) ([]byte, int, error)
- func MarshalLen(l uint32, buf []byte, rem int) ([]byte, int, error)
- func MarshalString(v string, buf []byte, rem int) ([]byte, int, error)
- func MarshalU16(x uint16, buf []byte, rem int) ([]byte, int, error)
- func MarshalU32(x uint32, buf []byte, rem int) ([]byte, int, error)
- func MarshalU64(x uint64, buf []byte, rem int) ([]byte, int, error)
- func MarshalU8(x uint8, buf []byte, rem int) ([]byte, int, error)
- func NewErrUnsupportedMarshalType(v interface{}) error
- func NewErrUnsupportedUnmarshalType(v interface{}) error
- func SizeHint(v interface{}) int
- func SizeHintBytes(v []byte) int
- func SizeHintString(v string) int
- func ToBinary(v interface{}) ([]byte, error)
- func Unmarshal(v interface{}, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalBool(x *bool, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalBytes(v *[]byte, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalF32(x *float32, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalF64(x *float64, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalI16(x *int16, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalI32(x *int32, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalI64(x *int64, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalI8(x *int8, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalLen(dst *uint32, elemSize int, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalString(v *string, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalU16(x *uint16, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalU32(x *uint32, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalU64(x *uint64, buf []byte, rem int) ([]byte, int, error)
- func UnmarshalU8(x *uint8, buf []byte, rem int) ([]byte, int, error)
- type ErrUnsupportedMarshalType
- type ErrUnsupportedUnmarshalType
- type MarshalUnmarshaler
- type Marshaler
- type SizeHinter
- type Unmarshaler
Constants ¶
const ( // SizeHintF32 is the number of bytes required to represent a float32 value // in binary. SizeHintF32 = 4 // SizeHintF64 is the number of bytes required to represent a float64 value // in binary. SizeHintF64 = 8 )
const ( // SizeHintU8 is the number of bytes required to represent a uint8 value in // binary. SizeHintU8 = 1 // SizeHintU16 is the number of bytes required to represent a uint16 value // in binary. SizeHintU16 = 2 // SizeHintU32 is the number of bytes required to represent a uint32 value // in binary. SizeHintU32 = 4 // SizeHintU64 is the number of bytes required to represent a uint64 value // in binary. SizeHintU64 = 8 // SizeHintI8 is the number of bytes required to represent a int8 value in // binary. SizeHintI8 = 1 // SizeHintI16 is the number of bytes required to represent a int16 value in // binary. SizeHintI16 = 2 // SizeHintI32 is the number of bytes required to represent a int32 value in // binary. SizeHintI32 = 4 // SizeHintI64 is the number of bytes required to represent a int64 value in // binary. SizeHintI64 = 8 )
const MaxBytes = int(64 * 1024 * 1024)
MaxBytes is set to 64 MB by default.
const ( // SizeHintBool is the number of bytes required to represent a boolean value // in binary. SizeHintBool = 1 )
Variables ¶
var ErrLengthOverflow = errors.New("max bytes exceeded")
ErrLengthOverflow is returned when the length of an array or slice has overflowed.
var ErrUnexpectedEndOfBuffer = errors.New("unexpected end of buffer")
ErrUnexpectedEndOfBuffer is used when reading/writing from/to a buffer that has less space than expected.
Functions ¶
func FromBinary ¶
FromBinary unmarshals a byte representation of a value to a pointer to that value. In uses the maximum memory quota to restrict the number of bytes that will be allocated during unmarshaling.
func Marshal ¶
Marshal a value into its binary representation, and store the value in a byte slice. The "remaining memory quota" defines the maximum amount of bytes that can be allocated on the heap when marshaling the value. In this way, the remaining memory quota can be used to avoid allocating too much memory during marshaling. Marshaling supports all scalars, strings, arrays, slices, maps, structs, and custom implementations (for types that implement the Marshaler interface). After marshaling, the unconsumed tail of the byte slice, and the remaining memory quota, are returned. If the byte slice is too small, then an error is returned. Similarly, if the remaining memory quote is too small, then an error is returned. If the type is not supported, then an error is returned. An error does not imply that nothing from the byte slice, or remaining memory quota, was consumed. If the value is a pointer, then the underlying value being pointed to will be marshaled.
x := int64(0) buf := make([]byte, 8) tail, rem, err := surge.Marshal(x, buf, 8) if len(tail) != 0 { panic("assertion failed: int64 must consume 8 bytes") } if rem != 0 { panic("assertion failed: int64 must consume 8 bytes of the memory quota") } if err != nil { panic(fmt.Errorf("assertion failed: %v", err)) }
func MarshalBool ¶
MarshalBool into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalBytes ¶
MarshalBytes into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalF32 ¶
MarshalF32 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalF64 ¶
MarshalF64 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalI16 ¶
MarshalI16 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalI32 ¶
MarshalI32 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalI64 ¶
MarshalI64 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalI8 ¶
MarshalI8 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalLen ¶
MarshalLen marshals the given slice length.
func MarshalString ¶
MarshalString into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalU16 ¶
MarshalU16 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalU32 ¶
MarshalU32 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalU64 ¶
MarshalU64 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func MarshalU8 ¶
MarshalU8 into a byte slice. It will not consume more memory than the remaining memory quota (either through writes, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func NewErrUnsupportedMarshalType ¶
func NewErrUnsupportedMarshalType(v interface{}) error
NewErrUnsupportedMarshalType constructs a new unsupported marshal type error for the given type.
func NewErrUnsupportedUnmarshalType ¶
func NewErrUnsupportedUnmarshalType(v interface{}) error
NewErrUnsupportedUnmarshalType constructs a new unsupported unmarshal type error for the given type.
func SizeHint ¶
func SizeHint(v interface{}) int
SizeHint returns the number of bytes required to store a value in its binary representation. This is the number of bytes "on the wire", not the number of bytes that need to be allocated during marshaling/unmarshaling (which can be different, depending on the representation of the value). SizeHint supports all scalars, strings, arrays, slices, maps, structs, and custom implementations (for types that implement the SizeHinter interface). If the type is not supported, then zero is returned. If the value is a pointer, then the size of the underlying value being pointed to will be returned.
x := int64(0) sizeHint := surge.SizeHint(x) if sizeHint != 8 { panic("assertion failed: size of int64 must be 8 bytes") }
func SizeHintBytes ¶
SizeHintBytes is the number of bytes required to represent the given byte slice in binary.
func SizeHintString ¶
SizeHintString is the number of bytes required to represent the given string in binary.
func ToBinary ¶
ToBinary returns the byte representation of a value. In uses the maximum memory quota to restrict the number of bytes that will be allocated during marshaling.
func Unmarshal ¶
Unmarshal a value from its binary representation by reading from a byte slice. The "remaining memory quota" defines the maximum amount of bytes that can be allocated on the heap when unmarshaling the value. In this way, the remaining memory quota can be used to avoid allocating too much memory during unmarshaling (this is particularly useful when dealing with potentially malicious input). Unmarshaling supports pointers to all scalars, strings, arrays, slices, maps, structs, and custom implementations (for types that implement the Unmarshaler interface). After unmarshaling, the unconsumed tail of the byte slice, and the remaining memory quota, are returned. If the byte slice is too small, then an error is returned. Similarly, if the remaining memory quote is too small, then an error is returned. If the type is not a pointer to one of the supported types, then an error is returned. An error does not imply that nothing from the byte slice, or remaining memory quota, was consumed. If the value is not a pointer, then an error is returned.
x := int64(0) buf := make([]byte, 8) tail, rem, err := surge.Unmarshal(&x, buf, 8) if len(tail) != 0 { panic("assertion failed: int64 must consume 8 bytes") } if rem != 0 { panic("assertion failed: int64 must consume 8 bytes of the memory quota") } if err != nil { panic(fmt.Errorf("assertion failed: %v", err)) }
func UnmarshalBool ¶
UnmarshalBool from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalBytes ¶
UnmarshalBytes from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalF32 ¶
UnmarshalF32 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalF64 ¶
UnmarshalF64 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalI16 ¶
UnmarshalI16 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalI32 ¶
UnmarshalI32 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalI64 ¶
UnmarshalI64 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalI8 ¶
UnmarshalI8 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalLen ¶
UnmarshalLen unmarshals a slice length, checking that the total space required for the slice will not exceed rem.
func UnmarshalString ¶
UnmarshalString from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalU16 ¶
UnmarshalU16 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalU32 ¶
UnmarshalU32 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalU64 ¶
UnmarshalU64 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
func UnmarshalU8 ¶
UnmarshalU8 from a byte slice. It will not consume more memory than the remaining memory quota (either through reads, or in-memory allocations). It will return the unconsumed tail of the byte slice, and the remaining memory quota. An error is returned if the byte slice is too small, or if the remainin memory quote is insufficient.
Types ¶
type ErrUnsupportedMarshalType ¶
type ErrUnsupportedMarshalType struct {
// contains filtered or unexported fields
}
ErrUnsupportedMarshalType is returned when the an unsupported type is encountered during marshaling.
type ErrUnsupportedUnmarshalType ¶
type ErrUnsupportedUnmarshalType struct {
// contains filtered or unexported fields
}
ErrUnsupportedUnmarshalType is returned when the an unsupported type is encountered during unmarshaling.
type MarshalUnmarshaler ¶
type MarshalUnmarshaler interface { Marshaler Unmarshaler }
A MarshalUnmarshaler is a marshaler and an unmarshaler.
type Marshaler ¶
type Marshaler interface { SizeHinter // Marshal this value into bytes. Marshal(buf []byte, rem int) ([]byte, int, error) }
A Marshaler can marshal itself into bytes.
type SizeHinter ¶
type SizeHinter interface { // SizeHint returns the upper bound for the number of bytes required to // represent this value in binary. SizeHint() int }
A SizeHinter can hint at the number of bytes required to represented it in binary.