Skip to content

Commit 4a55095

Browse files
adonovangopherbot
authored andcommitted
slices,maps: delegate to go1.21 functions of the same name
This change replaces the body of almost every function in the package with a call to its corresponding std function of the same name and--assumedly--semantics. If proposal golang/go#32816 is accepted, we will annotate each function with a "//go:fix inline" comment so that automated tooling such as golang.org/x/tools/internal/refactor/inline/analyzer can automatically inline the wrappers. (This CL is deemed to fix golang/go#70717 because it reduces maps.Clear to the same status as all the other maps functions.) Updates golang/go#32816 Updates golang/go#70815 Fixes golang/go#70717 Change-Id: I85246df07f903af97673b80024acdcae057b9f63 Reviewed-on: https://go-review.googlesource.com/c/exp/+/635680 Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 1829a12 commit 4a55095

File tree

9 files changed

+75
-1577
lines changed

9 files changed

+75
-1577
lines changed

constraints/constraints.go

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type Complex interface {
4545
// that supports the operators < <= >= >.
4646
// If future releases of Go add new ordered types,
4747
// this constraint will be modified to include them.
48+
//
49+
// This type is redundant since Go 1.21 introduced [cmp.Ordered].
4850
type Ordered interface {
4951
Integer | Float | ~string
5052
}

maps/maps.go

+20-38
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,20 @@
55
// Package maps defines various functions useful with maps of any type.
66
package maps
77

8+
import "maps"
9+
10+
// TODO(adonovan): when https://go.dev/issue/32816 is accepted, all of
11+
// these functions except Keys and Values should be annotated
12+
// (provisionally with "//go:fix inline") so that tools can safely and
13+
// automatically replace calls to exp/maps with calls to std maps by
14+
// inlining them.
15+
816
// Keys returns the keys of the map m.
917
// The keys will be in an indeterminate order.
1018
func Keys[M ~map[K]V, K comparable, V any](m M) []K {
19+
// The simplest true equivalent using std is:
20+
// return slices.AppendSeq(make([]K, 0, len(m)), maps.Keys(m)).
21+
1122
r := make([]K, 0, len(m))
1223
for k := range m {
1324
r = append(r, k)
@@ -18,6 +29,9 @@ func Keys[M ~map[K]V, K comparable, V any](m M) []K {
1829
// Values returns the values of the map m.
1930
// The values will be in an indeterminate order.
2031
func Values[M ~map[K]V, K comparable, V any](m M) []V {
32+
// The simplest true equivalent using std is:
33+
// return slices.AppendSeq(make([]V, 0, len(m)), maps.Values(m)).
34+
2135
r := make([]V, 0, len(m))
2236
for _, v := range m {
2337
r = append(r, v)
@@ -28,67 +42,35 @@ func Values[M ~map[K]V, K comparable, V any](m M) []V {
2842
// Equal reports whether two maps contain the same key/value pairs.
2943
// Values are compared using ==.
3044
func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool {
31-
if len(m1) != len(m2) {
32-
return false
33-
}
34-
for k, v1 := range m1 {
35-
if v2, ok := m2[k]; !ok || v1 != v2 {
36-
return false
37-
}
38-
}
39-
return true
45+
return maps.Equal(m1, m2)
4046
}
4147

4248
// EqualFunc is like Equal, but compares values using eq.
4349
// Keys are still compared with ==.
4450
func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool {
45-
if len(m1) != len(m2) {
46-
return false
47-
}
48-
for k, v1 := range m1 {
49-
if v2, ok := m2[k]; !ok || !eq(v1, v2) {
50-
return false
51-
}
52-
}
53-
return true
51+
return maps.EqualFunc(m1, m2, eq)
5452
}
5553

5654
// Clear removes all entries from m, leaving it empty.
5755
func Clear[M ~map[K]V, K comparable, V any](m M) {
58-
for k := range m {
59-
delete(m, k)
60-
}
56+
clear(m)
6157
}
6258

6359
// Clone returns a copy of m. This is a shallow clone:
6460
// the new keys and values are set using ordinary assignment.
6561
func Clone[M ~map[K]V, K comparable, V any](m M) M {
66-
// Preserve nil in case it matters.
67-
if m == nil {
68-
return nil
69-
}
70-
r := make(M, len(m))
71-
for k, v := range m {
72-
r[k] = v
73-
}
74-
return r
62+
return maps.Clone(m)
7563
}
7664

7765
// Copy copies all key/value pairs in src adding them to dst.
7866
// When a key in src is already present in dst,
7967
// the value in dst will be overwritten by the value associated
8068
// with the key in src.
8169
func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2) {
82-
for k, v := range src {
83-
dst[k] = v
84-
}
70+
maps.Copy(dst, src)
8571
}
8672

8773
// DeleteFunc deletes any key/value pairs from m for which del returns true.
8874
func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool) {
89-
for k, v := range m {
90-
if del(k, v) {
91-
delete(m, k)
92-
}
93-
}
75+
maps.DeleteFunc(m, del)
9476
}

slices/cmp.go

-44
This file was deleted.

0 commit comments

Comments
 (0)