diff --git a/types/typemap.go b/types/typemap.go index c6c51e1..7cff256 100644 --- a/types/typemap.go +++ b/types/typemap.go @@ -3,6 +3,7 @@ package types import ( "fmt" "reflect" + "sync" "unsafe" ) @@ -146,19 +147,30 @@ func (m *typemap) serdeOf(x reflect.Type) (serde, bool) { type doublemap[K, V comparable] struct { fromK map[K]V fromV map[V]K + + mu sync.Mutex } func (m *doublemap[K, V]) getK(k K) (V, bool) { + m.mu.Lock() + defer m.mu.Unlock() + v, ok := m.fromK[k] return v, ok } func (m *doublemap[K, V]) getV(v V) (K, bool) { + m.mu.Lock() + defer m.mu.Unlock() + k, ok := m.fromV[v] return k, ok } func (m *doublemap[K, V]) add(k K, v V) V { + m.mu.Lock() + defer m.mu.Unlock() + if m.fromK == nil { m.fromK = make(map[K]V) m.fromV = make(map[V]K)