Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[go] Add a Writable interface to ease the process to encode structs to Writer #100

@lburgazzoli

Description

@lburgazzoli

I've been experimenting with karmem and I found that it would be useful to have an interface to describe writeable structs, something like:

// Writable writes an encoded representation of the type to the underlying Writer.
type Writable interface {
	WriteAsRoot(*Writer) (uint, error)
}

This would help to create helper method to encode different type of generated structs, i.e. I have something like

func ToPtr(writable Writable, w *karmem.Writer ) (uint32, uint32) {
	if _, err := writable.WriteAsRoot(w); err != nil {
		panic(err)
	}

	buf := w.Bytes()
	ptr := &buf[0]
	unsafePtr := uintptr(unsafe.Pointer(ptr))

	return uint32(unsafePtr), uint32(len(buf))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions