From c0ce374f0fefebec5f7526218bf4743590a63eec Mon Sep 17 00:00:00 2001 From: Braden Walker Date: Sun, 10 Sep 2023 22:35:23 -0400 Subject: [PATCH] add (xsync.Map).{CompareAndSwap,CompareAndDelete,Swap} --- xsync/xsync.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xsync/xsync.go b/xsync/xsync.go index ae83783..445862a 100644 --- a/xsync/xsync.go +++ b/xsync/xsync.go @@ -230,6 +230,12 @@ type Map[K comparable, V any] struct { m sync.Map } +func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool) { + return m.m.CompareAndDelete(key, old) +} +func (m *Map[K, V]) CompareAndSwap(key K, old V, new V) (deleted bool) { + return m.m.CompareAndSwap(key, old, new) +} func (m *Map[K, V]) Delete(key K) { m.m.Delete(key) } @@ -261,6 +267,10 @@ func (m *Map[K, V]) Range(f func(key K, value V) bool) { func (m *Map[K, V]) Store(key K, value V) { m.m.Store(key, value) } +func (m *Map[K, V]) Swap(key K, value V) (previous V, loaded bool) { + previousUntyped, loaded := m.m.Swap(key, value) + return previousUntyped.(V), loaded +} // Pool is a typesafe wrapper over sync.Pool. type Pool[T any] struct {