bkietz commented on a change in pull request #9714:
URL: https://github.com/apache/arrow/pull/9714#discussion_r595171891
##########
File path: cpp/src/arrow/util/async_generator.h
##########
@@ -336,6 +345,68 @@ class ReadaheadGenerator {
std::queue<Future<T>> readahead_queue_;
};
+template <typename T>
+class PushGenerator {
+ struct State {
+ util::Mutex mutex;
+ std::deque<Result<T>> result_q;
+ util::optional<Future<T>> consumer_fut;
+ bool finished = false;
+ };
+
+ struct Generator {
+ const std::shared_ptr<State> state_;
+
+ Future<T> operator()() {
+ auto lock = state_->mutex.Lock();
+ assert(!state_->consumer_fut.has_value()); // Non-reentrant
+ if (!state_->result_q.empty()) {
+ auto fut =
Future<T>::MakeFinished(std::move(state_->result_q.front()));
+ state_->result_q.pop_front();
+ return fut;
+ }
+ if (state_->finished) {
+ return AsyncGeneratorEnd<T>();
+ }
+ auto fut = Future<T>::Make();
+ state_->consumer_fut = fut;
+ return fut;
+ }
+ };
+
+ public:
+ PushGenerator() : state_(std::make_shared<State>()) {}
+
+ void Push(Result<T> result) {
+ auto lock = state_->mutex.Lock();
+ if (state_->consumer_fut.has_value()) {
+ auto fut = std::move(state_->consumer_fut.value());
Review comment:
The reset is necessary; after this line the optional still contains a
object (albeit a moved-from object).
Either arrangement of `move` will result in an rvalue reference to the
contained object and the style guide doesn't explicitly prefer one over the
other. I'm not sure which I prefer, either
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]