adt

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: Apache-2.0 Imports: 9 Imported by: 18

Documentation

Overview

Package adt provides "atomic data types" as strongly-typed generic helpers for simple atomic operations (including sync.Map, sync.Pool, and a typed value).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareAndSwap

func CompareAndSwap[T comparable](a *Atomic[T], old, new T) bool

CompareAndSwap exposes the CompareAndSwap option for atomics that store values of comparable types.

func IsAtomicZero

func IsAtomicZero[T comparable](in *Atomic[T]) bool

IsAtomicZero checks an atomic value for a comparable type to see if it's zero. The fun.IsZero() function can't correctly check both that the Atomic is zero and that it holds a zero value, and because atomics need not be comparable this can't be a method on Atomic.

func SafeSet

func SafeSet[T comparable](atom *Atomic[T], value T)

SafeSet sets the atomic to the given value only if the value is not the Zero value for that type.

Types

type Atomic

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

Atomic is a very simple atomic Get/Set operation, providing a generic type-safe implementation wrapping sync/atomic.Value.

func NewAtomic

func NewAtomic[T any](initial T) *Atomic[T]

NewAtomic creates a new Atomic Get/Set value with the initial value already set.

func (*Atomic[T]) Get

func (a *Atomic[T]) Get() T

Get resolves the atomic value, returning the zero value of the type T if the value is unset.

func (*Atomic[T]) Set

func (a *Atomic[T]) Set(in T)

Set atomically sets the value of the Atomic.

func (*Atomic[T]) Swap

func (a *Atomic[T]) Swap(new T) (old T)

Swap does an in place exchange of the contents of a value exchanging the new value for the old. Unlike sync.Atomic.Swap() if new is nil, adt.Atomic.Swap() does NOT panic, and instead constructs the zero value of type T.

type Map

type Map[K comparable, V any] struct {
	// Default handles construction and pools objects in
	// the map for the Ensure and Get operations which must
	// construct zero-value items. No configuration or
	// construction is necessary; however, callers can modify the
	// default value constructed as needed.
	Default Pool[V]
	// contains filtered or unexported fields
}

Map provides a wrapper around the standard library's sync.Map type with key/value types enforced by generics. Additional helpers (Append, Extend, Populate) support adding multiple items to the map, while Iterator and StoreFrom provide compatibility with iterators.

func (*Map[K, V]) Append

func (mp *Map[K, V]) Append(its ...MapItem[K, V])

func (*Map[K, V]) Contains

func (mp *Map[K, V]) Contains(key K) bool

func (*Map[K, V]) Delete

func (mp *Map[K, V]) Delete(key K)

func (*Map[K, V]) Ensure

func (mp *Map[K, V]) Ensure(key K)

func (*Map[K, V]) EnsureDefault

func (mp *Map[K, V]) EnsureDefault(key K, constr func() V) V

func (*Map[K, V]) Export

func (mp *Map[K, V]) Export() map[K]V

func (*Map[K, V]) Extend

func (mp *Map[K, V]) Extend(its []MapItem[K, V])

func (*Map[K, V]) Get

func (mp *Map[K, V]) Get(key K) V

func (*Map[K, V]) Iterator

func (mp *Map[K, V]) Iterator() fun.Iterator[MapItem[K, V]]

func (*Map[K, V]) Join

func (mp *Map[K, V]) Join(in *Map[K, V])

func (*Map[K, V]) Len

func (mp *Map[K, V]) Len() int

func (*Map[K, V]) Load

func (mp *Map[K, V]) Load(key K) (V, bool)

func (*Map[K, V]) MarshalJSON

func (mp *Map[K, V]) MarshalJSON() ([]byte, error)

func (*Map[K, V]) Populate

func (mp *Map[K, V]) Populate(in map[K]V)

func (*Map[K, V]) Range

func (mp *Map[K, V]) Range(f func(K, V) bool)

func (*Map[K, V]) Set

func (mp *Map[K, V]) Set(it MapItem[K, V])

func (*Map[K, V]) Store

func (mp *Map[K, V]) Store(k K, v V)

func (*Map[K, V]) StoreFrom

func (mp *Map[K, V]) StoreFrom(ctx context.Context, iter fun.Iterator[MapItem[K, V]])

func (*Map[K, V]) Swap

func (mp *Map[K, V]) Swap(k K, v V) (V, bool)

func (*Map[K, V]) UnmarshalJSON

func (mp *Map[K, V]) UnmarshalJSON(in []byte) error

type MapItem

type MapItem[K comparable, V any] struct {
	Key   K
	Value V
}

func NewMapItem

func NewMapItem[K comparable, V any](k K, v V) MapItem[K, V]

type Pool

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

Pool is an ergonomic wrapper around sync.Pool that provides some additional functionality: generic type interface, a default clean up hook to modify (optionally) the object before returning it to the pool.

Additionally, the Make() method attaches an object finalizer to the object produced by the pool that returns the object to the pool rather than garbage collect it. This is likely to be less efficient than return the objects to the pool using defer functions, but may be more ergonomic in some situations.

Pool can be used with default construction; however, you should set the Constructor before calling Get() or Make() on the pool.

func (*Pool[T]) Get

func (p *Pool[T]) Get() T

Get returns an object from the pool or constructs a default object according to the constructor.

func (*Pool[T]) Make

func (p *Pool[T]) Make() T

Make gets an object out of the sync.Pool, and attaches a finalizer that returns the item to the pool when the object would be garbage collected.

Finalizer hooks are not automatically cleared by the Put() operation, so objects retrieved with Make should not be passed manually to Put().

func (*Pool[T]) Put

func (p *Pool[T]) Put(in T)

Put returns an object in the pool, calling the cleanuphook if set. Put *always* clears the object's finalizer before calling the cleanuphook or returning it to the pool.

func (*Pool[T]) SetCleanupHook

func (p *Pool[T]) SetCleanupHook(in func(T) T)

SetCleanupHook sets a function to be called on every object renetering the pool. By default, the cleanup function is a noop.

func (*Pool[T]) SetConstructor

func (p *Pool[T]) SetConstructor(in func() T)

Jump to

Keyboard shortcuts

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