-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
33 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
version: "3" | ||
|
||
vars: | ||
VERSION: 0.2.0 | ||
VERSION: 0.2.1 | ||
|
||
tasks: | ||
bump: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package toolbelt | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// A Pool is a generic wrapper around a sync.Pool. | ||
type Pool[T any] struct { | ||
pool sync.Pool | ||
} | ||
|
||
// New creates a new Pool with the provided new function. | ||
// | ||
// The equivalent sync.Pool construct is "sync.Pool{New: fn}" | ||
func New[T any](fn func() T) Pool[T] { | ||
return Pool[T]{ | ||
pool: sync.Pool{New: func() interface{} { return fn() }}, | ||
} | ||
} | ||
|
||
// Get is a generic wrapper around sync.Pool's Get method. | ||
func (p *Pool[T]) Get() T { | ||
return p.pool.Get().(T) | ||
} | ||
|
||
// Get is a generic wrapper around sync.Pool's Put method. | ||
func (p *Pool[T]) Put(x T) { | ||
p.pool.Put(x) | ||
} |