// Copyright 2021 gRPC authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H #define GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H #include #include #include "absl/meta/type_traits.h" #include "src/core/lib/promise/poll.h" // A Promise is a callable object that returns Poll for some T. // Often when we're writing code that uses promises, we end up wanting to also // deal with code that completes instantaneously - that is, it returns some T // where T is not Poll. // PromiseLike wraps any callable that takes no parameters and implements the // Promise interface. For things that already return Poll, this wrapping does // nothing. For things that do not return Poll, we wrap the return type in Poll. // This allows us to write things like: // Seq( // [] { return 42; }, // ...) // in preference to things like: // Seq( // [] { return Poll(42); }, // ...) // or: // Seq( // [] -> Poll { return 42; }, // ...) // leading to slightly more concise code and eliminating some rules that in // practice people find hard to deal with. namespace grpc_core { namespace promise_detail { template struct PollWrapper { static Poll Wrap(T&& x) { return Poll(std::forward(x)); } }; template struct PollWrapper> { static Poll Wrap(Poll&& x) { return std::forward>(x); } }; template auto WrapInPoll(T&& x) -> decltype(PollWrapper::Wrap(std::forward(x))) { return PollWrapper::Wrap(std::forward(x)); } template class PromiseLike { private: GPR_NO_UNIQUE_ADDRESS F f_; public: // NOLINTNEXTLINE - internal detail that drastically simplifies calling code. PromiseLike(F&& f) : f_(std::forward(f)) {} auto operator()() -> decltype(WrapInPoll(f_())) { return WrapInPoll(f_()); } using Result = typename PollTraits::Type; }; // T -> T, const T& -> T template using RemoveCVRef = absl::remove_cv_t>; } // namespace promise_detail } // namespace grpc_core #endif // GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H