1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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.

// This module contains the async runtime abstraction for iceberg.

use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

pub enum JoinHandle<T> {
    #[cfg(feature = "tokio")]
    Tokio(tokio::task::JoinHandle<T>),
    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    AsyncStd(async_std::task::JoinHandle<T>),
    #[cfg(all(not(feature = "async-std"), not(feature = "tokio")))]
    Unimplemented(Box<T>),
}

impl<T: Send + 'static> Future for JoinHandle<T> {
    type Output = T;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        match self.get_mut() {
            #[cfg(feature = "tokio")]
            JoinHandle::Tokio(handle) => Pin::new(handle)
                .poll(cx)
                .map(|h| h.expect("tokio spawned task failed")),
            #[cfg(all(feature = "async-std", not(feature = "tokio")))]
            JoinHandle::AsyncStd(handle) => Pin::new(handle).poll(cx),
            #[cfg(all(not(feature = "async-std"), not(feature = "tokio")))]
            JoinHandle::Unimplemented(_) => unimplemented!("no runtime has been enabled"),
        }
    }
}

#[allow(dead_code)]
pub fn spawn<F>(f: F) -> JoinHandle<F::Output>
where
    F: Future + Send + 'static,
    F::Output: Send + 'static,
{
    #[cfg(feature = "tokio")]
    return JoinHandle::Tokio(tokio::task::spawn(f));

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    return JoinHandle::AsyncStd(async_std::task::spawn(f));

    #[cfg(all(not(feature = "async-std"), not(feature = "tokio")))]
    unimplemented!("no runtime has been enabled")
}

#[allow(dead_code)]
pub fn spawn_blocking<F, T>(f: F) -> JoinHandle<T>
where
    F: FnOnce() -> T + Send + 'static,
    T: Send + 'static,
{
    #[cfg(feature = "tokio")]
    return JoinHandle::Tokio(tokio::task::spawn_blocking(f));

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    return JoinHandle::AsyncStd(async_std::task::spawn_blocking(f));

    #[cfg(all(not(feature = "async-std"), not(feature = "tokio")))]
    unimplemented!("no runtime has been enabled")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn test_tokio_spawn() {
        let handle = spawn(async { 1 + 1 });
        assert_eq!(handle.await, 2);
    }

    #[cfg(feature = "tokio")]
    #[tokio::test]
    async fn test_tokio_spawn_blocking() {
        let handle = spawn_blocking(|| 1 + 1);
        assert_eq!(handle.await, 2);
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_async_std_spawn() {
        let handle = spawn(async { 1 + 1 });
        assert_eq!(handle.await, 2);
    }

    #[cfg(all(feature = "async-std", not(feature = "tokio")))]
    #[async_std::test]
    async fn test_async_std_spawn_blocking() {
        let handle = spawn_blocking(|| 1 + 1);
        assert_eq!(handle.await, 2);
    }
}