Skip to content

Commit 2bd0972

Browse files
committed
use maps.Clone() in Go 1.21+
1 parent 006faa1 commit 2bd0972

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

helper.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !go1.21
2+
3+
package mapset
4+
5+
func mapclone[T comparable](m map[T]struct{}) map[T]struct{} {
6+
c := make(map[T]struct{}, len(m))
7+
for k := range m {
8+
c[k] = struct{}{}
9+
}
10+
return c
11+
}

helper_1_21.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build go1.21
2+
3+
package mapset
4+
5+
import "maps"
6+
7+
func mapclone[T comparable](m map[T]struct{}) map[T]struct{} {
8+
return maps.Clone(m)
9+
}

threadunsafe.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ func (s *threadUnsafeSet[T]) Clear() {
8282
}
8383

8484
func (s *threadUnsafeSet[T]) Clone() Set[T] {
85-
clonedSet := newThreadUnsafeSetWithSize[T](s.Cardinality())
86-
for elem := range *s {
87-
clonedSet.add(elem)
88-
}
89-
return clonedSet
85+
t := threadUnsafeSet[T](mapclone(*s))
86+
return &t
9087
}
9188

9289
func (s *threadUnsafeSet[T]) Contains(v ...T) bool {

0 commit comments

Comments
 (0)