File: reflectutil.go

package info (click to toggle)
golang-go4 0.0~git20190313.94abd69-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 760 kB
  • sloc: asm: 20; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,049 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package reflectutil contains reflect utilities.
package reflectutil

import "reflect"

// hasPointers reports whether the given type contains any pointers,
// including any internal pointers in slices, funcs, maps, channels,
// etc.
//
// This function exists for Swapper's internal use, instead of reaching
// into the runtime's *reflect._rtype kind&kindNoPointers flag.
func hasPointers(t reflect.Type) bool {
	if t == nil {
		panic("nil Type")
	}
	k := t.Kind()
	if k <= reflect.Complex128 {
		return false
	}
	switch k {
	default:
		// chan, func, interface, map, ptr, slice, string, unsafepointer
		// And anything else. It's safer to err on the side of true.
		return true
	case reflect.Array:
		return hasPointers(t.Elem())
	case reflect.Struct:
		num := t.NumField()
		for i := 0; i < num; i++ {
			if hasPointers(t.Field(i).Type) {
				return true
			}
		}
		return false
	}
}