Consider the following generic type, which ensures to be large enough by 
allocating enough padding A and the to-be-aligned type T:

type Alignment interface {
Align16 | Align64
}
type Align16 = [16]byte
type Align64 = [64]byte

type Aligned[T any, A Alignment] struct {
pad A
val T
}

func (p *Aligned[T, A]) Value() *T {
size := unsafe.Sizeof(*new(A))
align := size - uintptr(unsafe.Pointer(&p.pad[0]))&(size-1)
return (*T)(unsafe.Pointer(&p.pad[align]))
}

Under the assumption that T has no heap pointers, is this safe? Or asked in 
another way, under which circumstances would this break?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/d00058ec-cb9b-495f-807d-54397be94df2n%40googlegroups.com.

Reply via email to