From 10cc60af013c5cfc33f4cc377c423e8120030276 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 13 Apr 2023 11:07:54 +0000 Subject: [PATCH] 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 --- model/labels/labels.go | 6 ++++++ model/labels/labels_string.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/model/labels/labels.go b/model/labels/labels.go index e3a478a25..b7398d17f 100644 --- a/model/labels/labels.go +++ b/model/labels/labels.go @@ -631,3 +631,9 @@ func (b *ScratchBuilder) Labels() Labels { // Copy the slice, so the next use of ScratchBuilder doesn't overwrite. 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...) +} diff --git a/model/labels/labels_string.go b/model/labels/labels_string.go index d830d5a43..f293c9167 100644 --- a/model/labels/labels_string.go +++ b/model/labels/labels_string.go @@ -811,7 +811,7 @@ func (b *ScratchBuilder) Labels() Labels { } // 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) { size := labelsSize(b.add) if size <= cap(b.overwriteBuffer) {