exsync

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: MPL-2.0 Imports: 4 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// StopIteration can be returned by the RingBuffer.Iter or MapRingBuffer callbacks to stop iteration immediately.
	StopIteration = errors.New("stop iteration") //lint:ignore ST1012 not an error

	// SkipItem can be returned by the MapRingBuffer callback to skip adding a specific item.
	SkipItem = errors.New("skip item") //lint:ignore ST1012 not an error
)

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 Event added in v0.7.0

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

Event is a wrapper around a channel that can be used to notify multiple waiters that some event has happened.

It's modelled after Python's asyncio.Event: https://docs.python.org/3/library/asyncio-sync.html#asyncio.Event

func NewEvent added in v0.7.0

func NewEvent() *Event

NewEvent creates a new event. It will initially be unset.

func (*Event) Clear added in v0.7.0

func (e *Event) Clear()

Clear clears the event, making it unset. Future calls to Wait will now block until Set is called again. If the event is not already set, this is a no-op and existing calls to Wait will keep working.

func (*Event) GetChan added in v0.7.0

func (e *Event) GetChan() EventChan

GetChan returns the channel that will be closed when the event is set.

func (*Event) IsSet added in v0.7.0

func (e *Event) IsSet() bool

IsSet returns true if the event has been set.

func (*Event) Set added in v0.7.0

func (e *Event) Set()

Set sets the event, notifying all waiters.

func (*Event) Wait added in v0.7.0

func (e *Event) Wait(ctx context.Context) error

Wait waits for either the event to happen or the given context to be done. If the context is done first, the error is returned, otherwise the return value is nil.

func (*Event) WaitTimeout added in v0.7.0

func (e *Event) WaitTimeout(timeout time.Duration) bool

WaitTimeout waits for either the event to happen within the given timeout. If the timeout expires first, the return value is false, otherwise it's true.

type EventChan added in v0.8.2

type EventChan = <-chan empty

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 deprecated

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

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

Deprecated: Use sync.OnceValues instead.

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

type Set added in v0.3.0

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a wrapper around a map[T]struct{} with a built-in mutex.

func NewSet added in v0.3.0

func NewSet[T comparable]() *Set[T]

NewSet constructs a Set with an empty map.

func NewSetWithItems added in v0.3.0

func NewSetWithItems[T comparable](items []T) *Set[T]

NewSetWithItems constructs a Set with items from the given slice pre-filled. The slice is not modified or used after the function returns, so using it after this is safe.

func NewSetWithMap added in v0.3.0

func NewSetWithMap[T comparable](m map[T]empty) *Set[T]

NewSetWithMap constructs a Set with the given map. Accessing the map directly after passing it here is not safe.

func NewSetWithSize added in v0.3.0

func NewSetWithSize[T comparable](size int) *Set[T]

NewSetWithSize constructs a Set with a map that has been allocated the given amount of space.

func (*Set[T]) Add added in v0.3.0

func (s *Set[T]) Add(item T) bool

Add adds an item to the set. The return value is true if the item was added to the set, or false otherwise.

func (*Set[T]) AsList added in v0.8.0

func (s *Set[T]) AsList() []T

func (*Set[T]) Has added in v0.3.0

func (s *Set[T]) Has(item T) bool

Has checks if the given item is in the set.

func (*Set[T]) Pop added in v0.8.0

func (s *Set[T]) Pop(item T) bool

Pop removes the given item from the set. The return value is true if the item was in the set, or false otherwise.

func (*Set[T]) Remove added in v0.3.0

func (s *Set[T]) Remove(item T)

Remove removes the given item from the set.

func (*Set[T]) ReplaceAll added in v0.8.0

func (s *Set[T]) ReplaceAll(newSet *Set[T])

ReplaceAll replaces this set with the given set. If the given set is nil, the set is cleared.

func (*Set[T]) Size added in v0.8.0

func (s *Set[T]) Size() int

Jump to

Keyboard shortcuts

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