go4.org@v0.0.0-20230225012048-214862532bf5/reflectutil/swapper.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  package reflectutil
     6  
     7  import "reflect"
     8  
     9  // Swapper returns a function which swaps the elements in slice.
    10  // Swapper panics if the provided interface is not a slice.
    11  //
    12  // Its goal is to work safely and efficiently for all versions and
    13  // variants of Go: pre-Go1.5, Go1.5+, safe, unsafe, App Engine,
    14  // GopherJS, etc.
    15  //
    16  // Deprecated: this moved to the Go standard library. Use
    17  // reflect.Swapper in Go 1.8+ instead.
    18  func Swapper(slice interface{}) func(i, j int) {
    19  	v := reflect.ValueOf(slice)
    20  	if v.Kind() != reflect.Slice {
    21  		panic(&reflect.ValueError{Method: "reflectutil.Swapper", Kind: v.Kind()})
    22  	}
    23  	return swapper(v)
    24  }