go4.org@v0.0.0-20230225012048-214862532bf5/reflectutil/swapper_safe.go (about)

     1  // Copyright 2016 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  
     5  // +build !go1.8
     6  // +build js appengine safe ppc64 ppc64le arm64 mips mipsle mips64 mips64le
     7  
     8  package reflectutil
     9  
    10  import "reflect"
    11  
    12  func swapper(slice reflect.Value) func(i, j int) {
    13  	tmp := reflect.New(slice.Type().Elem()).Elem()
    14  	return func(i, j int) {
    15  		v1 := slice.Index(i)
    16  		v2 := slice.Index(j)
    17  		tmp.Set(v1)
    18  		v1.Set(v2)
    19  		v2.Set(tmp)
    20  	}
    21  }