diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-05 19:20:25 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-25 17:28:49 -0700 |
commit | dc586c25206a4b41a7d8c505e3700f5704134228 (patch) | |
tree | 43b1299be580366e8ae819e2f6cfefce3440d915 /lib/containers/syncpool.go | |
parent | dcd67db108bec3a4133542f02fe91faaa0681aa3 (diff) |
containers: Add SyncValue and SyncPool types
Diffstat (limited to 'lib/containers/syncpool.go')
-rw-r--r-- | lib/containers/syncpool.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/containers/syncpool.go b/lib/containers/syncpool.go new file mode 100644 index 0000000..cb5398d --- /dev/null +++ b/lib/containers/syncpool.go @@ -0,0 +1,33 @@ +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package containers + +import ( + "sync" +) + +type SyncPool[T any] struct { + New func() T + + inner sync.Pool +} + +func (p *SyncPool[T]) Get() (val T, ok bool) { + _val := p.inner.Get() + switch { + case _val != nil: + //nolint:forcetypeassert // Typed wrapper around untyped lib. + return _val.(T), true + case p.New != nil: + return p.New(), true + default: + var zero T + return zero, false + } +} + +func (p *SyncPool[T]) Put(val T) { + p.inner.Put(val) +} |