io: don't allocate new error on every call to Bytes()

It makes no sense and we're using Bytes() pretty often.
This commit is contained in:
Roman Khimov 2021-08-20 10:58:51 +03:00
parent 894cfe29be
commit b8dd284d3d

View file

@ -5,6 +5,9 @@ import (
"errors" "errors"
) )
// ErrDrained is returned on an attempt to use already drained write buffer.
var ErrDrained = errors.New("buffer already drained")
// BufBinWriter is an additional layer on top of BinWriter that // BufBinWriter is an additional layer on top of BinWriter that
// automatically creates buffer to write into that you can get after all // automatically creates buffer to write into that you can get after all
// writes via Bytes(). // writes via Bytes().
@ -29,7 +32,7 @@ func (bw *BufBinWriter) Bytes() []byte {
if bw.Err != nil { if bw.Err != nil {
return nil return nil
} }
bw.Err = errors.New("buffer already drained") bw.Err = ErrDrained
return bw.buf.Bytes() return bw.buf.Bytes()
} }