Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func Clone[M ~map[K]V, K comparable, V any](m M) M {
// the value in dst will be overwritten by the value associated
// with the key in src.
func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
if dst == nil {
dst = make(map[K]V)
}
for k, v := range src {
dst[k] = v
}
Expand Down
9 changes: 9 additions & 0 deletions src/maps/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ func TestCopy(t *testing.T) {
Copy(make(M1), make(M2))
}

func TestCopyNil(t *testing.T) {
var mnil map[string]int
mc := Copy(mnil, m1)
want := m1
if !Equal(mc, want) {
t.Errorf("Copy result = %v, want %v", mc, want)
}
}

func TestDeleteFunc(t *testing.T) {
mc := Clone(m1)
DeleteFunc(mc, func(int, int) bool { return false })
Expand Down