on main help sign in
docs: godoc
commit7e7a69fa6b86d5eaac25b9c0fe5a33c3e805b843 authorCarlos Alexandro Becker <caarlos0@users.noreply.github.com> dateTue 22 Jul 2025, 20:42 (1 year ago) parenta6f21b2
2 files changed+12
internal/csync/maps.go+8 internal/csync/slices.go+4
internal/csync/maps.go+8
@@ -7,35 +7,41 @@ import (
77 "sync"
88)
99
10+// Map is a concurrent map implementation that provides thread-safe access.
1011type Map[K comparable, V any] struct {
1112 inner map[K]V
1213 mu sync.RWMutex
1314}
1415
16+// NewMap creates a new thread-safe map with the specified key and value types.
1517func NewMap[K comparable, V any]() *Map[K, V] {
1618 return &Map[K, V]{
1719 inner: make(map[K]V),
1820 }
1921}
2022
23+// NewMapFrom creates a new thread-safe map from an existing map.
2124func NewMapFrom[K comparable, V any](m map[K]V) *Map[K, V] {
2225 return &Map[K, V]{
2326 inner: m,
2427 }
2528}
2629
30+// Set sets the value for the specified key in the map.
2731func (m *Map[K, V]) Set(key K, value V) {
2832 m.mu.Lock()
2933 defer m.mu.Unlock()
3034 m.inner[key] = value
3135}
3236
37+// Del deletes the specified key from the map.
3338func (m *Map[K, V]) Del(key K) {
3439 m.mu.Lock()
3540 defer m.mu.Unlock()
3641 delete(m.inner, key)
3742}
3843
44+// Get gets the value for the specified key from the map.
3945func (m *Map[K, V]) Get(key K) (V, bool) {
4046 m.mu.RLock()
4147 defer m.mu.RUnlock()
@@ -43,12 +49,14 @@ func (m *Map[K, V]) Get(key K) (V, bool) {
4349 return v, ok
4450}
4551
52+// Len returns the number of items in the map.
4653func (m *Map[K, V]) Len() int {
4754 m.mu.RLock()
4855 defer m.mu.RUnlock()
4956 return len(m.inner)
5057}
5158
59+// Seq2 returns an iter.Seq2 that yields key-value pairs from the map.
5260func (m *Map[K, V]) Seq2() iter.Seq2[K, V] {
5361 dst := make(map[K]V)
5462 m.mu.RLock()
internal/csync/slices.go+4
@@ -5,11 +5,14 @@ import (
55 "sync"
66)
77
8+// LazySlice is a thread-safe lazy-loaded slice.
89type LazySlice[K any] struct {
910 inner []K
1011 mu sync.Mutex
1112}
1213
14+// NewLazySlice creates a new slice and runs the [load] function in a goroutine
15+// to populate it.
1316func NewLazySlice[K any](load func() []K) *LazySlice[K] {
1417 s := &LazySlice[K]{}
1518 s.mu.Lock()
@@ -20,6 +23,7 @@ func NewLazySlice[K any](load func() []K) *LazySlice[K] {
2023 return s
2124}
2225
26+// Iter returns an iterator that yields elements from the slice.
2327func (s *LazySlice[K]) Iter() iter.Seq[K] {
2428 s.mu.Lock()
2529 inner := s.inner