1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-10 10:00:46 +09:00
anytype-heart/util/pbtypes/copy.go
Sergey Cherepanov 88c05033e3
fix copy struct
2020-04-17 13:31:51 +03:00

40 lines
793 B
Go

package pbtypes
import (
"sync"
"github.com/anytypeio/go-anytype-library/pb/model"
"github.com/gogo/protobuf/types"
)
var bytesPool = &sync.Pool{
New: func() interface{} {
return []byte{}
},
}
func CopyBlock(in *model.Block) (out *model.Block) {
buf := bytesPool.Get().([]byte)
size := in.Size()
if cap(buf) < size {
buf = make([]byte, 0, size*2)
}
size, _ = in.MarshalToSizedBuffer(buf[:size])
out = &model.Block{}
_ = out.Unmarshal(buf[:size])
bytesPool.Put(buf)
return
}
func CopyStruct(in *types.Struct) (out *types.Struct) {
buf := bytesPool.Get().([]byte)
size := in.Size()
if cap(buf) < size {
buf = make([]byte, 0, size*2)
}
size, _ = in.MarshalToSizedBuffer(buf[:size])
out = &types.Struct{}
_ = out.Unmarshal(buf[:size])
bytesPool.Put(buf)
return
}