slice_go14.go 478 B

1234567891011121314151617181920212223
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build !go1.8
  5. // +build !go1.8
  6. package sort
  7. import "reflect"
  8. var reflectValueOf = reflect.ValueOf
  9. func reflectSwapper(x any) func(int, int) {
  10. v := reflectValueOf(x)
  11. tmp := reflect.New(v.Type().Elem()).Elem()
  12. return func(i, j int) {
  13. a, b := v.Index(i), v.Index(j)
  14. tmp.Set(a)
  15. a.Set(b)
  16. b.Set(tmp)
  17. }
  18. }