-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.go
310 lines (259 loc) · 7.59 KB
/
misc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package gg
import (
"context"
"database/sql/driver"
)
var (
Indent = ` `
Space = ` `
Newline = "\n"
)
// Does nothing.
func Nop() {}
// Does nothing.
func Nop1[A any](A) {}
// Does nothing.
func Nop2[A, B any](A, B) {}
// Does nothing.
func Nop3[A, B, C any](A, B, C) {}
// Identity function. Returns input as-is.
func Id1[A any](val A) A { return val }
// Identity function. Returns input as-is.
func Id2[A, B any](val0 A, val1 B) (A, B) { return val0, val1 }
// Identity function. Returns input as-is.
func Id3[A, B, C any](val0 A, val1 B, val2 C) (A, B, C) { return val0, val1, val2 }
// Returns a zero value of the given type.
func Zero[A any]() (_ A) { return }
/*
Same as Go's `+` operator, expressed as a generic function. Input type may be
numeric or ~string. When the input type is numeric, this is unchecked and may
overflow. For integers, prefer `Add` whenever possible, which has overflow
checks.
*/
func Plus2[A Plusable](one, two A) A { return one + two }
/*
Variadic version of Go's `+` operator. Input type may be numeric or ~string.
If the input is empty, returns a zero value. Use caution: this has no overflow
checks for numbers. Prefer `Add` for integers.
*/
func Plus[A Plusable](val ...A) A { return Foldz(val, Plus2[A]) }
/*
Shortcut for implementing `driver.Valuer` on `Nullable` types that wrap other
types, such as `Opt`. Mostly for internal use.
*/
func ValueNull[A any, B NullableValGetter[A]](src B) (driver.Value, error) {
if src.IsNull() {
return nil, nil
}
val := src.Get()
impl, _ := AnyNoEscUnsafe(val).(driver.Valuer)
if impl != nil {
return impl.Value()
}
return val, nil
}
/*
Returns true if the given `any` can be usefully converted into a value of the
given type. If the result is true, `src.(A)` doesn't panic. If the output is
false, `src.(A)` panics.
*/
func AnyIs[A any](src any) bool {
_, ok := AnyNoEscUnsafe(src).(A)
return ok
}
/*
Non-asserting interface conversion. Converts the given `any` into the given
type, returning zero value on failure.
*/
func AnyAs[A any](src any) A {
val, _ := AnyNoEscUnsafe(src).(A)
return val
}
/*
Converts the argument to `any` and returns it. Sometimes useful in higher-order
functions.
*/
func ToAny[A any](val A) any { return val }
/*
Uses `context.WithValue` to create a context with the given value, using the
type's nil pointer "(*A)(nil)" as the key.
*/
func CtxSet[A any](ctx context.Context, val A) context.Context {
return context.WithValue(ctx, (*A)(nil), val)
}
/*
Uses `ctx.Value` to get the value of the given type, using the type's nil pointer
"(*A)(nil)" as the key. If the context is nil or doesn't contain the value,
returns zero value and false.
*/
func CtxGot[A any](ctx context.Context) (A, bool) {
if ctx == nil {
return Zero[A](), false
}
val, ok := ctx.Value((*A)(nil)).(A)
return val, ok
}
// Same as `CtxGot` but returns only the boolean.
func CtxHas[A any](ctx context.Context) bool {
_, ok := CtxGot[A](ctx)
return ok
}
/*
Same as `CtxGot` but returns only the resulting value. If value was not found,
output is zero.
*/
func CtxGet[A any](ctx context.Context) A {
val, _ := CtxGot[A](ctx)
return val
}
/*
Short for "iterator". Returns a slice of the given length that can be iterated
by using a `range` loop. Usage:
for range Iter(size) { ... }
for ind := range Iter(size) { ... }
Because `struct{}` is zero-sized, `[]struct{}` is backed by "zerobase" (see Go
source → "runtime/malloc.go") and does not allocate. Loops using this should
compile to approximately the same instructions as "normal" counted loops.
*/
func Iter(size int) []struct{} { return make([]struct{}, size) }
/*
Returns a slice of numbers from `min` to `max`. The range is inclusive at the
start but exclusive at the end: `[min,max)`. If `!(max > min)`, returns nil.
Values must be within the range of the Go type `int`.
*/
func Range[A Int](min, max A) []A {
// We must check this before calling `max-1` to avoid underflow.
if !(max > min) {
return nil
}
return RangeIncl(min, max-1)
}
/*
Returns a slice of numbers from `min` to `max`. The range is inclusive at the
start and at the end: `[min,max]`. If `!(max >= min)`, returns nil. Values must
be within the range of the Go type `int`.
While the exclusive range `[min,max)` implemented by `Range` is more
traditional, this function allows to create a range that includes the maximum
representable value of any given integer type, such as 255 for `uint8`, which
cannot be done with `Range`.
*/
func RangeIncl[A Int](min, max A) []A {
if !(max >= min) {
return nil
}
minInt := NumConv[int](min)
maxInt := NumConv[int](max)
buf := make([]A, (maxInt-minInt)+1)
for ind := range buf {
buf[ind] = A(ind + minInt)
}
return buf
}
// Shortcut for creating range `[0,N)`, exclusive at the end.
func Span[A Int](val A) []A { return Range(0, val) }
/*
Takes a pointer and a fallback value which must be non-zero. If the pointer
destination is zero, sets the fallback and returns true. Otherwise returns
false.
*/
func Fellback[A any](tar *A, fallback A) bool {
if IsZero(fallback) {
panic(Errf(`invalid non-zero fallback %#v`, fallback))
}
if tar == nil {
return false
}
if IsZero(*tar) {
*tar = fallback
return true
}
return false
}
/*
Snapshots the current value at the given pointer and returns a snapshot
that can restore this value. Usage:
defer Snap(&somePtr).Done()
somePtr.SomeField = someValue
*/
func Snap[A any](ptr *A) Snapshot[A] { return Snapshot[A]{ptr, *ptr} }
/*
Snapshots the previous value, sets the next value, and returns a snapshot
that can restore the previous value. Usage:
defer SnapSwap(&somePtr, someVal).Done()
*/
func SnapSwap[A any](ptr *A, next A) Snapshot[A] {
prev := *ptr
*ptr = next
return Snapshot[A]{ptr, prev}
}
// Short for "snapshot". Used by `SnapSwap`.
type Snapshot[A any] struct {
Ptr *A
Val A
}
// If the pointer is non-nil, writes the value to it. See `SnapSwap`.
func (self Snapshot[_]) Done() {
if self.Ptr != nil {
*self.Ptr = self.Val
}
}
/*
Snapshots the length of the given slice and returns a snapshot that can restore
the previous length. Usage:
defer SnapSlice(&somePtr).Done()
*/
func SnapSlice[Slice ~[]Elem, Elem any](ptr *Slice) SliceSnapshot[Slice, Elem] {
return SliceSnapshot[Slice, Elem]{ptr, PtrLen(ptr)}
}
/*
Analogous to `Snapshot`, but instead of storing a value, stores a length.
When done, reverts the referenced slice to the given length.
*/
type SliceSnapshot[Slice ~[]Elem, Elem any] struct {
Ptr *Slice
Len int
}
/*
Analogous to `Snapshot.Done`. Reverts the referenced slice to `self.Len` while
keeping the capacity.
*/
func (self SliceSnapshot[_, _]) Done() {
if self.Ptr != nil {
*self.Ptr = (*self.Ptr)[:self.Len]
}
}
// Shortcut for making a pseudo-tuple with two elements.
func Tuple2[A, B any](valA A, valB B) Tup2[A, B] {
return Tup2[A, B]{valA, valB}
}
// Represents a pseudo-tuple with two elements.
type Tup2[A, B any] struct {
A A
B B
}
// Converts the pseudo-tuple to a proper Go tuple.
func (self Tup2[A, B]) Get() (A, B) { return self.A, self.B }
// Shortcut for making a pseudo-tuple with three elements.
func Tuple3[A, B, C any](valA A, valB B, valC C) Tup3[A, B, C] {
return Tup3[A, B, C]{valA, valB, valC}
}
// Represents a pseudo-tuple with three elements.
type Tup3[A, B, C any] struct {
A A
B B
C C
}
// Converts the pseudo-tuple to a proper Go tuple.
func (self Tup3[A, B, C]) Get() (A, B, C) { return self.A, self.B, self.C }
/*
Makes a zero value of the given type, passes it to the given mutator functions
by pointer, and returns the modified value. Nil functions are ignored.
*/
func With[A any](funs ...func(*A)) (out A) {
for _, fun := range funs {
if fun != nil {
fun(&out)
}
}
return
}