-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ghOSt: Add serialization for absl::StatusOr<std::string>
This extends the TrivialStatus library to include a variant to work for absl::StatusOr<std::string>. It currently supports strings up to 15000 characters in length. PiperOrigin-RevId: 501929797
- Loading branch information
1 parent
e7127ec
commit e25f625
Showing
5 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,48 @@ | ||
#include "lib/trivial_status.h" | ||
#include "absl/strings/str_format.h" | ||
|
||
namespace ghost { | ||
|
||
namespace { | ||
|
||
template <size_t ArraySize> | ||
void CopyString(absl::string_view s, std::array<char, ArraySize>& dest) { | ||
void CopyString(std::array<char, ArraySize>& dest, absl::string_view s) { | ||
static_assert(ArraySize > 0); | ||
const size_t chars_to_copy = std::min(ArraySize - 1, s.size()); | ||
if (chars_to_copy < s.size()) { | ||
absl::FPrintF( | ||
stderr, | ||
"Source string too large to fix in TrivialStatus: %zu, vs max_size %zu", | ||
s.size(), chars_to_copy); | ||
} | ||
std::copy_n(s.begin(), chars_to_copy, dest.begin()); | ||
dest[std::min(chars_to_copy, ArraySize - 1)] = '\0'; | ||
dest[chars_to_copy] = '\0'; | ||
} | ||
|
||
} // namespace | ||
|
||
TrivialStatus::TrivialStatus(const absl::Status& s) { | ||
code_ = s.code(); | ||
|
||
CopyString<1000>(s.message(), error_message_); | ||
CopyString<kMaxErrorMessageSize>(error_message_, s.message()); | ||
} | ||
|
||
TrivialStatusOrString::TrivialStatusOrString( | ||
const absl::StatusOr<std::string>& s) | ||
: status_(TrivialStatus(s.status())) { | ||
if (s.ok()) { | ||
string_length_ = s.value().size(); | ||
CopyString<kMaxStringSize>(str_, s.value()); | ||
} | ||
} | ||
|
||
absl::StatusOr<std::string> TrivialStatusOrString::ToStatusOr() const | ||
{ | ||
absl::Status s = status_.ToStatus(); | ||
if (s.ok()) { | ||
return std::string(str_.data(), string_length_); | ||
} | ||
return s; | ||
} | ||
|
||
} // namespace ghost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters