copy(buf, t.buf[:t.bufsize])
return
}
-
-// *Need to lock when processing buf or Make sure buf only processing by AppendBufCopy
-//
-// *Not use b = make() to avoid pointer change
-func (t *Buf[T]) AppendBufCopy(buf *[]T) {
- t.l.RLock()
- defer t.l.RUnlock()
-
- origin := len(*buf)
- if origin == 0 {
- *buf = make([]T, t.bufsize)
- } else {
- *buf = append(*buf, make([]T, t.bufsize)...)
- }
- copy((*buf)[origin:], t.buf[:t.bufsize])
-}
import (
"bytes"
"testing"
- "unsafe"
)
func TestXxx(t *testing.T) {
t.Fatal()
}
}
-
-func TestXxx3(t *testing.T) {
- var c = New[byte]()
- var b []byte
- var bp = unsafe.Pointer(&b)
- c.Append([]byte("12345"))
- c.Append([]byte("67890"))
- c.AppendBufCopy(&b)
- c.Reset()
- if !bytes.Equal(b, []byte("1234567890")) {
- t.Fatal(string(b))
- }
- b = []byte{}
- c.Append([]byte("abc"))
- c.Append([]byte("defg"))
- c.AppendBufCopy(&b)
- c.Reset()
- if !bytes.Equal(b, []byte("abcdefg")) || unsafe.Pointer(&b) != bp {
- t.Fatal()
- }
- b = b[:0]
- c.Append([]byte("akjsdhfaksdjhf"))
- c.Append([]byte("9834719203857"))
- c.AppendBufCopy(&b)
- c.Reset()
- if !bytes.Equal(b, []byte("akjsdhfaksdjhf9834719203857")) || unsafe.Pointer(&b) != bp {
- t.Fatal()
- }
-}