reflect: add support for Type.Bits()

This commit is contained in:
Ayke van Laethem
2019-08-07 16:56:05 +02:00
committed by Ron Evans
parent f43d01bdc7
commit b8cd8b6f25
3 changed files with 53 additions and 25 deletions
+22 -1
View File
@@ -164,8 +164,15 @@ func (t Type) Field(i int) StructField {
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 {
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 {
@@ -213,3 +220,17 @@ type StructField struct {
Name string
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)
}