Skip to content

Commit e9dafc6

Browse files
authored
Merge pull request #21 from spekary/HideLock
Hiding the mutex functions
2 parents 71c70c3 + 9604fd5 commit e9dafc6

5 files changed

Lines changed: 106 additions & 95 deletions

File tree

mapi.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type MapI[K comparable, V any] interface {
1313
Has(k K) bool
1414
Keys() []K
1515
Values() []V
16-
Merge(MapI[K, V])
16+
Copy(MapI[K, V])
1717
Equal(MapI[K, V]) bool
1818
Delete(k K) V
1919
All() iter.Seq2[K, V]
@@ -22,6 +22,9 @@ type MapI[K comparable, V any] interface {
2222
Insert(seq iter.Seq2[K, V])
2323
DeleteFunc(del func(K, V) bool)
2424
String() string
25+
26+
// Deprecated: Call Copy instead
27+
Merge(MapI[K, V])
2528
}
2629

2730
// Setter sets a value in a map.

mapi_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"bytes"
55
"encoding/gob"
66
"encoding/json"
7-
"github.com/stretchr/testify/assert"
87
"iter"
98
"slices"
109
"strconv"
1110
"testing"
11+
12+
"github.com/stretchr/testify/assert"
1213
)
1314

1415
type makeF func(sources ...mapT) MapI[string, int]
@@ -18,15 +19,15 @@ func makeMapi[M any](sources ...mapT) MapI[string, int] {
1819
m = new(M)
1920
i := m.(MapI[string, int])
2021
for _, s := range sources {
21-
i.Merge(s)
22+
i.Copy(s)
2223
}
2324
return i
2425
}
2526

2627
func runMapiTests[M any](t *testing.T, f makeF) {
2728
testClear(t, f)
2829
testLen(t, f)
29-
testMerge(t, f)
30+
testCopy(t, f)
3031
testGetHasLoad(t, f)
3132
testRange(t, f)
3233
testSet(t, f)
@@ -69,7 +70,7 @@ func testLen(t *testing.T, f makeF) {
6970
assert.Equal(t, 2, f(mapT{"a": 1, "b": 2}).Len())
7071
}
7172

72-
func testMerge(t *testing.T, f makeF) {
73+
func testCopy(t *testing.T, f makeF) {
7374
tests := []struct {
7475
name string
7576
m1 mapTI
@@ -84,8 +85,8 @@ func testMerge(t *testing.T, f makeF) {
8485
{"from cast map", f(mapT{"a": 1}), Cast(map[string]int{"b": 2}), mapT{"a": 1, "b": 2}},
8586
}
8687
for _, tt := range tests {
87-
t.Run("Merge "+tt.name, func(t *testing.T) {
88-
tt.m1.Merge(tt.m2)
88+
t.Run("Copy "+tt.name, func(t *testing.T) {
89+
tt.m1.Copy(tt.m2)
8990
if !tt.m1.Equal(tt.expected) {
9091
t.Errorf("Merge error. Expected: %q, got %q", tt.expected, tt.m1)
9192
}

safe_map.go

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
//
2020
// Do not make a copy of a SafeMap using the equality operator (=). Use Clone instead.
2121
type SafeMap[K comparable, V any] struct {
22-
sync.RWMutex
22+
mu sync.RWMutex
2323
items StdMap[K, V]
2424
}
2525

@@ -38,20 +38,20 @@ func (m *SafeMap[K, V]) Clear() {
3838
if m.items == nil {
3939
return
4040
}
41-
m.Lock()
41+
m.mu.Lock()
4242
m.items = nil
43-
m.Unlock()
43+
m.mu.Unlock()
4444
}
4545

4646
// Set sets the key to the given value.
4747
func (m *SafeMap[K, V]) Set(k K, v V) {
48-
m.Lock()
48+
m.mu.Lock()
4949
if m.items == nil {
5050
m.items = map[K]V{k: v}
5151
} else {
5252
m.items[k] = v
5353
}
54-
m.Unlock()
54+
m.mu.Unlock()
5555
}
5656

5757
// Get returns the value based on its key. If it does not exist, an empty string will be returned.
@@ -72,19 +72,19 @@ func (m *SafeMap[K, V]) Load(k K) (v V, ok bool) {
7272
if m.items == nil {
7373
return
7474
}
75-
m.RLock()
75+
m.mu.RLock()
7676
if m.items != nil {
7777
v, ok = m.items[k]
7878
}
79-
m.RUnlock()
79+
m.mu.RUnlock()
8080
return
8181
}
8282

8383
// Delete removes the key from the map and returns the value. If the key does not exist, the zero value will be returned.
8484
func (m *SafeMap[K, V]) Delete(k K) (v V) {
85-
m.Lock()
85+
m.mu.Lock()
8686
v = m.items.Delete(k)
87-
m.Unlock()
87+
m.mu.Unlock()
8888
return
8989
}
9090

@@ -94,9 +94,9 @@ func (m *SafeMap[K, V]) Values() (v []V) {
9494
if m.items == nil {
9595
return
9696
}
97-
m.RLock()
97+
m.mu.RLock()
9898
v = m.items.Values()
99-
m.RUnlock()
99+
m.mu.RUnlock()
100100
return
101101
}
102102

@@ -106,9 +106,9 @@ func (m *SafeMap[K, V]) Keys() (keys []K) {
106106
if m.items == nil {
107107
return nil
108108
}
109-
m.RLock()
109+
m.mu.RLock()
110110
keys = m.items.Keys()
111-
m.RUnlock()
111+
m.mu.RUnlock()
112112
return
113113
}
114114

@@ -117,9 +117,9 @@ func (m *SafeMap[K, V]) Len() (l int) {
117117
if m.items == nil {
118118
return
119119
}
120-
m.RLock()
120+
m.mu.RLock()
121121
l = m.items.Len()
122-
m.RUnlock()
122+
m.mu.RUnlock()
123123
return
124124
}
125125

@@ -131,8 +131,8 @@ func (m *SafeMap[K, V]) Range(f func(k K, v V) bool) {
131131
if m == nil || m.items == nil {
132132
return
133133
}
134-
m.RLock()
135-
defer m.RUnlock()
134+
m.mu.RLock()
135+
defer m.mu.RUnlock()
136136
m.items.Range(f)
137137
}
138138

@@ -147,52 +147,52 @@ func (m *SafeMap[K, V]) Copy(in MapI[K, V]) {
147147
if m.items == nil {
148148
m.items = make(map[K]V, in.Len())
149149
}
150-
m.Lock()
151-
defer m.Unlock()
150+
m.mu.Lock()
151+
defer m.mu.Unlock()
152152
m.items.Copy(in)
153153
}
154154

155155
// Equal returns true if all the keys in the given map exist in this map, and the values are the same
156156
func (m *SafeMap[K, V]) Equal(m2 MapI[K, V]) bool {
157-
m.RLock()
158-
defer m.RUnlock()
157+
m.mu.RLock()
158+
defer m.mu.RUnlock()
159159
return m.items.Equal(m2)
160160
}
161161

162162
// MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
163163
func (m *SafeMap[K, V]) MarshalBinary() ([]byte, error) {
164-
m.RLock()
165-
defer m.RUnlock()
164+
m.mu.RLock()
165+
defer m.mu.RUnlock()
166166
return m.items.MarshalBinary()
167167
}
168168

169169
// UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a
170170
// SafeMap.
171171
func (m *SafeMap[K, V]) UnmarshalBinary(data []byte) (err error) {
172-
m.Lock()
173-
defer m.Unlock()
172+
m.mu.Lock()
173+
defer m.mu.Unlock()
174174
return m.items.UnmarshalBinary(data)
175175
}
176176

177177
// MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
178178
func (m *SafeMap[K, V]) MarshalJSON() (out []byte, err error) {
179-
m.RLock()
180-
defer m.RUnlock()
179+
m.mu.RLock()
180+
defer m.mu.RUnlock()
181181
return m.items.MarshalJSON()
182182
}
183183

184184
// UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a SafeMap.
185185
// The JSON must start with an object.
186186
func (m *SafeMap[K, V]) UnmarshalJSON(in []byte) (err error) {
187-
m.Lock()
188-
defer m.Unlock()
187+
m.mu.Lock()
188+
defer m.mu.Unlock()
189189
return m.items.UnmarshalJSON(in)
190190
}
191191

192192
// String outputs the map as a string.
193193
func (m *SafeMap[K, V]) String() string {
194-
m.RLock()
195-
defer m.RUnlock()
194+
m.mu.RLock()
195+
defer m.mu.RUnlock()
196196
return m.items.String()
197197
}
198198

@@ -213,8 +213,8 @@ func (m *SafeMap[K, V]) KeysIter() iter.Seq[K] {
213213
if m.items == nil {
214214
return
215215
}
216-
m.RLock()
217-
defer m.RUnlock()
216+
m.mu.RLock()
217+
defer m.mu.RUnlock()
218218
for k := range m.items {
219219
if !yield(k) {
220220
break
@@ -231,8 +231,8 @@ func (m *SafeMap[K, V]) ValuesIter() iter.Seq[V] {
231231
if m.items == nil {
232232
return
233233
}
234-
m.RLock()
235-
defer m.RUnlock()
234+
m.mu.RLock()
235+
defer m.mu.RUnlock()
236236
for _, v := range m.items {
237237
if !yield(v) {
238238
break
@@ -244,8 +244,8 @@ func (m *SafeMap[K, V]) ValuesIter() iter.Seq[V] {
244244
// Insert adds the values from seq to the map.
245245
// Duplicate keys are overridden.
246246
func (m *SafeMap[K, V]) Insert(seq iter.Seq2[K, V]) {
247-
m.Lock()
248-
defer m.Unlock()
247+
m.mu.Lock()
248+
defer m.mu.Unlock()
249249
for k, v := range seq {
250250
m.items[k] = v
251251
}
@@ -266,15 +266,15 @@ func CollectSafeMap[K comparable, V any](seq iter.Seq2[K, V]) *SafeMap[K, V] {
266266
// the new keys and values are set using ordinary assignment.
267267
func (m *SafeMap[K, V]) Clone() *SafeMap[K, V] {
268268
m1 := new(SafeMap[K, V])
269-
m.RLock()
270-
defer m.RUnlock()
269+
m.mu.RLock()
270+
defer m.mu.RUnlock()
271271
m1.items = m.items.Clone()
272272
return m1
273273
}
274274

275275
// DeleteFunc deletes any key/value pairs for which del returns true.
276276
func (m *SafeMap[K, V]) DeleteFunc(del func(K, V) bool) {
277-
m.Lock()
278-
defer m.Unlock()
277+
m.mu.Lock()
278+
defer m.mu.Unlock()
279279
m.items.DeleteFunc(del)
280280
}

0 commit comments

Comments
 (0)