-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
36 lines (31 loc) · 1.02 KB
/
pool.go
File metadata and controls
36 lines (31 loc) · 1.02 KB
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
package sync
import "sync"
// Pool is a generic wrapper of [sync.Pool].
type Pool[T any] struct {
pool sync.Pool
}
// New optionally specifies a function to generate a value when [Pool.Get]
// would otherwise have no item to return. It may not be changed concurrently
// with calls to [Pool.Get].
func (p *Pool[T]) New(f func() T) *Pool[T] {
if f != nil {
p.pool.New = func() any { return f() }
} else {
p.pool.New = nil
}
return p
}
// Get removes an arbitrary item from the [Pool] and returns it to the caller.
// Get may choose to ignore the pool and treat it as empty. Callers should not
// assume any relation between values passed to [Pool.Put] and the values
// returned by Get. If Get would be unable to return an item and [Pool.New] was
// called with a non-nil function, Get returns the result of calling this
// function. If no item can be returned, Get returns the zero value.
func (p *Pool[T]) Get() (x T) {
x, _ = p.pool.Get().(T)
return
}
// Put adds x to the Pool.
func (p *Pool[T]) Put(x T) {
p.pool.Put(x)
}