Skip to content

Commit

Permalink
add pool
Browse files Browse the repository at this point in the history
  • Loading branch information
delaneyj committed May 20, 2024
1 parent 5dc8e17 commit 8b302a3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version: "3"

vars:
VERSION: 0.2.0
VERSION: 0.2.1

tasks:
bump:
Expand Down
4 changes: 3 additions & 1 deletion gomps/gomps.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ func PREJSON[T any](v T) NODE {
return PRE(SAFE(string(b)))
}

var jsonIndentMarshaller = protojson.MarshalOptions{Indent: " "}

func PREPBJSON(m protoreflect.ProtoMessage) NODE {
b, err := protojson.Marshal(m)
b, err := jsonIndentMarshaller.Marshal(m)
if err != nil {
return TXT(err.Error())
}
Expand Down
29 changes: 29 additions & 0 deletions pool.go
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)
}

0 comments on commit 8b302a3

Please sign in to comment.