summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-mangler/v2/method.go
blob: 7a938d6ae3b5b7fd3300ca8ed499234565dcbdf4 (plain)
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
package mangler

import (
	"reflect"
	"unsafe"

	"codeberg.org/gruf/go-xunsafe"
)

var (
	// mangleable type for implement checks.
	mangleableType = reflect.TypeFor[Mangleable]()
)

type Mangleable interface {
	Mangle(dst []byte) []byte
}

// getMethodType returns a *possible* Mangler to handle case
// of a type that implements any known interface{} types, else nil.
func getMethodType(t xunsafe.TypeIter) Mangler {
	switch {
	case t.Type.Implements(mangleableType):
		switch t.Type.Kind() {
		case reflect.Interface:
			return getInterfaceMangleableType(t)
		default:
			return getConcreteMangleableType(t)
		}
	default:
		return nil
	}
}

// getInterfaceMangleableType returns a Mangler to handle case of an interface{}
// type that implements Mangleable{}, i.e. Mangleable{} itself and any superset of.
func getInterfaceMangleableType(t xunsafe.TypeIter) Mangler {
	switch t.Indirect() && !t.IfaceIndir() {
	case true:
		return func(buf []byte, ptr unsafe.Pointer) []byte {
			ptr = *(*unsafe.Pointer)(ptr)
			if ptr == nil || (*xunsafe.Abi_NonEmptyInterface)(ptr).Data == nil {
				buf = append(buf, '0')
				return buf
			}
			v := *(*Mangleable)(ptr)
			buf = append(buf, '1')
			buf = v.Mangle(buf)
			return buf
		}
	case false:
		return func(buf []byte, ptr unsafe.Pointer) []byte {
			if ptr == nil || (*xunsafe.Abi_NonEmptyInterface)(ptr).Data == nil {
				buf = append(buf, '0')
				return buf
			}
			v := *(*Mangleable)(ptr)
			buf = append(buf, '1')
			buf = v.Mangle(buf)
			return buf
		}
	default:
		panic("unreachable")
	}
}

// getConcreteMangleableType returns a Manlger to handle case of concrete
// (i.e. non-interface{}) type that has a Mangleable{} method receiver.
func getConcreteMangleableType(t xunsafe.TypeIter) Mangler {
	itab := xunsafe.GetIfaceITab[Mangleable](t.Type)
	switch {
	case t.Indirect() && !t.IfaceIndir():
		return func(buf []byte, ptr unsafe.Pointer) []byte {
			ptr = *(*unsafe.Pointer)(ptr)
			if ptr == nil {
				buf = append(buf, '0')
				return buf
			}
			v := *(*Mangleable)(xunsafe.PackIface(itab, ptr))
			buf = append(buf, '1')
			buf = v.Mangle(buf)
			return buf
		}
	case t.Type.Kind() == reflect.Pointer && t.Type.Implements(mangleableType):
		// if the interface implementation is received by
		// value type, the pointer type will also support
		// it but it requires an extra dereference check.
		return func(buf []byte, ptr unsafe.Pointer) []byte {
			if ptr == nil {
				buf = append(buf, '0')
				return buf
			}
			v := *(*Mangleable)(xunsafe.PackIface(itab, ptr))
			buf = append(buf, '1')
			buf = v.Mangle(buf)
			return buf
		}
	default:
		return func(buf []byte, ptr unsafe.Pointer) []byte {
			v := *(*Mangleable)(xunsafe.PackIface(itab, ptr))
			buf = v.Mangle(buf)
			return buf
		}
	}
}