pw_protobuf: Return a Result from Encode()
This updates Encoder::Encode() to return a Result to make it less
awkward to use and to ensure that its status is checked by the caller.
Change-Id: Ie376ee07555199010e4363d013959bb24b0db79a
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/19620
Reviewed-by: Ewout van Bekkum <ewout@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Alexei Frolov <frolv@google.com>
diff --git a/pw_protobuf/encoder.cc b/pw_protobuf/encoder.cc
index 6a5eceb..66174fc 100644
--- a/pw_protobuf/encoder.cc
+++ b/pw_protobuf/encoder.cc
@@ -126,16 +126,14 @@
return Status::Ok();
}
-Status Encoder::Encode(std::span<const std::byte>* out) {
+Result<ConstByteSpan> Encoder::Encode() {
if (!encode_status_.ok()) {
- *out = std::span<const std::byte>();
return encode_status_;
}
if (blob_count_ == 0) {
// If there are no nested blobs, the buffer already contains a valid proto.
- *out = buffer_.first(EncodedSize());
- return Status::Ok();
+ return Result<ConstByteSpan>(buffer_.first(EncodedSize()));
}
union {
@@ -179,8 +177,7 @@
// Point the cursor to the end of the encoded proto.
cursor_ = write_cursor;
- *out = buffer_.first(EncodedSize());
- return Status::Ok();
+ return Result<ConstByteSpan>(buffer_.first(EncodedSize()));
}
} // namespace pw::protobuf