From a0c89117d9af6d50a281a6e645f4553e8541208a Mon Sep 17 00:00:00 2001 From: Milarin Date: Wed, 31 Jul 2024 23:33:50 +0200 Subject: [PATCH] added DoWithError and Clone methods --- map.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/map.go b/map.go index a8d0b59..9f96343 100644 --- a/map.go +++ b/map.go @@ -101,3 +101,24 @@ func (m *Map[K, V]) Do(f func(m map[K]V)) { defer m.mutex.Unlock() f(m.data) } + +// DoWithError calls f with the underlying primitive map and returns its error. +// Be aware that this locks m for write access +// so Do can be used for reading as well as modifiying m. +func (m *Map[K, V]) DoWithError(f func(m map[K]V) error) error { + m.mutex.Lock() + defer m.mutex.Unlock() + return f(m.data) +} + +// Clone returns a copy of the underlying map data +func (m *Map[K, V]) Clone() map[K]V { + m.mutex.RLock() + defer m.mutex.RUnlock() + + c := map[K]V{} + for key, value := range m.data { + c[key] = value + } + return c +}