aboutsummaryrefslogtreecommitdiff
path: root/util.go
diff options
context:
space:
mode:
Diffstat (limited to 'util.go')
-rw-r--r--util.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/util.go b/util.go
index ec2a3dd..47a785a 100644
--- a/util.go
+++ b/util.go
@@ -3,6 +3,20 @@
package core
+// MapKeys returns a slice containing all the keys of the map supplied.
+// It basically is https://pkg.go.dev/golang.org/x/exp/maps#Keys, but
+// that package is still unstable.
+func MapKeys[T ~map[K]V, K comparable, V any](m T) []K {
+ if len(m) == 0 {
+ return nil
+ }
+ ret := make([]K, 0, len(m))
+ for k := range m {
+ ret = append(ret, k)
+ }
+ return ret
+}
+
// Must panics if err is not nil. It returns val otherwise.
func Must[T any](val T, err error) T {
if err != nil {