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()