mirror of
https://github.com/siderolabs/omni.git
synced 2026-05-11 01:26:15 +02:00
Bump copyright for conformance to 2026 Signed-off-by: Edward Sammut Alessi <edward.sammutalessi@siderolabs.com>
34 lines
803 B
Go
34 lines
803 B
Go
// Copyright (c) 2026 Sidero Labs, Inc.
|
|
//
|
|
// Use of this software is governed by the Business Source License
|
|
// included in the LICENSE file.
|
|
|
|
// Package pool provides a generic pool for a specific type.
|
|
package pool
|
|
|
|
import "sync"
|
|
|
|
// Pool is a generic pool for a specific type.
|
|
// The New field is a function that creates a new instance of the type.
|
|
// It should be set before calling Get.
|
|
//
|
|
//nolint:govet
|
|
type Pool[T any] struct {
|
|
New func() *T
|
|
|
|
once sync.Once
|
|
pool sync.Pool
|
|
}
|
|
|
|
// Get returns a new instance of the type from the pool.
|
|
func (p *Pool[T]) Get() *T {
|
|
p.once.Do(func() { p.pool.New = func() any { return p.New() } })
|
|
|
|
return p.pool.Get().(*T) //nolint:forcetypeassert,errcheck
|
|
}
|
|
|
|
// Put returns an instance of the type to the pool.
|
|
func (p *Pool[T]) Put(x *T) {
|
|
p.pool.Put(x)
|
|
}
|