1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-08-31 02:45:18 +00:00
arikawa/utils/handler/reflect.go
2023-08-04 14:18:58 -07:00

33 lines
637 B
Go

package handler
import (
"fmt"
"reflect"
"sync"
)
var asserted sync.Map
// assertImpl asserts that ImplT implements T. If it does not, the program
// panics.
func assertImpl[T any, ImplT any]() {
rt := reflect.TypeOf((*T)(nil))
ri := reflect.TypeOf((*ImplT)(nil))
_, checked := asserted.LoadOrStore([2]reflect.Type{rt, ri}, struct{}{})
if checked {
return
}
rt = rt.Elem()
ri = ri.Elem()
if rt.Kind() != reflect.Interface {
panic(fmt.Sprintf("handler: assertImpl: T=%v is not an interface", rt))
}
if !ri.Implements(rt) {
panic(fmt.Sprintf("handler: assertImpl: ImplT=%v does not implement T=%v", ri, rt))
}
}