summaryrefslogtreecommitdiff
path: root/pool.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-26 12:23:15 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-26 12:46:07 -0700
commit716dd31f7cf52d9772fd4ed687f9cdc921443a35 (patch)
treeded6651a3b6e240f60759465af9abccd52d5984d /pool.go
parent774689062b4ac1921434a6c7a2ac78b8f29ac85a (diff)
Set up as a separate repov0.0.1
Diffstat (limited to 'pool.go')
-rw-r--r--pool.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/pool.go b/pool.go
new file mode 100644
index 0000000..c196085
--- /dev/null
+++ b/pool.go
@@ -0,0 +1,33 @@
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package typedsync
+
+import (
+ "sync"
+)
+
+type Pool[T any] struct {
+ New func() T
+
+ inner sync.Pool
+}
+
+func (p *Pool[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 *Pool[T]) Put(val T) {
+ p.inner.Put(val)
+}