Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lburgazzoli opened this issue Mar 3, 2023 · 1 comment

Comments

@lburgazzoli
Copy link

lburgazzoli commented Mar 3, 2023

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))
}
@lburgazzoli
Copy link
Author

I can send a PR if it make sense

lburgazzoli added a commit to lburgazzoli/karmem that referenced this issue Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant