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 ¶
- func Wrap[T ~Node[T, C, Raw], C Constraint, Raw any](c C, id ID[T]) T
- func WrapDyn[T ~DynNode[T, K, C], K Kind[K], C Constraint](c C, id Dyn[T, K]) T
- func WrapRaw[T ~Node[T, C, Raw], C Constraint, Raw any](c C, id ID[T], raw Raw) T
- type Constraint
- type Context
- type Dyn
- type DynNode
- type DynSeq
- type HasContext
- type ID
- type Kind
- type Node
- type Seq
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.
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 ¶
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 NewDynFromRaw ¶
NewDynFromRaw encodes a new Dyn from the given raw parts.
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]
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.
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 ¶
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.
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]
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.