reflect: add support for Type.Bits()

Этот коммит содержится в:
Ayke van Laethem 2019-08-07 16:56:05 +02:00 коммит произвёл Ron Evans
родитель f43d01bdc7
коммит b8cd8b6f25
3 изменённых файлов: 53 добавлений и 25 удалений

Просмотреть файл

@ -164,8 +164,15 @@ func (t Type) Field(i int) StructField {
panic("unimplemented: (reflect.Type).Field()") panic("unimplemented: (reflect.Type).Field()")
} }
// Bits returns the number of bits that this type uses. It is only valid for
// arithmetic types (integers, floats, and complex numbers). For other types, it
// will panic.
func (t Type) Bits() int { func (t Type) Bits() int {
panic("unimplemented: (reflect.Type).Bits()") kind := t.Kind()
if kind >= Int && kind <= Complex128 {
return int(t.Size()) * 8
}
panic(TypeError{"Bits"})
} }
func (t Type) Len() int { func (t Type) Len() int {
@ -213,3 +220,17 @@ type StructField struct {
Name string Name string
Type Type Type Type
} }
// TypeError is the error that is used in a panic when invoking a method on a
// type that is not applicable to that type.
type TypeError struct {
Method string
}
func (e *TypeError) Error() string {
return "reflect: call of reflect.Type." + e.Method + " on invalid type"
}
func align(offset uintptr, alignment uintptr) uintptr {
return (offset + alignment - 1) &^ (alignment - 1)
}

31
testdata/reflect.go предоставленный
Просмотреть файл

@ -92,18 +92,25 @@ func main() {
// test sizes // test sizes
println("\nsizes:") println("\nsizes:")
println("int8", int(reflect.TypeOf(int8(0)).Size())) for _, tc := range []struct {
println("int16", int(reflect.TypeOf(int16(0)).Size())) name string
println("int32", int(reflect.TypeOf(int32(0)).Size())) rt reflect.Type
println("int64", int(reflect.TypeOf(int64(0)).Size())) }{
println("uint8", int(reflect.TypeOf(uint8(0)).Size())) {"int8", reflect.TypeOf(int8(0))},
println("uint16", int(reflect.TypeOf(uint16(0)).Size())) {"int16", reflect.TypeOf(int16(0))},
println("uint32", int(reflect.TypeOf(uint32(0)).Size())) {"int32", reflect.TypeOf(int32(0))},
println("uint64", int(reflect.TypeOf(uint64(0)).Size())) {"int64", reflect.TypeOf(int64(0))},
println("float32", int(reflect.TypeOf(float32(0)).Size())) {"uint8", reflect.TypeOf(uint8(0))},
println("float64", int(reflect.TypeOf(float64(0)).Size())) {"uint16", reflect.TypeOf(uint16(0))},
println("complex64", int(reflect.TypeOf(complex64(0)).Size())) {"uint32", reflect.TypeOf(uint32(0))},
println("complex128", int(reflect.TypeOf(complex128(0)).Size())) {"uint64", reflect.TypeOf(uint64(0))},
{"float32", reflect.TypeOf(float32(0))},
{"float64", reflect.TypeOf(float64(0))},
{"complex64", reflect.TypeOf(complex64(0))},
{"complex128", reflect.TypeOf(complex128(0))},
} {
println(tc.name, int(tc.rt.Size()), tc.rt.Bits())
}
assertSize(reflect.TypeOf(uintptr(0)).Size() == unsafe.Sizeof(uintptr(0)), "uintptr") assertSize(reflect.TypeOf(uintptr(0)).Size() == unsafe.Sizeof(uintptr(0)), "uintptr")
assertSize(reflect.TypeOf("").Size() == unsafe.Sizeof(""), "string") assertSize(reflect.TypeOf("").Size() == unsafe.Sizeof(""), "string")
assertSize(reflect.TypeOf(new(int)).Size() == unsafe.Sizeof(new(int)), "*int") assertSize(reflect.TypeOf(new(int)).Size() == unsafe.Sizeof(new(int)), "*int")

24
testdata/reflect.txt предоставленный
Просмотреть файл

@ -222,15 +222,15 @@ reflect type: struct
struct struct
sizes: sizes:
int8 1 int8 1 8
int16 2 int16 2 16
int32 4 int32 4 32
int64 8 int64 8 64
uint8 1 uint8 1 8
uint16 2 uint16 2 16
uint32 4 uint32 4 32
uint64 8 uint64 8 64
float32 4 float32 4 32
float64 8 float64 8 64
complex64 8 complex64 8 64
complex128 16 complex128 16 128