From f362cad044d5171bedce093ba300eb72b4c79d81 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: Fri, 28 Feb 2025 21:54:21 +0900 Subject: [PATCH] feat(pbuf): add api B() --- pbuf/bytes.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pbuf/bytes.go b/pbuf/bytes.go index 58c99bc..db40e40 100644 --- a/pbuf/bytes.go +++ b/pbuf/bytes.go @@ -15,9 +15,6 @@ type UserBytes[USRDAT any] struct { // BufferItemToBytes convert between *Buffer // and Bytes. -// -// Please notice that Bytes cannnot convert back to -// *Buffer again. func BufferItemToBytes[USRDAT any]( buf *orbyte.Item[UserBuffer[USRDAT]], ) (b UserBytes[USRDAT]) { @@ -28,6 +25,13 @@ func BufferItemToBytes[USRDAT any]( return } +// B directly use inner buf data and USRDAT safely. +func (b UserBytes[USRDAT]) B(f func([]byte, *USRDAT)) { + b.buf.P(func(ub *UserBuffer[USRDAT]) { + f(ub.Buffer.Bytes(), &ub.DAT) + }) +} + // NewBytes alloc sz bytes. func (bufferPool BufferPool[USRDAT]) NewBytes(sz int) (b UserBytes[USRDAT]) { buf := bufferPool.p.New(sz)