Skip to content

Commit

Permalink
Add non-public API for internal users
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 532553508
Change-Id: I813841ff3e5085b64c9b02ca41897bf7f6a8570e
  • Loading branch information
EricWF authored and copybara-github committed May 16, 2023
1 parent abe63eb commit c8b33b0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions absl/status/internal/status_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ struct StatusRep {

std::atomic<int32_t> ref;
absl::StatusCode code;

// As an internal implementation detail, we guarantee that if status.message()
// is non-empty, then the resulting string_view is null terminated.
// This is required to implement 'StatusMessageAsCStr(...)'
std::string message;
std::unique_ptr<status_internal::Payloads> payloads;
};
Expand Down
7 changes: 7 additions & 0 deletions absl/status/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -616,5 +616,12 @@ std::string* MakeCheckFailString(const absl::Status* status,

} // namespace status_internal

const char* StatusMessageAsCStr(const Status& status) {
// As an internal implementation detail, we guarantee that if status.message()
// is non-empty, then the resulting string_view is null terminated.
auto sv_message = status.message();
return sv_message.empty() ? "" : sv_message.data();
}

ABSL_NAMESPACE_END
} // namespace absl
9 changes: 9 additions & 0 deletions absl/status/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,15 @@ inline Status OkStatus() { return Status(); }
// message-less kCancelled errors are common in the infrastructure.
inline Status CancelledError() { return Status(absl::StatusCode::kCancelled); }

// Retrieves a message's status as a null terminated C string. The lifetime of
// this string is tied to the lifetime of the status object itself.
//
// If the status's message is empty, the empty string is returned.
//
// StatusMessageAsCStr exists for C support. Use `status.message()` in C++.
const char* StatusMessageAsCStr(
const Status& status ABSL_ATTRIBUTE_LIFETIME_BOUND);

ABSL_NAMESPACE_END
} // namespace absl

Expand Down
23 changes: 23 additions & 0 deletions absl/status/status_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ TEST(Status, ConstructorWithCodeMessage) {
}
}

TEST(Status, StatusMessageCStringTest) {
{
absl::Status status = absl::OkStatus();
EXPECT_EQ(status.message(), "");
EXPECT_STREQ(absl::StatusMessageAsCStr(status), "");
EXPECT_EQ(status.message(), absl::StatusMessageAsCStr(status));
EXPECT_NE(absl::StatusMessageAsCStr(status), nullptr);
}
{
absl::Status status;
EXPECT_EQ(status.message(), "");
EXPECT_NE(absl::StatusMessageAsCStr(status), nullptr);
EXPECT_STREQ(absl::StatusMessageAsCStr(status), "");
}
{
absl::Status status(absl::StatusCode::kInternal, "message");
EXPECT_FALSE(status.ok());
EXPECT_EQ(absl::StatusCode::kInternal, status.code());
EXPECT_EQ("message", status.message());
EXPECT_STREQ("message", absl::StatusMessageAsCStr(status));
}
}

TEST(Status, ConstructOutOfRangeCode) {
const int kRawCode = 9999;
absl::Status status(static_cast<absl::StatusCode>(kRawCode), "");
Expand Down

0 comments on commit c8b33b0

Please sign in to comment.