From c1bde22bc7de5e06d46c801207f9534bc8e9ccf3 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 11 Dec 2024 13:50:00 +0300 Subject: [PATCH] proto: Add helper function to marshal `Message` It will be useful for `interface { Marshal() []byte }` implementations. Signed-off-by: Leonard Lyubich --- internal/proto/encoding.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/proto/encoding.go b/internal/proto/encoding.go index 31c70565..cb933b9c 100644 --- a/internal/proto/encoding.go +++ b/internal/proto/encoding.go @@ -21,6 +21,13 @@ type Message interface { MarshalStable(b []byte) } +// MarshalMessage returns m encoded to dynamically allocated buffer. +func MarshalMessage(m Message) []byte { + b := make([]byte, m.MarshaledSize()) + m.MarshalStable(b) + return b +} + // Bytes is a type parameter constraint for any byte arrays. type Bytes interface{ ~[]byte | ~string }