1
0
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:
源文雨
2025-05-12 23:59:04 +09:00
parent 765c970d70
commit d33f1ca53e
4 changed files with 38 additions and 2 deletions

View File

@@ -2,6 +2,9 @@ linters-settings:
errcheck: errcheck:
ignore: fmt:.*,io/ioutil:^Read.* ignore: fmt:.*,io/ioutil:^Read.*
ignoretests: true ignoretests: true
exclude-functions:
- (*github.com/fumiama/orbyte.Item).V
- (*github.com/fumiama/orbyte.Item).P
goimports: goimports:
local-prefixes: github.com/fumiama/orbyte local-prefixes: github.com/fumiama/orbyte

18
item.go
View File

@@ -25,6 +25,13 @@ type Item[T any] struct {
val T 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 // Trans disable inner val being reset by
// destroy and return a safe copy of val. // destroy and return a safe copy of val.
// //
@@ -69,7 +76,7 @@ func (b *Item[T]) HasInvolved() bool {
// V use value of the item. // V use value of the item.
// //
// This operation is safe in function f. // 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() { if b.stat.hasdestroyed() {
panic("use after destroy") panic("use after destroy")
} }
@@ -82,12 +89,13 @@ func (b *Item[T]) V(f func(T)) {
} }
f(b.val) f(b.val)
runtime.KeepAlive(b) runtime.KeepAlive(b)
return b
} }
// P use pointer value of the item. // P use pointer value of the item.
// //
// This operation is safe in function f. // 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() { if b.stat.hasdestroyed() {
panic("use after destroy") panic("use after destroy")
} }
@@ -100,6 +108,7 @@ func (b *Item[T]) P(f func(*T)) {
} }
f(&b.val) f(&b.val)
runtime.KeepAlive(b) runtime.KeepAlive(b)
return b
} }
// Copy data completely with separated ownership. // Copy data completely with separated ownership.
@@ -129,6 +138,11 @@ func (b *Item[T]) destroybystat(stat status) {
var v T var v T
b.val = v b.val = v
} }
if stat.hasignored() { // ignore put
runtime.SetFinalizer(b, nil)
b.cfg = nil
return
}
b.pool.put(b) b.pool.put(b)
} }

View File

@@ -44,6 +44,16 @@ func (bufferPool BufferPool[USRDAT]) NewBytes(sz int) (b UserBytes[USRDAT]) {
return 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. // InvolveBytes involve outside buf into pool.
func (bufferPool BufferPool[USRDAT]) InvolveBytes(p ...byte) (b UserBytes[USRDAT]) { func (bufferPool BufferPool[USRDAT]) InvolveBytes(p ...byte) (b UserBytes[USRDAT]) {
buf := bufferPool.Involve(len(p), bytes.NewBuffer(p)) buf := bufferPool.Involve(len(p), bytes.NewBuffer(p))

View File

@@ -11,6 +11,7 @@ const (
statusisbuffered = 1 << iota statusisbuffered = 1 << iota
statusdestroyed statusdestroyed
statusinsyncop statusinsyncop
statushasignored
) )
type status uintptr type status uintptr
@@ -96,3 +97,11 @@ func (c *status) setdestroyed(v bool) {
func (c *status) setinsyncop(v bool) bool { func (c *status) setinsyncop(v bool) bool {
return c.setboolunique(v, statusinsyncop) return c.setboolunique(v, statusinsyncop)
} }
func (c *status) hasignored() bool {
return c.loadbool(statushasignored)
}
func (c *status) setignored(v bool) {
c.setbool(v, statushasignored)
}