invariants

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Index

Constants

Enabled is true if we were built with the "invariants" or "race" build tags.

Enabled should be used to gate invariant checks that may be expensive. It should not be used to unconditionally alter a code path significantly (e.g. wrapping an iterator - see #3678); Sometimes() should be used instead so that the production code path gets test coverage as well.

View Source
const RaceEnabled = buildtags.Race

RaceEnabled is true if we were built with the "race" build tag.

UseFinalizers is true if we want to use finalizers for assertions around object lifetime and cleanup. This happens when the invariants or tracing tags are set, but we exclude race builds because we historically ran into some finalizer-related race detector bugs.

Variables

This section is empty.

Functions

func CheckBounds added in v2.1.0

func CheckBounds[T Integer](i T, n T)

CheckBounds panics if the index is not in the range [0, n). No-op in non-invariant builds.

func SafeSub added in v2.0.5

func SafeSub[T Integer](a, b T) T

SafeSub returns a - b. If a < b, it panics in invariant builds and returns 0 in non-invariant builds.

func SetFinalizer

func SetFinalizer(obj, finalizer interface{})

SetFinalizer is a wrapper around runtime.SetFinalizer that is a no-op under race builds or if neither the invariants nor tracing build tags are specified.

We exclude race builds because we historically ran into some race detector bugs related to finalizers.

This function is a no-op if UseFinalizers is false and it should inline to nothing. However, note that it might not inline so in very hot paths it's best to check UseFinalizers first.

func Sometimes

func Sometimes(percent int) bool

Sometimes returns true percent% of the time if invariants are Enabled (i.e. we were built with the "invariants" or "race" build tags). Otherwise, always returns false.

Types

type BufMangler added in v2.1.0

type BufMangler struct{}

BufMangler is a utility that can be used to test that the caller doesn't use

func (*BufMangler) MaybeMangleLater added in v2.1.0

func (bm *BufMangler) MaybeMangleLater(buf []byte) []byte

MaybeMangleLater returns either the given buffer or a copy of it which will be mangled the next time this function is called.

type CloseChecker

type CloseChecker struct{}

CloseChecker is used to check that objects are closed exactly once. It is empty and does nothing in non-invariant builds.

Note that in non-invariant builds, the struct is zero-sized but it can still increase the size of a parent struct if it is the last field (because Go must allow getting a valid pointer address of the field).

func (*CloseChecker) AssertClosed

func (d *CloseChecker) AssertClosed()

AssertClosed panics in invariant builds if Close was not called.

func (*CloseChecker) AssertNotClosed

func (d *CloseChecker) AssertNotClosed()

AssertNotClosed panics in invariant builds if Close was called.

func (*CloseChecker) Close

func (d *CloseChecker) Close()

Close panics if called twice on the same object (if we were built with the "invariants" or "race" build tags).

type Integer added in v2.0.5

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer is a constraint that permits any integer type.

type Value

type Value[V any] struct{}

Value is a generic container for a value that should only exist in invariant builds. In non-invariant builds, storing a value is a no-op, retrieving a value returns the type parameter's zero value, and the Value struct takes up no space.

Note that in non-invariant builds, the struct is zero-sized but it can still increase the size of a parent struct if it is the last field (because Go must allow getting a valid pointer address of the field).

func (*Value[V]) Get

func (*Value[V]) Get() V

Get the current value, or the zero value if invariants are disabled.

func (*Value[V]) Set added in v2.1.0

func (*Value[V]) Set(v V)

Set the value; no-op in non-invariant builds.

Jump to

Keyboard shortcuts

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