protobuf-go/internal/impl/pointer_unsafe.go
Joe Tsai c6b7561199 internal/impl: support wrapping Go structs to implement proto.Message
Given a pointer to a Go struct (that is well-formed according to the v1
struct field layout), wrap the type such that it implements the v2
protoreflect.Message interface.

Change-Id: I5987cad0d22e53970c613cdbbb1cfd4210897f69
Reviewed-on: https://go-review.googlesource.com/c/138897
Reviewed-by: Damien Neil <dneil@google.com>
2018-10-03 02:10:04 +00:00

50 lines
1.4 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !purego
package impl
import (
"reflect"
"unsafe"
)
// offset represents the offset to a struct field, accessible from a pointer.
// The offset is the byte offset to the field from the start of the struct.
type offset uintptr
// offsetOf returns a field offset for the struct field.
func offsetOf(f reflect.StructField) offset {
return offset(f.Offset)
}
// pointer is a pointer to a message struct or field.
type pointer struct{ p unsafe.Pointer }
// pointerOfValue returns v as a pointer.
func pointerOfValue(v reflect.Value) pointer {
return pointer{p: unsafe.Pointer(v.Pointer())}
}
// pointerOfIface returns the pointer portion of an interface.
func pointerOfIface(v *interface{}) pointer {
type ifaceHeader struct {
Type unsafe.Pointer
Data unsafe.Pointer
}
return pointer{p: (*ifaceHeader)(unsafe.Pointer(v)).Data}
}
// apply adds an offset to the pointer to derive a new pointer
// to a specified field. The current pointer must be pointing at a struct.
func (p pointer) apply(f offset) pointer {
return pointer{p: unsafe.Pointer(uintptr(p.p) + uintptr(f))}
}
// asType treats p as a pointer to an object of type t and returns the value.
func (p pointer) asType(t reflect.Type) reflect.Value {
return reflect.NewAt(t, p.p)
}