From dc586c25206a4b41a7d8c505e3700f5704134228 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 5 Jan 2023 19:20:25 -0700 Subject: containers: Add SyncValue and SyncPool types --- lib/containers/syncpool.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/containers/syncpool.go (limited to 'lib/containers/syncpool.go') 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 +// +// 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) +} -- cgit v1.2.3-2-g168b