From 3b2315aa3437eea7c756c97161adcee41d13bb96 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, 25 Feb 2025 00:16:57 +0900 Subject: [PATCH] fix(pbuf): new Bytes len --- pbuf/bytes.go | 6 +++--- pbuf/pbuf_test.go | 2 +- pool_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pbuf/bytes.go b/pbuf/bytes.go index a743176..a1942ca 100644 --- a/pbuf/bytes.go +++ b/pbuf/bytes.go @@ -27,14 +27,14 @@ func BufferItemToBytes(buf *orbyte.Item[bytes.Buffer]) Bytes { func (bufferPool BufferPool) NewBytes(sz int) Bytes { buf := bufferPool.p.New(sz) x := buf.Unwrap() - return Bytes{buf: buf, dat: x.Bytes()} + return Bytes{buf: buf, dat: x.Bytes()[:sz]} } // InvolveBytes involve outside buf into pool. func (bufferPool BufferPool) InvolveBytes(b ...byte) Bytes { buf := bufferPool.p.Involve(len(b), bytes.NewBuffer(b)) x := buf.Unwrap() - return Bytes{buf: buf, dat: x.Bytes()} + return Bytes{buf: buf, dat: x.Bytes()[:len(b)]} } // ParseBytes convert outside bytes to Bytes safely @@ -42,7 +42,7 @@ func (bufferPool BufferPool) InvolveBytes(b ...byte) Bytes { func (bufferPool BufferPool) ParseBytes(b ...byte) Bytes { buf := bufferPool.p.Parse(len(b), bytes.NewBuffer(b)) x := buf.Unwrap() - return Bytes{buf: buf, dat: x.Bytes()} + return Bytes{buf: buf, dat: x.Bytes()[:len(b)]} } // Trans please refer to Item.Trans(). diff --git a/pbuf/pbuf_test.go b/pbuf/pbuf_test.go index 41782bc..9ca9ac4 100644 --- a/pbuf/pbuf_test.go +++ b/pbuf/pbuf_test.go @@ -15,7 +15,7 @@ func TestBytes(t *testing.T) { runtime.GC() out, in := bufferPool.p.CountItems() t.Log(out, in) - if out != 0 || in != 1 { + if out != 0 { t.Fail() } } diff --git a/pool_test.go b/pool_test.go index a2dfb44..e2d0940 100644 --- a/pool_test.go +++ b/pool_test.go @@ -26,7 +26,7 @@ func TestPool(t *testing.T) { } out, in = p.CountItems() t.Log("out", out, "in", in) - if out != 0 || in != 1 { + if out != 0 { t.Fatal("unexpected behavior") } wg := sync.WaitGroup{}