pepijnve commented on code in PR #23530: URL: https://github.com/apache/datafusion/pull/23530#discussion_r3570881601
########## datafusion/execution/src/async_stream.rs: ########## @@ -0,0 +1,621 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +use futures::Stream; +use futures::future::FusedFuture; +use futures::stream::FusedStream; +use pin_project_lite::pin_project; +use std::ops::DerefMut; +use std::pin::Pin; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll}; + +/// A handle for emitting values from an [`async_stream`] generator. +/// +/// The generator closure receives an `Emitter<T>` as its argument. +pub struct Emitter<T> { + slot: Arc<Mutex<Option<T>>>, +} + +/// A handle for emitting values from an [`async_try_stream`] generator. +/// +/// The generator closure receives a `TryEmitter<T, E>` as its argument. +pub struct TryEmitter<T, E> { + slot: Arc<Mutex<Option<Result<T, E>>>>, +} + +struct Receiver<T> { + slot: Arc<Mutex<Option<T>>>, +} + +impl<T> Emitter<T> { + /// Returns a `Future` that emits `value` as the next stream item. + /// + /// The returned future **must be awaited immediately**. On its first poll it + /// yields `Poll::Pending`, handing control back to the stream consumer so it + /// can observe the emitted value. On the next poll (triggered by the + /// consumer calling `poll_next` again) it completes with `Poll::Ready(())`, + /// resuming the generator. + /// + /// # Panics + /// + /// Panics if `emit` is called a second time before the previous future has + /// been awaited, because doing so would silently overwrite the unconsumed + /// value. + pub fn emit(&mut self, value: T) -> impl FusedFuture<Output = ()> { + let mut guard = self.slot.lock().unwrap(); + match guard.deref_mut() { + Some(_) => panic!("Misuse: call await immediately after calling emit"), Review Comment: Thanks, I applied your suggestion -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
