ctxutil

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 5 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

Index

Constants

This section is empty.

Variables

View Source
var (
	// Error of a Reset with a token that already undid its Set
	ErrTokenUsed = errors.New("token already used")
	// Error of a Reset with a token another variable's Set gave
	ErrTokenVariable = errors.New("token belongs to another variable")
	// Error of a Reset through a context running in another store than the Set did
	ErrTokenStore = errors.New("token belongs to another store")
)

Functions

func Fork

func Fork(ctx context.Context) context.Context

Context running in a copy of the store ctx runs in, so sets on either side stay apart from then on

func NonBlocking

func NonBlocking(ctx context.Context) bool

Whether ctx carries the non-blocking mark WithNonBlocking sets and WithBlocking lifts

func WithBlocking

func WithBlocking(ctx context.Context) context.Context

Context with the non-blocking mark lifted, as a worker goroutine started off a marked context runs: blocking waits are fine there

func WithNonBlocking

func WithNonBlocking(ctx context.Context) context.Context

Context marked as serving asynchronous dispatch, where callees must not park the goroutine on blocking waits; NonBlocking reports the mark

func WithStore

func WithStore(ctx context.Context, s *Store) context.Context

Context running in s, every variable read or set through it using s

Types

type Item

type Item struct {
	Var   Variable
	Value any
}

Variable and the value a Store holds for it

type Store

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

Mutable set of variable values a context carries: a Set made through the context is seen by all later code running in it, and Copy forks it so later sets on either side stay apart

func CopyStore

func CopyStore(ctx context.Context) *Store

Independent copy of the store ctx runs in

func NewStore

func NewStore() *Store

Empty store

func StoreOf

func StoreOf(ctx context.Context) *Store

Store ctx runs in, the process wide default store when it entered none

func (*Store) Checkpoint

func (s *Store) Checkpoint() (restore func())

Restore point: calling the returned func puts every value of s back to what it holds now

func (*Store) Copy

func (s *Store) Copy() *Store

Independent copy holding the same values, made in constant time

func (*Store) Items

func (s *Store) Items() []Item

Every variable s holds a value for, with that value, in the order they were first set

func (*Store) Overlay

func (s *Store) Overlay(src *Store)

Takes on every value src holds, variables new to s going last in src's order

type Token

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

Record of one Set that Reset uses to put the earlier value back once

type Var

type Var[T any] struct {
	// contains filtered or unexported fields
}

Variable whose value lives in the Store of the context it is used through, reading as its default where that Store holds none

func NewVar

func NewVar[T any](name string, def T) *Var[T]

Variable with its name and default value

func (*Var[T]) Get

func (v *Var[T]) Get(ctx context.Context) T

Value the store ctx runs in holds, else the default

func (*Var[T]) Name

func (v *Var[T]) Name() string

Name the variable was declared with

func (*Var[T]) Reset

func (v *Var[T]) Reset(ctx context.Context, t *Token) error

Undoes the Set that gave t, putting back the value held before or none, through a context running in the same store

func (*Var[T]) Set

func (v *Var[T]) Set(ctx context.Context, value T) *Token

Stores value in the store ctx runs in and returns the token that undoes it

type Variable

type Variable interface {
	Name() string
	// contains filtered or unexported methods
}

Variable of any value type, the form a Store lists its entries in

Jump to

Keyboard shortcuts

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