foyer_common/
asyncify.rs

1// Copyright 2025 foyer Project Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::runtime::SingletonHandle;
16
17/// Convert the block call to async call with given runtime handle.
18#[cfg(not(madsim))]
19pub async fn asyncify_with_runtime<F, T>(runtime: &SingletonHandle, f: F) -> T
20where
21    F: FnOnce() -> T + Send + 'static,
22    T: Send + 'static,
23{
24    runtime.spawn_blocking(f).await.unwrap()
25}
26
27#[cfg(madsim)]
28/// Convert the block call to async call with given runtime.
29///
30/// madsim compatible mode.
31pub async fn asyncify_with_runtime<F, T>(_: &SingletonHandle, f: F) -> T
32where
33    F: FnOnce() -> T + Send + 'static,
34    T: Send + 'static,
35{
36    f()
37}