Skip to content

proto: add function to shallow copy a message #1155

Open
@dsnet

Description

@dsnet

Generated messages have mutexes and other variables that make it dangerous to shallow copy.

Add a function to the proto package that does the equivalent of a shallow copy in a safe way.

The function would semantically equivalent to:

func ShallowCopy(dst, src proto.Message) {
     dm := dst.ProtoReflect()
     sm := src.Protoreflect()
     if dm.Type() != sm.Type() {
         panic("mismatching type")
     }
     proto.Reset(dst)
     sm.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
         dm.Set(fd, v)
         return true
     })
}

but be optimized for performance.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions