-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
75 lines (66 loc) · 1.53 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package xsync
import "sync"
// Pool is a type-asserted variant of sync.Pool.
type Pool[T any] struct {
sync.Pool
Reset func(*T)
}
// Get analogous to (*sync.Pool).Get, but already type asserted.
func (p *Pool[T]) Get() T {
return p.Pool.Get().(T)
}
// Put analogous to (*sync.Pool).Put, but already type asserted,
// and with Reset function called.
func (p *Pool[T]) Put(x T) {
if p.Reset != nil {
p.Reset(&x)
}
p.Pool.Put(x)
}
// PoolR is a wrapper for a Pool to wrap/unwrap automatically
// the Releasable structure.
type PoolR[T any] struct {
Pool Pool[T]
}
// Get analogous to (*sync.Pool).Get, but already type asserted.
func (p *PoolR[T]) Get() Releasable[T] {
return Releasable[T]{
Value: p.Pool.Get(),
Pool: &p.Pool,
}
}
// NewPoolR returns a new Pool which returns value with method
// Release to put then back to the Pool (after use).
func NewPoolR[T any](
initFunc func(*T),
resetFunc func(*T),
) *PoolR[T] {
p := &PoolR[T]{}
p.Pool.New = func() any {
var r T
if initFunc != nil {
initFunc(&r)
}
return r
}
if resetFunc != nil {
p.Pool.Reset = func(in *T) {
resetFunc(in)
}
}
return p
}
// Releasable is a wrapper for a value to make it releasable
// pack to its Pool.
type Releasable[T any] struct {
// TODO: make the field embedded when
// `type Value[T any] = T` will be permitted (Go1.22?).
//
// Partially related ticket: https://github.com/golang/go/issues/46477
Value T
Pool *Pool[T]
}
// Release puts the value back to the Pool.
func (v Releasable[T]) Release() {
v.Pool.Put(v.Value)
}