Feat[pool/strings]: More Must functions

This commit is contained in:
kayos@tcp.direct 2022-10-02 23:45:48 -07:00
parent f8d9462a66
commit 2e72a70838
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
2 changed files with 108 additions and 62 deletions

View File

@ -5,9 +5,8 @@ import (
"sync"
)
type String struct {
*strings.Builder
o *sync.Once
type StringFactory struct {
pool *sync.Pool
}
// NewStringFactory creates a new strings.Builder pool.
@ -19,6 +18,37 @@ func NewStringFactory() StringFactory {
}
}
// Put returns a strings.Builder back into to the pool after resetting it.
func (sf StringFactory) Put(buf *String) error {
var err = ErrBufferReturned
buf.o.Do(func() {
_ = buf.Reset()
sf.pool.Put(buf.Builder)
buf.Builder = nil
err = nil
})
return err
}
func (sf StringFactory) MustPut(buf *String) {
if err := sf.Put(buf); err != nil {
panic(err)
}
}
// Get returns a strings.Builder from the pool.
func (sf StringFactory) Get() *String {
return &String{
sf.pool.Get().(*strings.Builder),
&sync.Once{},
}
}
type String struct {
*strings.Builder
o *sync.Once
}
func (s String) String() string {
if s.Builder == nil {
return ""
@ -26,6 +56,13 @@ func (s String) String() string {
return s.Builder.String()
}
func (s String) MustString() string {
if s.Builder == nil {
panic(ErrBufferReturned)
}
return s.Builder.String()
}
func (s String) Reset() error {
if s.Builder == nil {
return ErrBufferReturned
@ -34,6 +71,13 @@ func (s String) Reset() error {
return nil
}
func (s String) MustReset() {
if err := s.Reset(); err != nil {
panic(err)
}
s.Builder.Reset()
}
func (s String) WriteString(str string) (int, error) {
if s.Builder == nil {
return 0, ErrBufferReturned
@ -41,8 +85,8 @@ func (s String) WriteString(str string) (int, error) {
return s.Builder.WriteString(str)
}
// MustWstr means Must Write String, like WriteString but will panic on error.
func (s String) MustWstr(str string) {
// MustWriteString means Must Write String, like WriteString but will panic on error.
func (s String) MustWriteString(str string) {
if s.Builder == nil {
panic(ErrBufferReturned)
}
@ -52,13 +96,6 @@ func (s String) MustWstr(str string) {
_, _ = s.Builder.WriteString(str)
}
func (s String) Len() int {
if s.Builder == nil {
return 0
}
return s.Builder.Len()
}
func (s String) Write(p []byte) (int, error) {
if s.Builder == nil {
return 0, ErrBufferReturned
@ -80,6 +117,20 @@ func (s String) WriteByte(c byte) error {
return s.Builder.WriteByte(c)
}
func (s String) Len() int {
if s.Builder == nil {
return 0
}
return s.Builder.Len()
}
func (s String) MustLen() int {
if s.Builder == nil {
panic(ErrBufferReturned)
}
return s.Builder.Len()
}
func (s String) Grow(n int) error {
if s.Builder == nil {
return ErrBufferReturned
@ -88,46 +139,16 @@ func (s String) Grow(n int) error {
return nil
}
func (s String) MustGrow(n int) {
if s.Builder == nil {
panic(ErrBufferReturned)
}
s.Builder.Grow(n)
}
func (s String) Cap() int {
if s.Builder == nil {
return 0
}
return s.Builder.Cap()
}
type StringFactory struct {
pool *sync.Pool
}
// Put returns a strings.Builder back into to the pool after resetting it.
func (sf StringFactory) Put(buf *String) error {
var err = ErrBufferReturned
buf.o.Do(func() {
_ = buf.Reset()
sf.pool.Put(buf.Builder)
buf.Builder = nil
err = nil
})
return err
}
func (sf StringFactory) MustPut(buf *String) {
var err = ErrBufferReturned
buf.o.Do(func() {
_ = buf.Reset()
sf.pool.Put(buf.Builder)
buf.Builder = nil
err = nil
})
if err != nil {
panic(err)
}
}
// Get returns a strings.Builder from the pool.
func (sf StringFactory) Get() *String {
return &String{
sf.pool.Get().(*strings.Builder),
&sync.Once{},
}
}

View File

@ -6,7 +6,9 @@ import (
)
func assertPanic(t *testing.T, f func()) {
t.Helper()
defer func() {
t.Helper()
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
@ -15,10 +17,10 @@ func assertPanic(t *testing.T, f func()) {
}
func TestStringFactoryPanic(t *testing.T) {
s := NewStringFactory()
sf := NewStringFactory()
t.Run("StringsMustWrite", func(t *testing.T) {
buf := s.Get()
buf.MustWstr("hello world")
buf := sf.Get()
buf.MustWriteString("hello world")
if buf.Len() == 0 {
t.Fatalf("The buffer is empty after we wrote to it")
}
@ -28,28 +30,51 @@ func TestStringFactoryPanic(t *testing.T) {
})
t.Run("StringsMustWritePanic", func(t *testing.T) {
var badString *string = nil
buf := s.Get()
buf := sf.Get()
assertPanic(t, func() {
buf.MustWstr(*badString)
buf.MustWriteString(*badString)
})
assertPanic(t, func() {
buf.MustWstr("")
buf.MustWriteString("")
})
if err := s.Put(buf); err != nil {
if err := sf.Put(buf); err != nil {
t.Fatalf("The buffer was not returned: %v", err)
}
})
t.Run("StringsPanic", func(t *testing.T) {
buf := s.Get()
err := s.Put(buf)
t.Run("StringsMustString", func(t *testing.T) {
buf := sf.Get()
buf.MustWriteString("hello world")
if buf.MustString() != "hello world" {
t.Fatalf("The buffer has the wrong content")
}
sf.MustPut(buf)
assertPanic(t, func() {
buf.MustString()
})
})
t.Run("StringsMust", func(t *testing.T) {
buf := sf.Get()
buf.MustReset()
_ = buf.MustLen()
buf.MustGrow(10)
err := sf.Put(buf)
if err != nil {
t.Fatalf("The buffer was not returned: %v", err)
}
assertPanic(t, func() {
s.MustPut(buf)
sf.MustPut(buf)
})
assertPanic(t, func() {
buf.MustWstr("hello")
buf.MustWriteString("hello")
})
assertPanic(t, func() {
buf.MustGrow(10)
})
assertPanic(t, func() {
buf.MustLen()
})
assertPanic(t, func() {
buf.MustReset()
})
})
}
@ -133,7 +158,7 @@ func TestStringFactory(t *testing.T) {
})
t.Run("StringPoolCleanBuffer", func(t *testing.T) {
t.Parallel()
time.Sleep(100 * time.Millisecond)
time.Sleep(25 * time.Millisecond)
got := s.Get()
if got.String() != "" {
t.Fatalf("should have gotten a clean buffer")