Kotlin 是否定义调用异步闭包的函数?

Kotlin 是否定义调用异步闭包的函数?,kotlin,rust,async-await,Kotlin,Rust,Async Await,假设我们有一些重复的异步代码来执行htttp调用, 现在我们要编写一个函数,它将使用与闭包相同的代码,并记录请求前后的所有内容 下面是我想在kotlin中实现的简化代码(锈专家只需查看下面的锈代码忽略kotlin示例) 如果你运行它,你会得到输出 write down the request REQUEST my closure uses 'REQUEST' and can do some async stuff write down the response RESPONSE 现在,我正试图

假设我们有一些重复的异步代码来执行htttp调用, 现在我们要编写一个函数,它将使用与闭包相同的代码,并记录请求前后的所有内容

下面是我想在
kotlin
中实现的简化代码(锈专家只需查看下面的锈代码忽略kotlin示例)

如果你运行它,你会得到输出

write down the request REQUEST
my closure uses 'REQUEST' and can do some async stuff
write down the response RESPONSE
现在,我正试图用铁锈做同样的事情(我是新来的)

使用std::fmt::Display;
//忽略这一点
异步fn延迟(毫秒:i64){}
fn log_请求(请求:A,可调用:impl FnOnce(A)->B)->B{
println!(“写下请求{},&req);
let rep:B=可调用(req);
println!(“写下响应{},&rep);
代表
}
#[tokio::main]
异步fn main(){
let request=“request”;
让callable=| it:&str |->和str{
println!(“我的闭包使用{}并且可以做一些异步的事情”,it);
//延迟(1000)。等待;//取消对此的注释
“答复”
};
let response=log_请求(请求,可调用);
}
在生锈的情况下,正确的方法是什么?

如果取消注释,延迟调用编译器将显示
| it:&str |->&str{不是异步的
那么如何将其定义为异步。。。
因为我已经在异步范围内运行,所以我肯定能够调用异步闭包(这相当于kotlin中的可挂起函数)Kotlin和Rust示例之间的区别在于,你的Kotlin
log\u请求是
suspend
,我假设它与Rust中的
async
fn相同。你也可以通过在Rust
async
中发出
log\u请求来获得你想要的东西。这需要一些额外的步骤来等待
的未来在
log_request
中取消>并对函数定义进行一些调整

log_request
上添加
async
后,您需要将闭包的输出更改为返回
未来的
,其中
C:Display
。然后您需要
等待
闭包的输出以实际获得可显示的值

另一个变化在闭包定义本身中,因为闭包不是
async
,所以需要通过从闭包中返回
Future
来解决这个问题。使用
async{}
块是这里的解决方案

这些步骤会使某些类型属性无效,例如,闭包上的
&str
属性可以完全删除,如果要保留
rep:B
注释,则需要将其更改为
rep:C

use std::fmt::Display;
use std::future::Future;

// ignore this
async fn delay(millis: i64) {}

async fn log_request<A: Display, B, C: Display>(req: A, callable: impl FnOnce(A) -> B) -> C
where
    B: Future<Output = C>,
{
    println!("write down the request {}", &req);
    let rep: C = callable(req).await;
    println!("write down the response {}", &rep);
    rep
}

#[tokio::main]
async fn main() {
    let request = "REQUEST";

    let callable = |it| {
        async move {
            println!("my closure uses '{}' and can do some async stuff", it);
            delay(1000).await; // uncommenting this
            "RESPONSE"
        }
    };

    let response = log_request(request, callable).await;
}

使用std::fmt::Display;
使用std::future::future;
//忽略这一点
异步fn延迟(毫秒:i64){}

async fn log_request

如果闭包执行了async操作,它将不再返回
&str
,而是返回一个
未来
。因此,类型注释是不正确的。在我的回答中,类型归属消失了,另外一个
C
类型被添加到
log_request
中,该类型被绑定到未来的输出。我将添加som我的答案有更多的上下文,因为它显然没有提供所有必要的信息:)
write down the request REQUEST
my closure uses 'REQUEST' and can do some async stuff
write down the response RESPONSE
use std::fmt::Display;

// ignore this
async fn delay(millis: i64) {}

fn log_request<A: Display, B: Display>(req: A, callable: impl FnOnce(A) -> B) -> B {
    println!("write down the request {}", &req);
    let rep: B = callable(req);
    println!("write down the response {}", &rep);
    rep
}

#[tokio::main]
async fn main() {
    let request = "REQUEST";

    let callable = |it: &str| -> &str {
        println!("my closure uses '{}' and can do some async stuff", it);
        // delay(1000).await; // uncommenting this
        "RESPONSE"
    };

    let response = log_request(request, callable);
}
use std::fmt::Display;
use std::future::Future;

// ignore this
async fn delay(millis: i64) {}

async fn log_request<A: Display, B, C: Display>(req: A, callable: impl FnOnce(A) -> B) -> C
where
    B: Future<Output = C>,
{
    println!("write down the request {}", &req);
    let rep: C = callable(req).await;
    println!("write down the response {}", &rep);
    rep
}

#[tokio::main]
async fn main() {
    let request = "REQUEST";

    let callable = |it| {
        async move {
            println!("my closure uses '{}' and can do some async stuff", it);
            delay(1000).await; // uncommenting this
            "RESPONSE"
        }
    };

    let response = log_request(request, callable).await;
}