1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/capy
7  
// Official repository: https://github.com/cppalliance/capy
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_CAPY_DETAIL_AWAIT_SUSPEND_HELPER_HPP
10  
#ifndef BOOST_CAPY_DETAIL_AWAIT_SUSPEND_HELPER_HPP
11  
#define BOOST_CAPY_DETAIL_AWAIT_SUSPEND_HELPER_HPP
11  
#define BOOST_CAPY_DETAIL_AWAIT_SUSPEND_HELPER_HPP
12  

12  

13  
#include <coroutine>
13  
#include <coroutine>
14  
#include <boost/capy/ex/io_env.hpp>
14  
#include <boost/capy/ex/io_env.hpp>
15  

15  

16  
#include <type_traits>
16  
#include <type_traits>
17  

17  

18  
namespace boost {
18  
namespace boost {
19  
namespace capy {
19  
namespace capy {
20  
namespace detail {
20  
namespace detail {
21  

21  

22  
// Helper to normalize await_suspend return types to std::coroutine_handle<>
22  
// Helper to normalize await_suspend return types to std::coroutine_handle<>
23  
template<typename Awaitable>
23  
template<typename Awaitable>
24  
std::coroutine_handle<> call_await_suspend(
24  
std::coroutine_handle<> call_await_suspend(
25  
    Awaitable* a,
25  
    Awaitable* a,
26  
    std::coroutine_handle<> h,
26  
    std::coroutine_handle<> h,
27  
    io_env const* env)
27  
    io_env const* env)
28  
{
28  
{
29  
    using R = decltype(a->await_suspend(h, env));
29  
    using R = decltype(a->await_suspend(h, env));
30  
    if constexpr (std::is_void_v<R>)
30  
    if constexpr (std::is_void_v<R>)
31  
    {
31  
    {
32  
        a->await_suspend(h, env);
32  
        a->await_suspend(h, env);
33  
        return std::noop_coroutine();
33  
        return std::noop_coroutine();
34  
    }
34  
    }
35  
    else if constexpr (std::is_same_v<R, bool>)
35  
    else if constexpr (std::is_same_v<R, bool>)
36  
    {
36  
    {
37  
        if(a->await_suspend(h, env))
37  
        if(a->await_suspend(h, env))
38  
            return std::noop_coroutine();
38  
            return std::noop_coroutine();
39  
        return h;
39  
        return h;
40  
    }
40  
    }
41  
    else
41  
    else
42  
    {
42  
    {
43  
        return a->await_suspend(h, env);
43  
        return a->await_suspend(h, env);
44  
    }
44  
    }
45  
}
45  
}
46  

46  

47  
} // namespace detail
47  
} // namespace detail
48  
} // namespace capy
48  
} // namespace capy
49  
} // namespace boost
49  
} // namespace boost
50  

50  

51  
#endif
51  
#endif