union

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const InlineSize = 16

InlineSize is the largest payload, in bytes, kept inside a Value without allocating.

Variables

View Source
var ErrUnregistered = errors.New("type not registered")

ErrUnregistered reports a type that was never registered with the Registry.

Functions

func Free

func Free(a memory.Allocator, v Value) error

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

func Interface(r *Registry, v Value) any

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

func Load[T any](r *Registry, v Value) (T, bool)

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.

func Register

func Register[T any](r *Registry) (uint32, error)

Register adds T to r and returns its type ID. Registering a type again returns the ID it already has. T must hold no Go pointers; otherwise the error wraps memory.ErrHoldsPointers.

func TypeOf

func TypeOf(r *Registry, v Value) reflect.Type

TypeOf returns the type of the value v holds, or nil if v holds nothing or an ID r does not know.

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

func Store[T any](a memory.Allocator, r *Registry, x T) (Value, error)

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

func StoreInterface(a memory.Allocator, r *Registry, x any) (Value, error)

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.

func (Value) ID

func (v Value) ID() uint32

ID returns the type ID of the value v holds, or 0 if v holds nothing.

func (Value) IsNil

func (v Value) IsNil() bool

IsNil reports whether v holds nothing.

Jump to

Keyboard shortcuts

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