mirror of
https://github.com/fumiama/orbyte.git
synced 2026-06-05 02:00:30 +08:00
feat: add Ignore to Item
This commit is contained in:
@@ -2,6 +2,9 @@ linters-settings:
|
||||
errcheck:
|
||||
ignore: fmt:.*,io/ioutil:^Read.*
|
||||
ignoretests: true
|
||||
exclude-functions:
|
||||
- (*github.com/fumiama/orbyte.Item).V
|
||||
- (*github.com/fumiama/orbyte.Item).P
|
||||
|
||||
goimports:
|
||||
local-prefixes: github.com/fumiama/orbyte
|
||||
|
||||
18
item.go
18
item.go
@@ -25,6 +25,13 @@ type Item[T any] struct {
|
||||
val T
|
||||
}
|
||||
|
||||
// Ignore marks Item to be independent and will not be
|
||||
// put back.
|
||||
func (b *Item[T]) Ignore() *Item[T] {
|
||||
b.stat.setignored(true)
|
||||
return b
|
||||
}
|
||||
|
||||
// Trans disable inner val being reset by
|
||||
// destroy and return a safe copy of val.
|
||||
//
|
||||
@@ -69,7 +76,7 @@ func (b *Item[T]) HasInvolved() bool {
|
||||
// V use value of the item.
|
||||
//
|
||||
// This operation is safe in function f.
|
||||
func (b *Item[T]) V(f func(T)) {
|
||||
func (b *Item[T]) V(f func(T)) *Item[T] {
|
||||
if b.stat.hasdestroyed() {
|
||||
panic("use after destroy")
|
||||
}
|
||||
@@ -82,12 +89,13 @@ func (b *Item[T]) V(f func(T)) {
|
||||
}
|
||||
f(b.val)
|
||||
runtime.KeepAlive(b)
|
||||
return b
|
||||
}
|
||||
|
||||
// P use pointer value of the item.
|
||||
//
|
||||
// This operation is safe in function f.
|
||||
func (b *Item[T]) P(f func(*T)) {
|
||||
func (b *Item[T]) P(f func(*T)) *Item[T] {
|
||||
if b.stat.hasdestroyed() {
|
||||
panic("use after destroy")
|
||||
}
|
||||
@@ -100,6 +108,7 @@ func (b *Item[T]) P(f func(*T)) {
|
||||
}
|
||||
f(&b.val)
|
||||
runtime.KeepAlive(b)
|
||||
return b
|
||||
}
|
||||
|
||||
// Copy data completely with separated ownership.
|
||||
@@ -129,6 +138,11 @@ func (b *Item[T]) destroybystat(stat status) {
|
||||
var v T
|
||||
b.val = v
|
||||
}
|
||||
if stat.hasignored() { // ignore put
|
||||
runtime.SetFinalizer(b, nil)
|
||||
b.cfg = nil
|
||||
return
|
||||
}
|
||||
b.pool.put(b)
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,16 @@ func (bufferPool BufferPool[USRDAT]) NewBytes(sz int) (b UserBytes[USRDAT]) {
|
||||
return
|
||||
}
|
||||
|
||||
// NewLargeBytes alloc sz bytes without involving.
|
||||
func (bufferPool BufferPool[USRDAT]) NewLargeBytes(sz int) (b UserBytes[USRDAT]) {
|
||||
buf := bufferPool.New(sz).Ignore()
|
||||
b.buf = buf
|
||||
buf.P(func(buf *UserBuffer[USRDAT]) {
|
||||
b.b = buf.Len()
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// InvolveBytes involve outside buf into pool.
|
||||
func (bufferPool BufferPool[USRDAT]) InvolveBytes(p ...byte) (b UserBytes[USRDAT]) {
|
||||
buf := bufferPool.Involve(len(p), bytes.NewBuffer(p))
|
||||
|
||||
@@ -11,6 +11,7 @@ const (
|
||||
statusisbuffered = 1 << iota
|
||||
statusdestroyed
|
||||
statusinsyncop
|
||||
statushasignored
|
||||
)
|
||||
|
||||
type status uintptr
|
||||
@@ -96,3 +97,11 @@ func (c *status) setdestroyed(v bool) {
|
||||
func (c *status) setinsyncop(v bool) bool {
|
||||
return c.setboolunique(v, statusinsyncop)
|
||||
}
|
||||
|
||||
func (c *status) hasignored() bool {
|
||||
return c.loadbool(statushasignored)
|
||||
}
|
||||
|
||||
func (c *status) setignored(v bool) {
|
||||
c.setbool(v, statushasignored)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user