labels: add ScratchBuilder.Overwrite for slice implementation

This is a method used by some downstream projects; it was created to
optimize the implementation in `labels_string.go` but we should have one
for both implementations so the same code works with either.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2023-04-13 11:07:54 +00:00
parent 8ed90b567b
commit 10cc60af01
2 changed files with 7 additions and 1 deletions

View File

@ -631,3 +631,9 @@ func (b *ScratchBuilder) Labels() Labels {
// Copy the slice, so the next use of ScratchBuilder doesn't overwrite. // Copy the slice, so the next use of ScratchBuilder doesn't overwrite.
return append([]Label{}, b.add...) return append([]Label{}, b.add...)
} }
// Write the newly-built Labels out to ls.
// Callers must ensure that there are no other references to ls, or any strings fetched from it.
func (b *ScratchBuilder) Overwrite(ls *Labels) {
*ls = append((*ls)[:0], b.add...)
}

View File

@ -811,7 +811,7 @@ func (b *ScratchBuilder) Labels() Labels {
} }
// Write the newly-built Labels out to ls, reusing an internal buffer. // Write the newly-built Labels out to ls, reusing an internal buffer.
// Callers must ensure that there are no other references to ls. // Callers must ensure that there are no other references to ls, or any strings fetched from it.
func (b *ScratchBuilder) Overwrite(ls *Labels) { func (b *ScratchBuilder) Overwrite(ls *Labels) {
size := labelsSize(b.add) size := labelsSize(b.add)
if size <= cap(b.overwriteBuffer) { if size <= cap(b.overwriteBuffer) {