1
0
mirror of https://github.com/fumiama/orbyte.git synced 2026-06-16 08:34:04 +08:00

fix(pbuf): bytes slice trans

This commit is contained in:
源文雨
2025-02-25 14:22:31 +09:00
parent 8c5e0cfad0
commit 224f488d8e
3 changed files with 15 additions and 1 deletions

View File

@@ -41,6 +41,11 @@ func (b *Item[T]) Trans() (tb *Item[T]) {
return tb 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 // Unwrap use value of the item
func (b *Item[T]) Unwrap() T { func (b *Item[T]) Unwrap() T {
if b.stat.hasdestroyed() { if b.stat.hasdestroyed() {

View File

@@ -94,6 +94,9 @@ func (b Bytes) Copy() (cb Bytes) {
// SliceFrom dat[from:] with Ref. // SliceFrom dat[from:] with Ref.
func (b Bytes) SliceFrom(from int) Bytes { func (b Bytes) SliceFrom(from int) Bytes {
if b.buf.IsTrans() {
return InvolveBytes(b.dat[from:]...)
}
nb := b.Ref() nb := b.Ref()
nb.dat = b.dat[from:] nb.dat = b.dat[from:]
return nb return nb
@@ -101,6 +104,9 @@ func (b Bytes) SliceFrom(from int) Bytes {
// SliceTo dat[:to] with Ref. // SliceTo dat[:to] with Ref.
func (b Bytes) SliceTo(to int) Bytes { func (b Bytes) SliceTo(to int) Bytes {
if b.buf.IsTrans() {
return InvolveBytes(b.dat[:to]...)
}
nb := b.Ref() nb := b.Ref()
nb.dat = b.dat[:to] nb.dat = b.dat[:to]
return nb return nb
@@ -108,6 +114,9 @@ func (b Bytes) SliceTo(to int) Bytes {
// Slice dat[from:to] with Ref. // Slice dat[from:to] with Ref.
func (b Bytes) Slice(from, to int) Bytes { func (b Bytes) Slice(from, to int) Bytes {
if b.buf.IsTrans() {
return InvolveBytes(b.dat[from:to]...)
}
nb := b.Ref() nb := b.Ref()
nb.dat = b.dat[from:to] nb.dat = b.dat[from:to]
return nb return nb

View File

@@ -10,7 +10,7 @@ func TestBytes(t *testing.T) {
for i := 0; i < 4096; i++ { for i := 0; i < 4096; i++ {
b := NewBytes(i) b := NewBytes(i)
rand.Read(b.Bytes()) rand.Read(b.Bytes())
b.Destroy() b.Trans().SliceFrom(0).SliceTo(i).Destroy()
} }
runtime.GC() runtime.GC()
out, in := bufferPool.p.CountItems() out, in := bufferPool.p.CountItems()