| 1 |
// Copyright 2022 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#ifndef GRPC_CORE_LIB_PROMISE_MAP_PIPE_H |
| 16 |
#define GRPC_CORE_LIB_PROMISE_MAP_PIPE_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include "absl/status/status.h" |
| 21 |
|
| 22 |
#include "src/core/lib/promise/detail/promise_factory.h" |
| 23 |
#include "src/core/lib/promise/for_each.h" |
| 24 |
#include "src/core/lib/promise/map.h" |
| 25 |
#include "src/core/lib/promise/pipe.h" |
| 26 |
|
| 27 |
namespace grpc_core { |
| 28 |
|
| 29 |
// Apply a (possibly async) mapping function to src, and output into dst. |
| 30 |
// |
| 31 |
// In psuedo-code: |
| 32 |
// for each element in wait_for src.Next: |
| 33 |
// x = wait_for filter_factory(element) |
| 34 |
// wait_for dst.Push(x) |
| 35 |
template <typename T, typename Filter> |
| 36 |
auto MapPipe(PipeReceiver<T> src, PipeSender<T> dst, Filter filter_factory) { |
| 37 |
return ForEach( |
| 38 |
std::move(src), |
| 39 |
[filter_factory = promise_detail::RepeatedPromiseFactory<T, Filter>( |
| 40 |
std::move(filter_factory)), |
| 41 |
dst = std::move(dst)](T t) mutable { |
| 42 |
return TrySeq(filter_factory.Make(std::move(t)), [&dst](T t) { |
| 43 |
return Map(dst.Push(std::move(t)), [](bool successful_push) { |
| 44 |
if (successful_push) { |
| 45 |
return absl::OkStatus(); |
| 46 |
} |
| 47 |
return absl::CancelledError(); |
| 48 |
}); |
| 49 |
}); |
| 50 |
}); |
| 51 |
} |
| 52 |
|
| 53 |
// Helper to intecept a pipe and apply a mapping function. |
| 54 |
// Each of the `Intercept` constructors will take a PipeSender or PipeReceiver, |
| 55 |
// construct a new pipe, and then replace the passed in pipe with its new end. |
| 56 |
// In this way it can interject logic per-element. |
| 57 |
// Next, the TakeAndRun function will return a promise that can be run to apply |
| 58 |
// a mapping promise to each element of the pipe. |
| 59 |
template <typename T> |
| 60 |
class PipeMapper { |
| 61 |
public: |
| 62 |
static PipeMapper Intercept(PipeSender<T>& intercept_sender) { |
| 63 |
PipeMapper<T> r; |
| 64 |
r.interceptor_.sender.Swap(&intercept_sender); |
| 65 |
return r; |
| 66 |
} |
| 67 |
|
| 68 |
static PipeMapper Intercept(PipeReceiver<T>& intercept_receiver) { |
| 69 |
PipeMapper<T> r; |
| 70 |
r.interceptor_.receiver.Swap(&intercept_receiver); |
| 71 |
return r; |
| 72 |
} |
| 73 |
|
| 74 |
template <typename Filter> |
| 75 |
auto TakeAndRun(Filter filter) { |
| 76 |
return MapPipe(std::move(interceptor_.receiver), |
| 77 |
std::move(interceptor_.sender), std::move(filter)); |
| 78 |
} |
| 79 |
|
| 80 |
private: |
| 81 |
PipeMapper() = default; |
| 82 |
Pipe<T> interceptor_; |
| 83 |
}; |
| 84 |
|
| 85 |
} // namespace grpc_core |
| 86 |
|
| 87 |
#endif // GRPC_CORE_LIB_PROMISE_MAP_PIPE_H |