id

package
v0.38.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 3 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 id provides helpers for working with node-oriented data structures (such as ASTs and graphs) using compressed pointers (IDs).

The whole data structure is represented by some kind of Context type; nodes of different types which that data structure are represented by [ID]s which index into the Context. The actual data of each node is stored in an unexported type in the package that defines it, and the exported version is a type based on Node, which provides all of the helpers necessary for a consistent node type interface, such as IsZero, ID, and Context.

Dyn is a variant on ID that is heavier but allows for storing extra data in addition to the ID value itself, such as type information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap

func Wrap[T ~Node[T, C, Raw], C Constraint, Raw any](c C, id ID[T]) T

Wrap gets the value of type T with the given ID from c.

If c or id is its zero value (e.g. nil), returns a zero value.

func WrapDyn

func WrapDyn[T ~DynNode[T, K, C], K Kind[K], C Constraint](c C, id Dyn[T, K]) T

WrapDyn wraps a dynamic ID with a context.

If c or id is its zero value (e.g. nil), returns a zero value.

func WrapRaw

func WrapRaw[T ~Node[T, C, Raw], C Constraint, Raw any](c C, id ID[T], raw Raw) T

WrapRaw constructs a new T using the given raw value.

If c or id is its zero value (e.g. nil), returns a zero value.

Types

type Constraint

type Constraint interface {
	comparable
	Context
}

Constraint is a version of Context that can be used as a constraint.

type Context

type Context interface {
	// FromID gets the value for a given ID.
	//
	// The ID will be passed in as a raw 64-bit value. It is up to the caller
	// to interpret it based on the requested type.
	//
	// The requested type is passed in via the parameter want, which will be
	// a nil pointer to a value of the desired type. E.g., if the desired type
	// is *int, want will be (**int)(nil).
	FromID(id uint64, want any) any
}

Context is an "ID context", which allows converting between IDs and the underlying values they represent.

Users of this package should not call the Context methods directly.

type Dyn

type Dyn[T any, K Kind[K]] uint64

Dyn is a generic 64-bit identifier for a value of type T, intended to carry additional dynamic type information than an ID. It consists of a K and an ID[T]. However, some uses may make use of the whole 64 bits of this ID, which can be accessed with Dyn.Raw.

DynIDs are typed and require a Context to be interpreted.

func NewDyn

func NewDyn[T any, K Kind[K]](kind K, id ID[T]) Dyn[T, K]

NewDyn encodes a new Dyn from the given parts.

func NewDynFromRaw

func NewDynFromRaw[T any, K Kind[K]](lo, hi int32) Dyn[T, K]

NewDynFromRaw encodes a new Dyn from the given raw parts.

func (Dyn[T, K]) IsZero

func (id Dyn[T, K]) IsZero() bool

IsZero returns whether this is the zero ID.

func (Dyn[T, K]) Kind

func (id Dyn[T, K]) Kind() K

Kind returns the kind of this DynID.

func (Dyn[T, K]) Raw

func (id Dyn[T, K]) Raw() (lo, hi int32)

Raw reinterprets this ID as two 32-bit integers.

func (Dyn[T, K]) String

func (id Dyn[T, K]) String() string

String implements fmt.Stringer.

func (Dyn[T, K]) Value

func (id Dyn[T, K]) Value() ID[T]

Value returns the id part of this DynID.

If the resulting ID is not well-formed, returns zero.

type DynNode

type DynNode[T any, K Kind[K], C Constraint] = struct {
	// contains filtered or unexported fields
}

DynNode is the equivalent of Node for Dyn.

Types which are a context/ID pair should be defined as

type MyNode DynNode[MyNode, MyNodeKind, Context]

func (DynNode) ID

func (v DynNode) ID() Dyn[T, K]

ID returns this value's ID.

func (DynNode) Kind

func (v DynNode) Kind() K

Kind returns this value's kind.

type DynSeq

type DynSeq[T ~DynNode[T, K, C], K Kind[K], C Constraint] struct {
	// contains filtered or unexported fields
}

DynSeq is an array of dynamic nodes which uses a compressed representation.

A zero value is empty and ready to use.

func (*DynSeq[T, K, C]) Inserter

func (s *DynSeq[T, K, C]) Inserter(context C) seq.Inserter[T]

Inserter returns a seq.Inserter wrapping this DynSeq.

type HasContext

type HasContext[Context comparable] struct {
	// contains filtered or unexported fields
}

HasContext is a helper for adding IsZero and Context methods to a type.

Simply alias it as an unexported type in your package, and embed it into types of interest.

func WrapContext

func WrapContext[Context comparable](c Context) HasContext[Context]

WrapContext wraps the context c in a HasContext.

func (HasContext[Context]) Context

func (c HasContext[Context]) Context() Context

Context returns this value's context.

func (HasContext[Context]) IsZero

func (c HasContext[Context]) IsZero() bool

IsZero returns whether this is a zero value.

type ID

type ID[T any] int32

ID is a generic 32-bit identifier for a value of type T. The zero value is reserved as a sentinel "no value" ID.

IDs are typed and require a Context to be interpreted.

func (ID[T]) IsZero

func (id ID[T]) IsZero() bool

IsZero returns whether this is the zero ID.

func (ID[T]) String

func (id ID[T]) String() string

String implements fmt.Stringer.

type Kind

type Kind[Self any] interface {
	comparable // Make it not useable as an interface.

	// Decodes the kind by decoding the low and high parts of a Dyn
	// and setting this value to the result. A zero return value is taken to
	// mean that decoding failed.
	DecodeDynID(lo, hi int32) Self

	// EncodeDynID encodes a new Dyn with the given value part. Returns
	// arguments for [NewDynFromRaw].
	EncodeDynID(value int32) (lo, hi int32, ok bool)
}

Kind is a kind type usable in a Dyn.

Self should be the type implementing Kind.

type Node

type Node[T any, C Constraint, Raw any] = struct {
	// contains filtered or unexported fields
}

Node is a raw node value with an associated context and ID.

Types which are a context/ID pair should be defined as

type MyNode id.Node[MyNode, Context, *rawMyNode]

func (Node) ID

func (v Node) ID() ID[T]

ID returns this value's ID.

func (Node) Raw

func (v Node) Raw() R

Raw returns the wrapped raw value.

type Seq

type Seq[T ~Node[T, C, Raw], C Constraint, Raw any] struct {
	// contains filtered or unexported fields
}

Seq is an array of nodes which uses a compressed representation.

A zero value is empty and ready to use.

func (*Seq[T, C, Raw]) Inserter

func (s *Seq[T, C, Raw]) Inserter(context C) seq.Inserter[T]

Inserter returns a seq.Inserter wrapping this Seq.

Jump to

Keyboard shortcuts

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