diff --git a/Taskfile.yml b/Taskfile.yml index f52ac52..c1158ab 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -3,7 +3,7 @@ version: "3" vars: - VERSION: 0.2.0 + VERSION: 0.2.1 tasks: bump: diff --git a/gomps/gomps.go b/gomps/gomps.go index 1762f5a..80a0a6c 100644 --- a/gomps/gomps.go +++ b/gomps/gomps.go @@ -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()) } diff --git a/pool.go b/pool.go new file mode 100644 index 0000000..40e30c2 --- /dev/null +++ b/pool.go @@ -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) +}