Skip to content

Commit

Permalink
Change WithResult to AssertResultType for better readability. (grpc#3…
Browse files Browse the repository at this point in the history
…8743)

WithResult acts as a type checker to assert that the promise passed to the function returns the expected type (i.e. Poll\<T\>). The name WithResult does not imply that the function is an assertion.

Closes grpc#38743

COPYBARA_INTEGRATE_REVIEW=grpc#38743 from ac-patel:fix_typo 4fa77b7
PiperOrigin-RevId: 728914326
  • Loading branch information
ac-patel authored and copybara-github committed Feb 20, 2025
1 parent 54d338e commit 55c89e4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/core/lib/promise/promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ struct ImmediateOkStatus {
};

// Typecheck that a promise returns the expected return type.
// usage: auto promise = WithResult<int>([]() { return 3; });
// usage: auto promise = AssertResultType<int>([]() { return 3; });
// NOTE: there are tests in promise_test.cc that are commented out because they
// should fail to compile. When modifying this code these should be uncommented
// and their miscompilation verified.
template <typename T, typename F>
GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline auto WithResult(F f) ->
GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION inline auto AssertResultType(F f) ->
typename std::enable_if<std::is_same<decltype(f()), Poll<T>>::value,
F>::type {
return f;
Expand Down
2 changes: 1 addition & 1 deletion src/core/lib/security/transport/auth_filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class ClientAuthFilter final : public ImplementChannelFilter<ClientAuthFilter> {
ClientAuthFilter* filter) {
filter->InstallContext();
auto* host = md->get_pointer(HttpAuthorityMetadata());
return WithResult<absl::StatusOr<ClientMetadataHandle>>(If(
return AssertResultType<absl::StatusOr<ClientMetadataHandle>>(If(
host == nullptr,
[&md]() mutable -> absl::StatusOr<ClientMetadataHandle> {
return std::move(md);
Expand Down
8 changes: 4 additions & 4 deletions test/core/promise/promise_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ TEST(PromiseTest, Works) {

TEST(PromiseTest, Immediate) { EXPECT_EQ(Immediate(42)(), Poll<int>(42)); }

TEST(PromiseTest, WithResult) {
EXPECT_EQ(WithResult<int>(Immediate(42))(), Poll<int>(42));
// Fails to compile: WithResult<int>(Immediate(std::string("hello")));
// Fails to compile: WithResult<int>(Immediate(42.9));
TEST(PromiseTest, AssertResultType) {
EXPECT_EQ(AssertResultType<int>(Immediate(42))(), Poll<int>(42));
// Fails to compile: AssertResultType<int>(Immediate(std::string("hello")));
// Fails to compile: AssertResultType<int>(Immediate(42.9));
}

TEST(PromiseTest, NowOrNever) {
Expand Down

0 comments on commit 55c89e4

Please sign in to comment.