return t.buf[:t.bufsize]
}
+// must call unlock
+//
+// buf will no modify before unlock
+//
+// modify func(eg Reset) with block until unlock
+//
+// unsafe
+func (t *Buf[T]) GetPureBufRLock() (buf []T, unlock func()) {
+ t.l.RLock()
+ return t.buf[:t.bufsize], t.l.RUnlock
+}
+
func (t *Buf[T]) GetCopyBuf() (buf []T) {
t.l.RLock()
defer t.l.RUnlock()
t.Fatal()
}
}
+
+func Test4(t *testing.T) {
+ var c = New[byte]()
+ var w = make(chan struct{})
+
+ c.Append([]byte("12345"))
+
+ buf, unlock := c.GetPureBufRLock()
+
+ go func() {
+ w <- struct{}{}
+ c.Reset()
+ t.Log(c.Append([]byte("22345")))
+ t.Log(c.buf)
+ w <- struct{}{}
+ }()
+
+ <-w
+
+ if !bytes.Equal(buf, []byte("12345")) {
+ t.Fatal()
+ }
+
+ unlock()
+
+ <-w
+
+ if !bytes.Equal(buf, []byte("22345")) {
+ t.Fatal(buf)
+ }
+}