Skip to content

Commit 05031e0

Browse files
committed
patch reflect: Zero/Len
1 parent 28b3f67 commit 05031e0

File tree

5 files changed

+505
-1
lines changed

5 files changed

+505
-1
lines changed

_demo/rtype/rtype.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ import "reflect"
55
func main() {
66
tyIntSlice := reflect.SliceOf(reflect.TypeOf(0))
77
println(tyIntSlice.String())
8+
9+
v := reflect.Zero(tyIntSlice)
10+
println(v.Len())
811
}

internal/abi/type.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,22 @@ func (p *Imethod) PkgPath() string {
329329

330330
func (t *Type) Kind() Kind { return Kind(t.Kind_ & KindMask) }
331331

332+
func (t *Type) HasName() bool {
333+
return t.TFlag&TFlagNamed != 0
334+
}
335+
336+
func (t *Type) Pointers() bool { return t.PtrBytes != 0 }
337+
338+
// IfaceIndir reports whether t is stored indirectly in an interface value.
339+
func (t *Type) IfaceIndir() bool {
340+
return t.Kind_&KindDirectIface == 0
341+
}
342+
343+
// isDirectIface reports whether t is stored directly in an interface value.
344+
func (t *Type) IsDirectIface() bool {
345+
return t.Kind_&KindDirectIface != 0
346+
}
347+
332348
// Size returns the size of data with type t.
333349
func (t *Type) Size() uintptr { return t.Size_ }
334350

@@ -340,7 +356,7 @@ func (t *Type) FieldAlign() int { return int(t.FieldAlign_) }
340356
// String returns string form of type t.
341357
func (t *Type) String() string {
342358
if t.TFlag&TFlagExtraStar != 0 {
343-
return "*" + t.Str_
359+
return "*" + t.Str_ // TODO(xsw): misunderstand
344360
}
345361
return t.Str_
346362
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2020 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 unsafeheader contains header declarations for the Go runtime's slice
6+
// and string implementations.
7+
//
8+
// This package allows packages that cannot import "reflect" to use types that
9+
// are tested to be equivalent to reflect.SliceHeader and reflect.StringHeader.
10+
package reflect
11+
12+
import (
13+
"unsafe"
14+
)
15+
16+
// unsafeheaderSlice is the runtime representation of a slice.
17+
// It cannot be used safely or portably and its representation may
18+
// change in a later release.
19+
//
20+
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
21+
// data it references will not be garbage collected.
22+
type unsafeheaderSlice struct {
23+
Data unsafe.Pointer
24+
Len int
25+
Cap int
26+
}
27+
28+
// unsafeheaderString is the runtime representation of a string.
29+
// It cannot be used safely or portably and its representation may
30+
// change in a later release.
31+
//
32+
// Unlike reflect.StringHeader, its Data field is sufficient to guarantee the
33+
// data it references will not be garbage collected.
34+
type unsafeheaderString struct {
35+
Data unsafe.Pointer
36+
Len int
37+
}

0 commit comments

Comments
 (0)