Closed tmm1 closed 4 years ago
Okay I understand this a little better now. struc
will always dereference pointers to serialize the underlying value, which makes perfect sense for golang types and mimics how json/xml and other serializers work.
In this case C.char
is a Uint8
, so it serializes an 8-bit int instead of the pointer as I intended.
I'm thinking the best thing to handle my case is to add support in struc
for unsafe.Pointer
fields. Right now they blow up with:
panic: struc: Could not find field type.
Looks like maybe a custom type is the way to go here..
Custom type worked!
type cstr struct {
str *C.char
}
func (c *cstr) Pack(p []byte, opt *struc.Options) (int, error) {
size := c.Size(opt)
if size == 4 {
opt.Order.PutUint32(p[:], uint32(uintptr(unsafe.Pointer(c.str))))
} else {
opt.Order.PutUint64(p[:], uint64(uintptr(unsafe.Pointer(c.str))))
}
return size, nil
}
func (c *cstr) Unpack(r io.Reader, length int, opt *struc.Options) error {
panic("unimpl")
}
func (c *cstr) Size(opt *struc.Options) int {
return (int)(unsafe.Sizeof(uintptr(0)))
}
func (c *cstr) String() string {
panic("unimpl")
}