io: add Grow to BinWriter

Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgeniy Stratonikov 2021-08-06 10:52:24 +03:00
parent c69670c85b
commit dacf025dd9

View file

@ -1,6 +1,7 @@
package io package io
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"io" "io"
"reflect" "reflect"
@ -144,3 +145,11 @@ func (w *BinWriter) WriteVarBytes(b []byte) {
func (w *BinWriter) WriteString(s string) { func (w *BinWriter) WriteString(s string) {
w.WriteVarBytes([]byte(s)) w.WriteVarBytes([]byte(s))
} }
// Grow tries to increase underlying buffer capacity so that at least n bytes
// can be written without reallocation. If the writer is not a buffer, this is a no-op.
func (w *BinWriter) Grow(n int) {
if b, ok := w.w.(*bytes.Buffer); ok {
b.Grow(n)
}
}