Documentation
¶
Overview ¶
Package union stores values of several concrete types in one off-heap handle, the way an interface does on the Go heap. Off-heap memory cannot hold an interface, because an interface holds Go pointers; a Value holds a small type ID and the bytes of the concrete value instead, and a Registry maps the ID back to the type.
Every registered type must pass the same check as an allocation: no Go pointers, though memory handles such as memory.String are allowed. A payload of up to 16 bytes is kept inside the Value; a larger one is allocated from a memory.Allocator and released by Free.
Example ¶
package main
import (
"fmt"
"math"
memory "github.com/stubbedev/memory-go"
"github.com/stubbedev/memory-go/union"
"github.com/stubbedev/memory-go/vector"
)
type shape interface{ area() float64 }
type circle struct{ R float64 }
func (c circle) area() float64 { return math.Pi * c.R * c.R }
type rect struct{ W, H float64 }
func (r rect) area() float64 { return r.W * r.H }
func main() {
var a memory.Local
defer a.Close()
var r union.Registry
if _, err := union.Register[circle](&r); err != nil {
panic(err)
}
if _, err := union.Register[rect](&r); err != nil {
panic(err)
}
shapes, err := vector.New[union.Value](&a, 0)
if err != nil {
panic(err)
}
defer shapes.Free()
for _, s := range []shape{circle{R: 1}, rect{W: 2, H: 3}} {
v, err := union.StoreInterface(&a, &r, s)
if err != nil {
panic(err)
}
if err := shapes.Push(v); err != nil {
panic(err)
}
}
for v := range shapes.Values() {
fmt.Printf("%.2f\n", union.Interface(&r, v).(shape).area())
if err := union.Free(&a, v); err != nil {
panic(err)
}
}
}
Output: 3.14 6.00
Index ¶
Examples ¶
Constants ¶
const InlineSize = 16
InlineSize is the largest payload, in bytes, kept inside a Value without allocating.
Variables ¶
var ErrUnregistered = errors.New("type not registered")
ErrUnregistered reports a type that was never registered with the Registry.
Functions ¶
func Free ¶
Free releases the payload of v, which must come from Store with the same allocator. Freeing a Value with an inline payload, or the zero Value, does nothing. Freeing a large payload twice, or through another allocator, panics.
func Interface ¶
Interface returns the value v holds as an interface, or nil if v holds nothing or an ID r does not know. It panics if the payload of v was already freed.
func Load ¶
Load returns the T v holds, or reports false if v holds nothing or a value of another type. It panics if the payload of v was already freed.
Types ¶
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry assigns type IDs, starting at 1, to the concrete types a Value may hold. The zero Registry is empty and ready. It is safe for concurrent use and must not be copied after use. IDs belong to one Registry: a Value must be read with the Registry that stored it.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value holds one value of a registered type, or nothing. It holds no Go pointers, so it may be stored off-heap: in memory.New, a vector or a hashmap. A Value whose payload exceeds InlineSize owns an allocation that Free releases. The zero Value holds nothing. It is not comparable, because == could only compare addresses of large payloads; compare what Load returns.
func Store ¶
Store returns a Value holding x, allocating from a if x is larger than InlineSize. T must be registered with r; otherwise the error wraps ErrUnregistered.
func StoreInterface ¶
StoreInterface is Store for a value whose concrete type is known only at run time, such as one held in an interface. Its dynamic type must be registered with r; otherwise, or if x is nil, the error wraps ErrUnregistered.