utilgo.mau.fi/util/exsync Index | Files

package exsync

import "go.mau.fi/util/exsync"

Index

Variables

var (
	// StopIteration can be returned by the RingBuffer.Iter or MapRingBuffer callbacks to stop iteration immediately.
	StopIteration = errors.New("stop iteration")

	// SkipItem can be returned by the MapRingBuffer callback to skip adding a specific item.
	SkipItem = errors.New("skip item")
)

Functions

func MapRingBuffer

func MapRingBuffer[Key comparable, Value, Output any](rb *RingBuffer[Key, Value], callback func(key Key, val Value) (Output, error)) ([]Output, error)

Types

type Map

type Map[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

Map is a simple map with a built-in mutex.

func NewMap

func NewMap[Key comparable, Value any]() *Map[Key, Value]

func (*Map[Key, Value]) Clone

func (sm *Map[Key, Value]) Clone() *Map[Key, Value]

Clone returns a copy of the map.

func (*Map[Key, Value]) CopyData

func (sm *Map[Key, Value]) CopyData() map[Key]Value

CopyData returns a copy of the data in the map as a normal (non-atomic) map.

func (*Map[Key, Value]) Delete

func (sm *Map[Key, Value]) Delete(key Key)

Delete removes a key from the map.

func (*Map[Key, Value]) Get

func (sm *Map[Key, Value]) Get(key Key) (value Value, ok bool)

Get gets a value in the map.

The boolean return parameter is the same as with normal Go map access (true if the key exists, false if not).

func (*Map[Key, Value]) GetOrSet

func (sm *Map[Key, Value]) GetOrSet(key Key, value Value) (actual Value, wasGet bool)

GetOrSet gets a value in the map if the key already exists, otherwise inserts the given value and returns it.

The boolean return parameter is true if the key already exists, and false if the given value was inserted.

func (*Map[Key, Value]) Pop

func (sm *Map[Key, Value]) Pop(key Key) (value Value, ok bool)

Pop removes a key from the map and returns the old value.

The boolean return parameter is the same as with normal Go map access (true if the key exists, false if not).

func (*Map[Key, Value]) Set

func (sm *Map[Key, Value]) Set(key Key, value Value)

Set stores a value in the map.

func (*Map[Key, Value]) Swap

func (sm *Map[Key, Value]) Swap(key Key, value Value) (oldValue Value, wasReplaced bool)

Swap sets a value in the map and returns the old value.

The boolean return parameter is true if the value already existed, false if not.

type ReturnableOnce

type ReturnableOnce[Value any] struct {
	// contains filtered or unexported fields
}

ReturnableOnce is a wrapper for sync.Once that can return a value

func (*ReturnableOnce[Value]) Do

func (ronce *ReturnableOnce[Value]) Do(fn func() (Value, error)) (Value, error)

type RingBuffer

type RingBuffer[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

func NewRingBuffer

func NewRingBuffer[Key comparable, Value any](size int) *RingBuffer[Key, Value]

func (*RingBuffer[Key, Value]) Contains

func (rb *RingBuffer[Key, Value]) Contains(val Key) bool

func (*RingBuffer[Key, Value]) Get

func (rb *RingBuffer[Key, Value]) Get(key Key) (val Value, found bool)

func (*RingBuffer[Key, Value]) Iter

func (rb *RingBuffer[Key, Value]) Iter(callback func(key Key, val Value) error) error

func (*RingBuffer[Key, Value]) Push

func (rb *RingBuffer[Key, Value]) Push(key Key, val Value)

func (*RingBuffer[Key, Value]) Replace

func (rb *RingBuffer[Key, Value]) Replace(key Key, val Value) bool

func (*RingBuffer[Key, Value]) Size

func (rb *RingBuffer[Key, Value]) Size() int

Source Files

returnonce.go ringbuffer.go syncmap.go

Version
v0.2.1
Published
Nov 16, 2023
Platform
darwin/amd64
Imports
2 packages
Last checked
4 hours ago

Tools for package owners.