mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Add marshal.MarshalAll() and marshal.TotalSize().
These util methods make it easier to work with multiple marshallable structs at once. PiperOrigin-RevId: 522353552
This commit is contained in:
committed by
gVisor bot
parent
59329f20b5
commit
f1a8f2714c
@@ -21,3 +21,24 @@ func Marshal(m Marshallable) []byte {
|
||||
m.MarshalUnsafe(buf)
|
||||
return buf
|
||||
}
|
||||
|
||||
// MarshalAll returns the serialized contents of all ms in a newly allocated
|
||||
// byte slice.
|
||||
func MarshalAll(ms []Marshallable) []byte {
|
||||
buf := make([]byte, TotalSize(ms))
|
||||
var written int
|
||||
for _, m := range ms {
|
||||
m.MarshalUnsafe(buf[written:])
|
||||
written += m.SizeBytes()
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
// TotalSize returns the total size of all ms.
|
||||
func TotalSize(ms []Marshallable) int {
|
||||
var size int
|
||||
for _, m := range ms {
|
||||
size += m.SizeBytes()
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user