aboutsummaryrefslogtreecommitdiff
path: root/src/core/lib/promise/detail/promise_like.h
blob: 70be971cc69da318837e710b703fd9851468b925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 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_SRC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H
#define GRPC_SRC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H

#include <grpc/support/port_platform.h>

#include <utility>

#include "absl/meta/type_traits.h"

#include "src/core/lib/promise/poll.h"

// A Promise is a callable object that returns Poll<T> 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<int>(42); },
//     ...)
// or:
//   Seq(
//     [] -> Poll<int> { 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 <typename T>
struct PollWrapper {
  static Poll<T> Wrap(T&& x) { return Poll<T>(std::forward<T>(x)); }
};

template <typename T>
struct PollWrapper<Poll<T>> {
  static Poll<T> Wrap(Poll<T>&& x) { return std::forward<Poll<T>>(x); }
};

template <typename T>
auto WrapInPoll(T&& x) -> decltype(PollWrapper<T>::Wrap(std::forward<T>(x))) {
  return PollWrapper<T>::Wrap(std::forward<T>(x));
}

template <typename F, typename SfinaeVoid = void>
class PromiseLike;

template <>
class PromiseLike<void>;

// Android local modification: Android builds grpc with C++20, which removes
// std::result_of. Use std::invoke_result_t instead.
template <typename F>
class PromiseLike<F, absl::enable_if_t<!std::is_void<
#if __cplusplus >= 201703L
                         std::invoke_result_t<F>
#else
                         typename std::result_of<F()>::type
#endif
                         >::value>> {
 private:
  GPR_NO_UNIQUE_ADDRESS F f_;

 public:
  // NOLINTNEXTLINE - internal detail that drastically simplifies calling code.
  PromiseLike(F&& f) : f_(std::forward<F>(f)) {}
  auto operator()() -> decltype(WrapInPoll(f_())) { return WrapInPoll(f_()); }
  using Result = typename PollTraits<decltype(WrapInPoll(f_()))>::Type;
};

// T -> T, const T& -> T
template <typename T>
using RemoveCVRef = absl::remove_cv_t<absl::remove_reference_t<T>>;

}  // namespace promise_detail
}  // namespace grpc_core

#endif  // GRPC_SRC_CORE_LIB_PROMISE_DETAIL_PROMISE_LIKE_H