Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
  • Loading branch information
hawkingrei committed Jan 8, 2023
1 parent 084b2da commit 373056f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import (
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/sem"
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/slice"
"github.com/pingcap/tidb/util/sqlexec"
"github.com/pingcap/tidb/util/stringutil"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -313,8 +314,7 @@ func (e *ShowExec) fetchShowBind() error {
} else {
tmp = domain.GetDomain(e.ctx).BindHandle().GetAllBindRecord()
}
bindRecords := make([]*bindinfo.BindRecord, len(tmp))
copy(bindRecords, tmp)
bindRecords := slice.Copy(tmp)
// Remove the invalid bindRecord.
ind := 0
for _, bindData := range bindRecords {
Expand Down
13 changes: 13 additions & 0 deletions util/slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ func AllOf(s interface{}, p func(int) bool) bool {
}
return NoneOf(s, np)
}

// Copy is a deep copy of the slice.
func Copy[T any](a []*T) []*T {
b := make([]*T, len(a))
for i, p := range a {
if p == nil {
continue
}
v := *p
b[i] = &v
}
return b
}
17 changes: 17 additions & 0 deletions util/slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ func TestSlice(t *testing.T) {
})
}
}

func TestCopy(t *testing.T) {
type T int
v0, v1, v2, v3 := T(0), T(1), T(2), T(3)
s := []*T{&v0, &v1, &v2, &v3, nil}
e := Copy(s)
require.Equal(t, len(s), len(e))
for i := range s {
if s[i] == nil {
require.Nil(t, e[i])
} else {
require.Equal(t, *s[i], *e[i])
require.True(t, s[i] != e[i])
}
}

}

0 comments on commit 373056f

Please sign in to comment.