"sync"
)
-type buf[T any] struct {
+type Buf[T any] struct {
maxsize int
newF func() *T
validF func(*T) bool
sync.RWMutex
}
-func New[T any](NewF func() *T, ValidF func(*T) bool, ReuseF func(*T) *T, maxsize int) *buf[T] {
- t := new(buf[T])
+func New[T any](NewF func() *T, ValidF func(*T) bool, ReuseF func(*T) *T, maxsize int) *Buf[T] {
+ t := new(Buf[T])
t.newF = NewF
t.validF = ValidF
t.reuseF = ReuseF
return t
}
-func (t *buf[T]) Trim() {
+func (t *Buf[T]) Trim() {
t.Lock()
defer t.Unlock()
}
}
-func (t *buf[T]) Get() *T {
+func (t *Buf[T]) Get() *T {
t.Lock()
defer t.Unlock()
return t.newF()
}
-func (t *buf[T]) Put(item ...*T) {
+func (t *Buf[T]) Put(item ...*T) {
if len(item) == 0 {
return
}
"unsafe"
)
-type buf[T any] struct {
+type Buf[T any] struct {
maxsize int
bufsize int
modified Modified
t uint64
}
-func New[T any](maxsize ...int) *buf[T] {
- t := new(buf[T])
+func New[T any](maxsize ...int) *Buf[T] {
+ t := new(Buf[T])
if len(maxsize) > 0 {
t.maxsize = maxsize[0]
}
return t
}
-func (t *buf[T]) Clear() {
+func (t *Buf[T]) Clear() {
t.Lock()
defer t.Unlock()
t.buf = nil
t.modified.t += 1
}
-func (t *buf[T]) Size() int {
+func (t *Buf[T]) Size() int {
t.RLock()
defer t.RUnlock()
return t.bufsize
}
-func (t *buf[T]) IsEmpty() bool {
+func (t *Buf[T]) IsEmpty() bool {
t.RLock()
defer t.RUnlock()
return t.bufsize == 0
}
-func (t *buf[T]) Reset() {
+func (t *Buf[T]) Reset() {
t.Lock()
defer t.Unlock()
t.modified.t += 1
}
-func (t *buf[T]) Append(data []T) error {
+func (t *Buf[T]) Append(data []T) error {
t.Lock()
defer t.Unlock()
return nil
}
-func (t *buf[T]) RemoveFront(n int) error {
+func (t *Buf[T]) RemoveFront(n int) error {
if n <= 0 {
return nil
}
return nil
}
-func (t *buf[T]) RemoveBack(n int) error {
+func (t *Buf[T]) RemoveBack(n int) error {
if n <= 0 {
return nil
}
}
// unsafe
-func (t *buf[T]) SetModified() {
+func (t *Buf[T]) SetModified() {
t.Lock()
defer t.Unlock()
t.modified.t += 1
}
-func (t *buf[T]) GetModified() Modified {
+func (t *Buf[T]) GetModified() Modified {
t.RLock()
defer t.RUnlock()
return t.modified
}
-func (t *buf[T]) HadModified(mt Modified) (same bool, err error) {
+func (t *Buf[T]) HadModified(mt Modified) (same bool, err error) {
t.RLock()
defer t.RUnlock()
}
// unsafe
-func (t *buf[T]) GetPureBuf() (buf []T) {
+func (t *Buf[T]) GetPureBuf() (buf []T) {
t.RLock()
defer t.RUnlock()
return t.buf[:t.bufsize]
}
-func (t *buf[T]) GetCopyBuf() (buf []T) {
+func (t *Buf[T]) GetCopyBuf() (buf []T) {
t.RLock()
defer t.RUnlock()