Debian Codegraph @

pathrs-lite/internal/gocompat/gocompat_generics_unsupported.go

raw
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 //go:build linux && !go1.21
4
5 // Copyright (C) 2021, 2022 The Go Authors. All rights reserved.
6 // Copyright (C) 2024-2025 SUSE LLC. All rights reserved.
7 // Use of this source code is governed by a BSD-style
8 // license that can be found in the LICENSE.BSD file.
9
10 package gocompat
11
12 import (
13 "sync"
14 )
15
16 // These are very minimal implementations of functions that appear in Go 1.21's
17 // stdlib, included so that we can build on older Go versions. Most are
18 // borrowed directly from the stdlib, and a few are modified to be "obviously
19 // correct" without needing to copy too many other helpers.
20
21 // clearSlice is equivalent to Go 1.21's builtin clear.
22 // Copied from the Go 1.24 stdlib implementation.
23 func clearSlice[S ~[]E, E any](slice S) {
24 var zero E
25 for i := range slice {
26 slice[i] = zero
27 }
28 }
29
30 // slicesIndexFunc is equivalent to Go 1.21's slices.IndexFunc.
31 // Copied from the Go 1.24 stdlib implementation.
32 func slicesIndexFunc[S ~[]E, E any](s S, f func(E) bool) int {
33 for i := range s {
34 if f(s[i]) {
35 return i
36 }
37 }
38 return -1
39 }
40
41 // SlicesDeleteFunc is equivalent to Go 1.21's slices.DeleteFunc.
42 // Copied from the Go 1.24 stdlib implementation.
43 func SlicesDeleteFunc[S ~[]E, E any](s S, del func(E) bool) S {
44 i := slicesIndexFunc(s, del)
45 if i == -1 {
46 return s
47 }
48 // Don't start copying elements until we find one to delete.
49 for j := i + 1; j < len(s); j++ {
50 if v := s[j]; !del(v) {
51 s[i] = v
52 i++
53 }
54 }
55 clearSlice(s[i:]) // zero/nil out the obsolete elements, for GC
56 return s[:i]
57 }
58
59 // SlicesContains is equivalent to Go 1.21's slices.Contains.
60 // Similar to the stdlib slices.Contains, except that we don't have
61 // slices.Index so we need to use slices.IndexFunc for this non-Func helper.
62 func SlicesContains[S ~[]E, E comparable](s S, v E) bool {
63 return slicesIndexFunc(s, func(e E) bool { return e == v }) >= 0
64 }
65
66 // SlicesClone is equivalent to Go 1.21's slices.Clone.
67 // Copied from the Go 1.24 stdlib implementation.
68 func SlicesClone[S ~[]E, E any](s S) S {
69 // Preserve nil in case it matters.
70 if s == nil {
71 return nil
72 }
73 return append(S([]E{}), s...)
74 }
75
76 // SyncOnceValue is equivalent to Go 1.21's sync.OnceValue.
77 // Copied from the Go 1.25 stdlib implementation.
78 func SyncOnceValue[T any](f func() T) func() T {
79 // Use a struct so that there's a single heap allocation.
80 d := struct {
81 f func() T
82 once sync.Once
83 valid bool
84 p any
85 result T
86 }{
87 f: f,
88 }
89 return func() T {
90 d.once.Do(func() {
91 defer func() {
92 d.f = nil
93 d.p = recover()
94 if !d.valid {
95 panic(d.p)
96 }
97 }()
98 d.result = d.f()
99 d.valid = true
100 })
101 if !d.valid {
102 panic(d.p)
103 }
104 return d.result
105 }
106 }
107
108 // SyncOnceValues is equivalent to Go 1.21's sync.OnceValues.
109 // Copied from the Go 1.25 stdlib implementation.
110 func SyncOnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2) {
111 // Use a struct so that there's a single heap allocation.
112 d := struct {
113 f func() (T1, T2)
114 once sync.Once
115 valid bool
116 p any
117 r1 T1
118 r2 T2
119 }{
120 f: f,
121 }
122 return func() (T1, T2) {
123 d.once.Do(func() {
124 defer func() {
125 d.f = nil
126 d.p = recover()
127 if !d.valid {
128 panic(d.p)
129 }
130 }()
131 d.r1, d.r2 = d.f()
132 d.valid = true
133 })
134 if !d.valid {
135 panic(d.p)
136 }
137 return d.r1, d.r2
138 }
139 }
140
141 // CmpOrdered is equivalent to Go 1.21's cmp.Ordered generic type definition.
142 // Copied from the Go 1.25 stdlib implementation.
143 type CmpOrdered interface {
144 ~int | ~int8 | ~int16 | ~int32 | ~int64 |
145 ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
146 ~float32 | ~float64 |
147 ~string
148 }
149
150 // isNaN reports whether x is a NaN without requiring the math package.
151 // This will always return false if T is not floating-point.
152 // Copied from the Go 1.25 stdlib implementation.
153 func isNaN[T CmpOrdered](x T) bool {
154 return x != x
155 }
156
157 // CmpCompare is equivalent to Go 1.21's cmp.Compare.
158 // Copied from the Go 1.25 stdlib implementation.
159 func CmpCompare[T CmpOrdered](x, y T) int {
160 xNaN := isNaN(x)
161 yNaN := isNaN(y)
162 if xNaN {
163 if yNaN {
164 return 0
165 }
166 return -1
167 }
168 if yNaN {
169 return +1
170 }
171 if x < y {
172 return -1
173 }
174 if x > y {
175 return +1
176 }
177 return 0
178 }
179
180 // Max2 is equivalent to Go 1.21's max builtin for two parameters.
181 func Max2[T CmpOrdered](x, y T) T {
182 m := x
183 if y > m {
184 m = y
185 }
186 return m
187 }