From 57f71b9f5258512a81dabdebe028300871e00692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Tue, 10 Oct 2023 01:23:56 +0900 Subject: [PATCH] feat(context): add api ExposeCaller --- context.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/context.go b/context.go index 6c29594..11c661d 100644 --- a/context.go +++ b/context.go @@ -4,6 +4,7 @@ import ( "fmt" "reflect" "sync" + "unsafe" "github.com/wdvxdr1123/ZeroBot/message" ) @@ -26,6 +27,11 @@ func (ctx *Ctx) GetMatcher() *Matcher { return ctx.ma } +// ExposeCaller as *T, maybe panic if misused +func ExposeCaller[T any](ctx *Ctx) *T { + return (*T)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ctx.caller), unsafe.Sizeof(uintptr(0))))) +} + // decoder 反射获取的数据 type decoder []dec