mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-10 10:00:46 +09:00
40 lines
793 B
Go
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
|
|
}
|