|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +// This module contains the async runtime abstraction for iceberg. |
| 19 | + |
| 20 | +use std::future::Future; |
| 21 | +use std::pin::Pin; |
| 22 | +use std::task::{Context, Poll}; |
| 23 | + |
| 24 | +pub enum JoinHandle<T> { |
| 25 | + #[cfg(feature = "tokio")] |
| 26 | + Tokio(tokio::task::JoinHandle<T>), |
| 27 | + #[cfg(all(feature = "async-std", not(feature = "tokio")))] |
| 28 | + AsyncStd(async_std::task::JoinHandle<T>), |
| 29 | +} |
| 30 | + |
| 31 | +impl<T: Send + 'static> Future for JoinHandle<T> { |
| 32 | + type Output = T; |
| 33 | + |
| 34 | + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 35 | + match self.get_mut() { |
| 36 | + #[cfg(feature = "tokio")] |
| 37 | + JoinHandle::Tokio(handle) => Pin::new(handle) |
| 38 | + .poll(cx) |
| 39 | + .map(|h| h.expect("tokio spawned task failed")), |
| 40 | + #[cfg(all(feature = "async-std", not(feature = "tokio")))] |
| 41 | + JoinHandle::AsyncStd(handle) => Pin::new(handle).poll(cx), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[allow(dead_code)] |
| 47 | +pub fn spawn<F>(f: F) -> JoinHandle<F::Output> |
| 48 | +where |
| 49 | + F: Future + Send + 'static, |
| 50 | + F::Output: Send + 'static, |
| 51 | +{ |
| 52 | + #[cfg(feature = "tokio")] |
| 53 | + return JoinHandle::Tokio(tokio::task::spawn(f)); |
| 54 | + |
| 55 | + #[cfg(all(feature = "async-std", not(feature = "tokio")))] |
| 56 | + return JoinHandle::AsyncStd(async_std::task::spawn(f)); |
| 57 | +} |
| 58 | + |
| 59 | +#[allow(dead_code)] |
| 60 | +pub fn spawn_blocking<F, T>(f: F) -> JoinHandle<T> |
| 61 | +where |
| 62 | + F: FnOnce() -> T + Send + 'static, |
| 63 | + T: Send + 'static, |
| 64 | +{ |
| 65 | + #[cfg(feature = "tokio")] |
| 66 | + return JoinHandle::Tokio(tokio::task::spawn_blocking(f)); |
| 67 | + |
| 68 | + #[cfg(all(feature = "async-std", not(feature = "tokio")))] |
| 69 | + return JoinHandle::AsyncStd(async_std::task::spawn_blocking(f)); |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + |
| 76 | + #[cfg(feature = "tokio")] |
| 77 | + #[tokio::test] |
| 78 | + async fn test_tokio_spawn() { |
| 79 | + let handle = spawn(async { 1 + 1 }); |
| 80 | + assert_eq!(handle.await, 2); |
| 81 | + } |
| 82 | + |
| 83 | + #[cfg(feature = "tokio")] |
| 84 | + #[tokio::test] |
| 85 | + async fn test_tokio_spawn_blocking() { |
| 86 | + let handle = spawn_blocking(|| 1 + 1); |
| 87 | + assert_eq!(handle.await, 2); |
| 88 | + } |
| 89 | + |
| 90 | + #[cfg(all(feature = "async-std", not(feature = "tokio")))] |
| 91 | + #[async_std::test] |
| 92 | + async fn test_async_std_spawn() { |
| 93 | + let handle = spawn(async { 1 + 1 }); |
| 94 | + assert_eq!(handle.await, 2); |
| 95 | + } |
| 96 | + |
| 97 | + #[cfg(all(feature = "async-std", not(feature = "tokio")))] |
| 98 | + #[async_std::test] |
| 99 | + async fn test_async_std_spawn_blocking() { |
| 100 | + let handle = spawn_blocking(|| 1 + 1); |
| 101 | + assert_eq!(handle.await, 2); |
| 102 | + } |
| 103 | +} |
0 commit comments