-
Notifications
You must be signed in to change notification settings - Fork 32
/
stringer.go
120 lines (101 loc) · 2.71 KB
/
stringer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package mapmutex
import (
"fmt"
)
// StringerMapMutex is an implementation of mapMutex for the fmt.Stringer conforming types.
type StringerMapMutex interface {
Lock(key fmt.Stringer) Unlocker
TryLock(key fmt.Stringer) (Unlocker, bool)
Keys() []string
}
// stringerLockerImpl is the implementation of StringerMapMutex.
type stringerLockerImpl struct {
mapMux untypedMapMutex
}
func (s stringerLockerImpl) TryLock(key fmt.Stringer) (Unlocker, bool) {
return s.mapMux.TryLock(key.String())
}
// Lock locks on the string.
func (s stringerLockerImpl) Lock(key fmt.Stringer) Unlocker {
return s.mapMux.Lock(key.String())
}
// Keys returns the keys of the map.
func (s stringerLockerImpl) Keys() []string {
var keys []string
for _, key := range s.mapMux.Keys() {
// nolint: forcetypeassert
keys = append(keys, key.(string))
}
return keys
}
// NewStringerMapMutex creates an initialized locker that locks on fmt.String.
func NewStringerMapMutex() StringerMapMutex {
return &stringerLockerImpl{
mapMux: newMapMutex(),
}
}
// StringMapMutex is an implementation of map mutex for string typed values.
type StringMapMutex interface {
Lock(key string) Unlocker
TryLock(key string) (Unlocker, bool)
Keys() []string
}
// stringMutexImpl locks on a string type.
type stringMutexImpl struct {
mapMux untypedMapMutex
}
// NewStringMapMutex creates a map mutex for the string type.
func NewStringMapMutex() StringMapMutex {
return &stringMutexImpl{
mapMux: newMapMutex(),
}
}
// Lock locks ona string value.
func (s stringMutexImpl) Lock(key string) Unlocker {
return s.mapMux.Lock(key)
}
// TryLock attempts to lock on a string value.
func (s stringMutexImpl) TryLock(key string) (Unlocker, bool) {
return s.mapMux.TryLock(key)
}
// Keys returns the keys of the map.
func (s stringMutexImpl) Keys() []string {
keys := []string{}
for _, key := range s.mapMux.Keys() {
// nolint: forcetypeassert
keys = append(keys, key.(string))
}
return keys
}
// IntMapMutex is a map mutex that allows locking on an int.
type IntMapMutex interface {
Lock(key int) Unlocker
TryLock(key int) (Unlocker, bool)
Keys() []int
}
// intMapMux locks on an int.
type intMapMux struct {
mapMux untypedMapMutex
}
func (i intMapMux) TryLock(key int) (Unlocker, bool) {
return i.mapMux.TryLock(key)
}
// Lock locks an int map mux.
func (i intMapMux) Lock(key int) Unlocker {
return i.mapMux.Lock(key)
}
// Keys returns the keys of the map.
func (i intMapMux) Keys() []int {
var keys []int
for _, key := range i.mapMux.Keys() {
// nolint: forcetypeassert
keys = append(keys, key.(int))
}
return keys
}
// NewIntMapMutex creates a map mutex for locking on an integer.
func NewIntMapMutex() IntMapMutex {
return &intMapMux{
mapMux: newMapMutex(),
}
}