all: abstract fast-path marshal and unmarshal inputs and outputs
We may want to make changes to the inputs and outputs of the fast-path
functions in the future. For example, we likely want to add the ability
for the fast-path unmarshal to report back whether the unmarshaled
message is known to be initialized.
Change the signatures of these functions to take in and return struct
types which can be extended with whatever fields we want in the future.
Change-Id: Idead360785df730283a4630ea405265b72482e62
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/215719
Reviewed-by: Joe Tsai <joetsai@google.com>
diff --git a/internal/impl/legacy_message.go b/internal/impl/legacy_message.go
index 80220a2..6f08912 100644
--- a/internal/impl/legacy_message.go
+++ b/internal/impl/legacy_message.go
@@ -50,7 +50,7 @@
v := reflect.Zero(t).Interface()
if _, ok := v.(legacyMarshaler); ok {
- mi.methods.MarshalAppend = legacyMarshalAppend
+ mi.methods.Marshal = legacyMarshal
// We have no way to tell whether the type's Marshal method
// supports deterministic serialization or not, but this
@@ -363,8 +363,8 @@
}
var legacyProtoMethods = &piface.Methods{
- MarshalAppend: legacyMarshalAppend,
- Unmarshal: legacyUnmarshal,
+ Marshal: legacyMarshal,
+ Unmarshal: legacyUnmarshal,
// We have no way to tell whether the type's Marshal method
// supports deterministic serialization or not, but this
@@ -373,26 +373,28 @@
Flags: piface.SupportMarshalDeterministic,
}
-func legacyMarshalAppend(b []byte, m protoreflect.Message, opts piface.MarshalOptions) ([]byte, error) {
+func legacyMarshal(m protoreflect.Message, in piface.MarshalInput) (piface.MarshalOutput, error) {
v := m.(unwrapper).protoUnwrap()
marshaler, ok := v.(legacyMarshaler)
if !ok {
- return nil, errors.New("%T does not implement Marshal", v)
+ return piface.MarshalOutput{}, errors.New("%T does not implement Marshal", v)
}
out, err := marshaler.Marshal()
- if b != nil {
- out = append(b, out...)
+ if in.Buf != nil {
+ out = append(in.Buf, out...)
}
- return out, err
+ return piface.MarshalOutput{
+ Buf: out,
+ }, err
}
-func legacyUnmarshal(b []byte, m protoreflect.Message, opts piface.UnmarshalOptions) error {
+func legacyUnmarshal(m protoreflect.Message, in piface.UnmarshalInput) (piface.UnmarshalOutput, error) {
v := m.(unwrapper).protoUnwrap()
unmarshaler, ok := v.(legacyUnmarshaler)
if !ok {
- return errors.New("%T does not implement Marshal", v)
+ return piface.UnmarshalOutput{}, errors.New("%T does not implement Marshal", v)
}
- return unmarshaler.Unmarshal(b)
+ return piface.UnmarshalOutput{}, unmarshaler.Unmarshal(in.Buf)
}
// aberrantMessageType implements MessageType for all types other than pointer-to-struct.