From 224f488d8e83a6306529023ca2a67f2b954563a1 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 14:22:31 +0900 Subject: [PATCH] fix(pbuf): bytes slice trans --- item.go | 5 +++++ pbuf/bytes.go | 9 +++++++++ pbuf/pbuf_test.go | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/item.go b/item.go index 9647481..05117a3 100644 --- a/item.go +++ b/item.go @@ -41,6 +41,11 @@ func (b *Item[T]) Trans() (tb *Item[T]) { return tb } +// IsTrans whether this item has been marked as trans. +func (b *Item[T]) IsTrans() bool { + return b.stat.isintrans() +} + // Unwrap use value of the item func (b *Item[T]) Unwrap() T { if b.stat.hasdestroyed() { diff --git a/pbuf/bytes.go b/pbuf/bytes.go index 9d271d3..80ec79b 100644 --- a/pbuf/bytes.go +++ b/pbuf/bytes.go @@ -94,6 +94,9 @@ func (b Bytes) Copy() (cb Bytes) { // SliceFrom dat[from:] with Ref. func (b Bytes) SliceFrom(from int) Bytes { + if b.buf.IsTrans() { + return InvolveBytes(b.dat[from:]...) + } nb := b.Ref() nb.dat = b.dat[from:] return nb @@ -101,6 +104,9 @@ func (b Bytes) SliceFrom(from int) Bytes { // SliceTo dat[:to] with Ref. func (b Bytes) SliceTo(to int) Bytes { + if b.buf.IsTrans() { + return InvolveBytes(b.dat[:to]...) + } nb := b.Ref() nb.dat = b.dat[:to] return nb @@ -108,6 +114,9 @@ func (b Bytes) SliceTo(to int) Bytes { // Slice dat[from:to] with Ref. func (b Bytes) Slice(from, to int) Bytes { + if b.buf.IsTrans() { + return InvolveBytes(b.dat[from:to]...) + } nb := b.Ref() nb.dat = b.dat[from:to] return nb diff --git a/pbuf/pbuf_test.go b/pbuf/pbuf_test.go index 9ca9ac4..fff855f 100644 --- a/pbuf/pbuf_test.go +++ b/pbuf/pbuf_test.go @@ -10,7 +10,7 @@ func TestBytes(t *testing.T) { for i := 0; i < 4096; i++ { b := NewBytes(i) rand.Read(b.Bytes()) - b.Destroy() + b.Trans().SliceFrom(0).SliceTo(i).Destroy() } runtime.GC() out, in := bufferPool.p.CountItems()