Skip to content

Commit 3010466

Browse files
aykevldeadprogram
authored andcommitted
reflect: implement PtrTo
1 parent 36db75b commit 3010466

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/reflect/type.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ func TypeOf(i interface{}) Type {
133133
return ValueOf(i).typecode
134134
}
135135

136+
func PtrTo(t Type) Type {
137+
ptrType := t<<5 | 5 // 0b0101 == 5
138+
if ptrType>>5 != t {
139+
panic("reflect: PtrTo type does not fit")
140+
}
141+
return ptrType
142+
}
143+
136144
func (t Type) String() string {
137145
return "T"
138146
}

testdata/reflect.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ func main() {
265265
println("type assertion failed (but should succeed)")
266266
}
267267

268+
if reflect.TypeOf(new(myint)) != reflect.PtrTo(reflect.TypeOf(myint(0))) {
269+
println("PtrTo failed for type myint")
270+
}
271+
if reflect.TypeOf(new(myslice)) != reflect.PtrTo(reflect.TypeOf(make(myslice, 0))) {
272+
println("PtrTo failed for type myslice")
273+
}
274+
268275
println("\nstruct tags")
269276
TestStructTag()
270277
}

0 commit comments

Comments
 (0)