diff --git a/readme.md b/readme.md index 74a2edc..1a1cbc4 100644 --- a/readme.md +++ b/readme.md @@ -2,9 +2,9 @@ Pooled Buffer IO for Go programs. -Package buffer provides a reader implementation similar to bufio.Reader. The underlying memory buffers can be -used from a synchronized pool. It also supports scenarios similar to io.Pipe, where a content writer process is -wrapped by a buffered reader. +Package buffer provides a reader implementation similar to bufio.Reader, and a writer implementation to batch +write operations targeting an underlying writer. The used memory buffers can be taken from a synchronized pool. +It also supports scenarios similar to io.Pipe, where a content writer process is wrapped by a buffered reader. Find the documentation here: diff --git a/writer.go b/writer.go index 5ee9a9f..9d00848 100644 --- a/writer.go +++ b/writer.go @@ -28,6 +28,10 @@ func (w *writer) write(p []byte) (int, error) { if len(w.buffer) == 0 { w.buffer, w.err = w.options.Pool.Get() + if len(w.buffer) == 0 && w.err == nil { + w.err = ErrZeroAllocation + } + continue } diff --git a/writer_test.go b/writer_test.go index 6ba777c..f51a5c9 100644 --- a/writer_test.go +++ b/writer_test.go @@ -402,4 +402,14 @@ func TestWriter(t *testing.T) { t.Fatal(string(w.written)) } }) + + t.Run("zero allocation", func(t *testing.T) { + w := &writer{} + p := &fakePool{} + o := buffer.Options{Pool: p} + b := buffer.BufferedWriter(w, o) + if n, err := b.Write([]byte("123")); n != 0 || !errors.Is(err, buffer.ErrZeroAllocation) { + t.Fatal(n, err) + } + }) }