-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
35 lines (31 loc) · 754 Bytes
/
Copy pathutil.go
File metadata and controls
35 lines (31 loc) · 754 Bytes
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
package click
import (
"reflect"
"unsafe"
)
// convInto is a shortcut of `(*V)(unsafe.Pointer(u))`, performing raw pointer cast, bypassing static type check.
// Caller should write guard expression like `_ = U(V{})` to perform explicit type-check when compiling.
func convInto[U, V any](u *U) (v *V) {
return (*V)(unsafe.Pointer(u))
}
func tryGetLength(v any) (int, bool) {
switch v := v.(type) {
case interface{ Length() int }:
return v.Length(), true
case interface{ Len() int }:
return v.Len(), true
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Array, reflect.Map, reflect.Slice:
return rv.Len(), true
default:
return 0, false
}
}
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}