box

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 6 Imported by: 1

README

box

GoDoc

Box is a Go package for wrapping value types. Works similar to an interface{} and is optimized for primitives, strings, and byte slices.

Features

  • Zero new allocations for wrapping primitives, strings, and []byte slices.
  • Uses a 128 bit structure. Same as interface{} on 64-bit architectures.
  • Allows for auto convertions between value types. No panics on assertions.
  • Pretty decent performance.

Examples

// A boxed value can hold various types, just like an interface{}.
var v box.Value

// box an int
v = box.Int(123)

// unbox the value
println(v.Int())
println(v.String())
println(v.Bool())

// box a string
v = box.String("hello")
println(v.String())

// Auto conversions between types
println(box.String("123.45").Float64())
println(box.Bool(false).String())
println(box.String("hello").IsString())

// output
// 123
// 123
// true
// hello
// +1.234500e+002
// false
// true

Performance

Below are some benchmarks comparing interface{} to box.Value.

  • Iface*/to: Convert a value to an interface{}.
  • Iface*/from: Convert an interface{} back to its original value.
  • Box*/to: Convert a value to box.Value.
  • Box*/from: Convert a box.Value back to its original value.
goos: darwin
goarch: arm64
pkg: github.com/tidwall/box
BenchmarkIfaceInt/to-10        10000000    8.921 ns/op    7 B/op   0 allocs/op
BenchmarkIfaceInt/from-10      10000000    0.6289 ns/op   0 B/op   0 allocs/op
BenchmarkBoxInt/to-10          10000000    1.334 ns/op    0 B/op   0 allocs/op
BenchmarkBoxInt/from-10        10000000    0.6823 ns/op   0 B/op   0 allocs/op
BenchmarkIfaceString/to-10     10000000   18.17 ns/op    16 B/op   1 allocs/op
BenchmarkIfaceString/from-10   10000000    0.8010 ns/op   0 B/op   0 allocs/op
BenchmarkBoxString/to-10       10000000    3.705 ns/op    0 B/op   0 allocs/op
BenchmarkBoxString/from-10     10000000    2.421 ns/op    0 B/op   0 allocs/op
BenchmarkIfaceBytes/to-10      10000000   21.62 ns/op    24 B/op   1 allocs/op
BenchmarkIfaceBytes/from-10    10000000    0.8104 ns/op   0 B/op   0 allocs/op
BenchmarkBoxBytes/to-10        10000000    2.881 ns/op    0 B/op   0 allocs/op
BenchmarkBoxBytes/from-10      10000000    2.366 ns/op    0 B/op   0 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Value

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

Value is a boxed value

func Any

func Any(v any) Value

Any boxes anything

func Bool

func Bool(t bool) Value

Bool boxes a bool

func Byte

func Byte(x byte) Value

Byte boxes an byte

func Bytes

func Bytes(b []byte) Value

Bytes boxes a byte slice

func CustomBits

func CustomBits(x uint64) Value

CustomBits boxes a custom value.

func Float32

func Float32(x float32) Value

Float32 boxes a float32

func Float64

func Float64(f float64) Value

Float64 boxes a float64

func Int

func Int(x int) Value

Int boxes an int

func Int16

func Int16(x int16) Value

Int16 boxes an int16

func Int32

func Int32(x int32) Value

Int32 boxes an int32

func Int64

func Int64(x int64) Value

Int64 boxes an int64

func Int8

func Int8(x int8) Value

Int8 boxes an int8

func Nil

func Nil() Value

Nil boxes a nil. This is the same as the default `box.Value{}`.

func String

func String(s string) Value

String boxes a string value

func StringWithTag

func StringWithTag(s string, tag uint16) Value

StringWithTag boxes a string value and adds a custom tag.

func Uint

func Uint(x uint) Value

Uint boxes a uint

func Uint16

func Uint16(x uint16) Value

Uint16 boxes a uint16

func Uint32

func Uint32(x uint32) Value

Uint32 boxes a uint32

func Uint64

func Uint64(x uint64) Value

Uint64 boxes a uint64

func Uint8

func Uint8(x uint8) Value

Uint8 boxes a uint8

func (Value) Any

func (v Value) Any() any

Any returns the value as an `any/interface{}` type.

func (Value) Bool

func (v Value) Bool() bool

Bool returns the value as a bool

func (Value) Byte

func (v Value) Byte() byte

Byte returns the value as a byte

func (Value) Bytes

func (v Value) Bytes() []byte

Bytes returns the value as a byte slice. When the boxed value is a `[]byte` then those original bytes are returned. Otherwise, the string representation of the value is returned, which will be equivalent to `[]byte(value.String())`.

func (Value) Float32

func (v Value) Float32() float32

Float32 returns the value as a float32

func (Value) Float64

func (v Value) Float64() float64

Float64 returns the value as a float64

func (Value) Int

func (v Value) Int() int

Int returns the value as an int

func (Value) Int16

func (v Value) Int16() int16

Int16 returns the value as an int16

func (Value) Int32

func (v Value) Int32() int32

Int32 returns the value as an int32

func (Value) Int64

func (v Value) Int64() int64

Int64 returns the value as an int64

func (Value) Int8

func (v Value) Int8() int8

Int8 returns the value as an int8

func (Value) IsBool

func (v Value) IsBool() bool

IsBool returns true if the boxed value is a bool primitive.

func (Value) IsBytes

func (v Value) IsBytes() bool

IsBytes returns true if the boxed value is a []byte.

func (Value) IsCustomBits

func (v Value) IsCustomBits() bool

IsCustomBits returns true if the boxed value was created using box.CustomBits.

func (Value) IsFloat

func (v Value) IsFloat() bool

IsFloat returns true if the boxed value is an float-like primitive: float32, float64

func (Value) IsInt

func (v Value) IsInt() bool

IsInt returns true if the boxed value is an int-like primitive: int, int8, int16, int32, int64, byte

func (Value) IsNil

func (v Value) IsNil() bool

IsNil returns true if the boxed value is nil.

func (Value) IsNumber

func (v Value) IsNumber() bool

IsNumber returns true if the boxed value is an numeric-like primitive: int, int8, int16, int32, int64, byte, uint, uint8, uint16, uint32, uint64, float32, float64

func (Value) IsString

func (v Value) IsString() bool

IsString returns true if the boxed value is a string.

func (Value) IsUint

func (v Value) IsUint() bool

IsUint returns true if the boxed value is an uint-like primitive: uint, uint8, uint16, uint32, uint64

func (Value) String

func (v Value) String() string

String returns the value as a string.

func (Value) Tag

func (v Value) Tag() uint16

Tag returns the tag from a value created by box.StringWithTag

func (Value) Uint

func (v Value) Uint() uint

Uint returns the value as a uint

func (Value) Uint16

func (v Value) Uint16() uint16

Uint16 returns the value as a uint16

func (Value) Uint32

func (v Value) Uint32() uint32

Uint32 returns the value as a uint32

func (Value) Uint64

func (v Value) Uint64() uint64

Uint64 returns the value as a uint64

func (Value) Uint8

func (v Value) Uint8() uint8

Uint8 returns the value as a uint8

Jump to

Keyboard shortcuts

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