From 5012be337f59c45833e4c9dc55feeaf0d86f52bf Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 8 Aug 2019 14:39:41 +0200 Subject: [PATCH] reflect: implement Type.Align() --- src/reflect/type.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/reflect/type.go b/src/reflect/type.go index 9e5b752e..56ce0857 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -216,6 +216,47 @@ func (t Type) Size() uintptr { } } +// Align returns the alignment of this type. It is similar to calling +// unsafe.Alignof. +func (t Type) Align() int { + switch t.Kind() { + case Bool, Int8, Uint8: + return int(unsafe.Alignof(int8(0))) + case Int16, Uint16: + return int(unsafe.Alignof(int16(0))) + case Int32, Uint32: + return int(unsafe.Alignof(int32(0))) + case Int64, Uint64: + return int(unsafe.Alignof(int64(0))) + case Int, Uint: + return int(unsafe.Alignof(int(0))) + case Uintptr: + return int(unsafe.Alignof(uintptr(0))) + case Float32: + return int(unsafe.Alignof(float32(0))) + case Float64: + return int(unsafe.Alignof(float64(0))) + case Complex64: + return int(unsafe.Alignof(complex64(0))) + case Complex128: + return int(unsafe.Alignof(complex128(0))) + case String: + return int(unsafe.Alignof(StringHeader{})) + case UnsafePointer, Chan, Map, Ptr: + return int(unsafe.Alignof(uintptr(0))) + case Slice: + return int(unsafe.Alignof(SliceHeader{})) + default: + panic("unimplemented: alignment of type") + } +} + +// FieldAlign returns the alignment if this type is used in a struct field. It +// is currently an alias for Align() but this might change in the future. +func (t Type) FieldAlign() int { + return t.Align() +} + // AssignableTo returns whether a value of type u can be assigned to a variable // of type t. func (t Type) AssignableTo(u Type) bool {