Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// operations — Load, Store, LoadOrStore, LoadAndDelete, Delete, and
// Range — with identical semantics and the same concurrency
// guarantees. Four convenience methods are added on top: Len, Map,
// Keys, and Items. The underlying sync.Map is not exported; use the
// Keys, and Values. The underlying sync.Map is not exported; use the
// typed methods exclusively.
//
// # When to use SyncMap
Expand Down
10 changes: 5 additions & 5 deletions syncmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,19 @@ func (m *SyncMap[K, V]) Keys() []K {
return keys
}

// Items returns a slice of all values present in the map at the
// Values returns a slice of all values present in the map at the
// moment of the call. It runs in O(n) time.
//
// The result is a point-in-time approximation. Concurrent stores and
// deletes may cause the slice to include values that have since been
// removed, or to omit values that were added during traversal. The
// order of values is undefined, and does not correspond to the order
// returned by Keys.
func (m *SyncMap[K, V]) Items() []V {
var items []V
func (m *SyncMap[K, V]) Values() []V {
var values []V
m.syncMap.Range(func(key, value any) bool {
items = append(items, value.(V))
values = append(values, value.(V))
return true
})
return items
return values
}
14 changes: 7 additions & 7 deletions syncmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,14 @@ func TestKeys(t *testing.T) {
})
}

func TestItems(t *testing.T) {
func TestValues(t *testing.T) {
t.Parallel()

t.Run("empty_is_empty", func(t *testing.T) {
t.Parallel()
m := &syncmap.SyncMap[string, int]{}
items := m.Items()
assert.Empty(t, items)
values := m.Values()
assert.Empty(t, values)
})

t.Run("matches", func(t *testing.T) {
Expand All @@ -373,17 +373,17 @@ func TestItems(t *testing.T) {
m.Store("a", 1)
m.Store("b", 2)
m.Store("c", 3)
items := m.Items()
sort.Ints(items)
assert.Equal(t, []int{1, 2, 3}, items)
values := m.Values()
sort.Ints(values)
assert.Equal(t, []int{1, 2, 3}, values)
})

t.Run("length_equals_len", func(t *testing.T) {
t.Parallel()
m := &syncmap.SyncMap[string, int]{}
m.Store("x", 10)
m.Store("y", 20)
assert.Equal(t, m.Len(), len(m.Items()))
assert.Equal(t, m.Len(), len(m.Values()))
})
}

Expand Down