vortex_io/runtime/
blocking.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use futures::Stream;
5
6use crate::runtime::Handle;
7
8/// A generic API blocking entry points to runtimes.
9pub trait BlockingRuntime {
10    /// Associated type for the blocking iterator returned by `block_on_stream`.
11    type BlockingIterator<'a, R: 'a>: Iterator<Item = R> + 'a;
12
13    /// Returns a handle to the runtime.
14    fn handle(&self) -> Handle;
15
16    /// Runs a future to completion on the runtime, blocking the current thread until it completes.
17    ///
18    /// The future is provided a [`Handle`] to the runtime so that it may spawn additional tasks
19    /// to be executed concurrently.
20    fn block_on<F, Fut, R>(&self, f: F) -> R
21    where
22        F: FnOnce(Handle) -> Fut,
23        Fut: Future<Output = R>;
24
25    /// Returns an iterator wrapper around a stream, blocking the current thread for each item.
26    fn block_on_stream<'a, F, S, R>(&self, f: F) -> Self::BlockingIterator<'a, R>
27    where
28        F: FnOnce(Handle) -> S,
29        S: Stream<Item = R> + Send + 'a,
30        R: Send + 'a;
31}