syncs: add Swap method

To mimic sync.Map.Swap, sync/atomic.Value.Swap, etc.

Updates tailscale/corp#1297

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: If7627da1bce8b552873b21d7e5ebb98904e9a650
This commit is contained in:
Andrew Dunham
2024-03-19 18:22:42 -04:00
parent 6288c9b41e
commit e382e4cee6
2 changed files with 22 additions and 0 deletions

View File

@@ -169,4 +169,15 @@ func TestMap(t *testing.T) {
t.Errorf("Len after Clear want=0 got=%d", m.Len())
}
})
t.Run("Swap", func(t *testing.T) {
var m Map[string, string]
m.Store("hello", "world")
if got, want := m.Swap("hello", "world2"), "world"; got != want {
t.Errorf("got old value %q, want %q", got, want)
}
if got := m.Swap("empty", "foo"); got != "" {
t.Errorf("got old value %q, want empty string", got)
}
})
}