summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-26 12:52:54 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-26 12:52:54 -0700
commit4d7ce9f864409dcfb84d2e027df0022076946583 (patch)
treec4313520e8008e85c147b6d6864f51046e7ed450
parent716dd31f7cf52d9772fd4ed687f9cdc921443a35 (diff)
Map: Work around https://github.com/golang/go/issues/56548
-rw-r--r--map.go18
-rw-r--r--map_go118.go9
-rw-r--r--map_go120.go9
3 files changed, 35 insertions, 1 deletions
diff --git a/map.go b/map.go
index 5b53748..6bb1170 100644
--- a/map.go
+++ b/map.go
@@ -8,7 +8,23 @@ import (
"sync"
)
-type Map[K comparable, V any] struct {
+// Map is a type-safe equivalent of the standard library's sync.Map.
+//
+// With versions of Go prior to Go 1.20, Map is specified too loosely,
+// as
+//
+// Map[K any, V any]
+//
+// while with Go 1.20 and later, Map is specified as
+//
+// Map[K comparable, V any]
+//
+// This is because with Go versions prior to 1.20, 'comparable' was
+// overly strict, disallowing many types that are valid map-keys (see
+// https://github.com/golang/go/issues/56548). The type used as K in
+// a Map older versions of Go must be a valid map-key type, even
+// though the type specification of Map does not enforce that.
+type Map[K mapkey, V any] struct {
inner sync.Map
}
diff --git a/map_go118.go b/map_go118.go
new file mode 100644
index 0000000..5446c88
--- /dev/null
+++ b/map_go118.go
@@ -0,0 +1,9 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+//go:build !go1.20
+
+package typedsync
+
+type mapkey = any
diff --git a/map_go120.go b/map_go120.go
new file mode 100644
index 0000000..0d4ff5b
--- /dev/null
+++ b/map_go120.go
@@ -0,0 +1,9 @@
+// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+//go:build go1.20
+
+package typedsync
+
+type mapkey = comparable