Repository: puzpuzpuz/xsync
Pull request #36 (2022-04-06 09:25:24)

LoadOrStore: actually return the given value
The method docs for `LoadOrStore` mention:
> it stores and returns the given value

but the current implementation does not return the provided value when `!loaded`.

Maybe I'm overlooking something obvious? :thinking: 

costela: That's [already done](https://github.com/puzpuzpuz/xsync/pull/36/files#diff-5445e2bbe6943084e702ba9c8e9bd5d085fceae2e3c9b2b7db4e172e20dc97fe) :wink: 
puzpuzpuz: > That's [already done](https://github.com/puzpuzpuz/xsync/pull/36/files#diff-5445e2bbe6943084e702ba9c8e9bd5d085fceae2e3c9b2b7db4e172e20dc97fe) :wink: 

My bad. Overlooked the changes.

Thanks for the contribution!

Patch:
diff --git a/map.go b/map.go
index 8041ceb..b4225e7 100644
--- a/map.go
+++ b/map.go
@@ -169,6 +169,7 @@ func (m *Map) doStore(key string, value interface{}, loadIfExists bool) (actual
 	if value == nil {
 		value = nilVal
 	}
+	actual = value
 	// Read-only path.
 	if loadIfExists {
 		if v, ok := m.Load(key); ok {
diff --git a/mapof.go b/mapof.go
index 6a5a459..773fe5b 100644
--- a/mapof.go
+++ b/mapof.go
@@ -103,6 +103,7 @@ func (m *MapOf[V]) doStore(key string, value V, loadIfExists bool) (actual V, lo
 			return v, true
 		}
 	}
+	actual = value
 	// Write path.
 	hash := maphash64(key)
 	for {


Test patch:
diff --git a/map_test.go b/map_test.go
index c784a78..a111e49 100644
--- a/map_test.go
+++ b/map_test.go
@@ -100,6 +100,19 @@ func TestMapLoadOrStore_NilValue(t *testing.T) {
 	}
 }
 
+func TestMapLoadOrStore_NonNilValue(t *testing.T) {
+	type foo struct{}
+	m := NewMap()
+	newv := &foo{}
+	v, loaded := m.LoadOrStore("foo", newv)
+	if loaded {
+		t.Error("no value was expected")
+	}
+	if v != newv {
+		t.Errorf("value was not newv: %v", v)
+	}
+}
+
 func TestMapRange(t *testing.T) {
 	const numEntries = 1000
 	m := NewMap()
@@ -579,8 +592,8 @@ func benchmarkMap(
 	loadFn func(k string) (interface{}, bool),
 	storeFn func(k string, v interface{}),
 	deleteFn func(k string),
-	readPercentage int) {
-
+	readPercentage int,
+) {
 	b.ResetTimer()
 	b.RunParallel(func(pb *testing.PB) {
 		r := rand.New(rand.NewSource(time.Now().UnixNano()))
diff --git a/mapof_test.go b/mapof_test.go
index 17d4b6c..285137c 100644
--- a/mapof_test.go
+++ b/mapof_test.go
@@ -4,11 +4,12 @@
 package xsync_test
 
 import (
-	. "github.com/puzpuzpuz/xsync"
 	"math/rand"
 	"strconv"
 	"testing"
 	"time"
+
+	. "github.com/puzpuzpuz/xsync"
 )
 
 func TestMapOf_MissingEntry(t *testing.T) {
@@ -61,6 +62,19 @@ func TestMapOfLoadOrStore_NilValue(t *testing.T) {
 	}
 }
 
+func TestMapOfLoadOrStore_NonNilValue(t *testing.T) {
+	type foo struct{}
+	m := NewMapOf[*foo]()
+	newv := &foo{}
+	v, loaded := m.LoadOrStore("foo", newv)
+	if loaded {
+		t.Error("no value was expected")
+	}
+	if v != newv {
+		t.Errorf("value was not newv: %v", v)
+	}
+}
+
 func TestMapOfRange(t *testing.T) {
 	const numEntries = 1000
 	m := NewMapOf[int]()
@@ -430,8 +444,8 @@ func benchmarkMapInt(
 	loadFn func(k string) (int, bool),
 	storeFn func(k string, v int),
 	deleteFn func(k string),
-	readPercentage int) {
-
+	readPercentage int,
+) {
 	b.ResetTimer()
 	b.RunParallel(func(pb *testing.PB) {
 		r := rand.New(rand.NewSource(time.Now().UnixNano()))