mirror of
https://github.com/fumiama/orbyte.git
synced 2026-06-05 02:00:30 +08:00
fix(pbuf): bytes slice trans
This commit is contained in:
5
item.go
5
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() {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user