Asynchronous 如何使用futures编写简单的异步代理;0.3“;及超",;0.13.0-α.4“;?

Asynchronous 如何使用futures编写简单的异步代理;0.3“;及超",;0.13.0-α.4“;?,asynchronous,rust,async-await,future,hyper,Asynchronous,Rust,Async Await,Future,Hyper,我正试图通过迁移到以下位置来重写book的代理: futures-preview = { version = "0.3.0-alpha.19", features = ["async-await"]}` hyper = "0.13.0-alpha.4"` 发件人: 当前示例将返回的Future从futures 0.3转换为futures 0.1,因为hyper=“0.12.9”与futures 0.3的异步/等待不兼容 我的代码: 使用{ 未来:{FutureExt,TryFutureExt}

我正试图通过迁移到以下位置来重写book的代理:

futures-preview = { version = "0.3.0-alpha.19", features = ["async-await"]}`
hyper = "0.13.0-alpha.4"`
发件人:

当前示例将返回的
Future
futures 0.3
转换为
futures 0.1
,因为
hyper=“0.12.9”
futures 0.3
的异步/等待不兼容

我的代码:

使用{
未来:{FutureExt,TryFutureExt},
超级::{
rt::运行,
服务:{make_service_fn,service_fn},
正文、客户端、错误、请求、响应、服务器、Uri、,
},
std::net::SocketAddress,
std::str::FromStr,
};
fn forward_uri(forward_url:&'static str,req:&Request)->uri{
自传\u uri=match req.uri().query(){
Some(query)=>format!(“{}{}?{}”,forward_url,req.uri().path(),query),
None=>format!(“{}{}”,forward_url,req.uri().path()),
};
Uri::from_str(forward_Uri.as_str()).unwrap()
}
异步fn调用(
转发url:&'static str,
mut_请求:请求,
)->结果{
*_req.uri_mut()=转发uri(转发url和请求);
设url_str=forward_uri(forward_url,&_req);
让res=Client::new().get(url\u str).wait;
物件
}
异步fn运行服务器(转发url:&'static str,地址:SocketAddr){
let forwarded_url=forward_url;
让serve_future=service_fn(move | req | call(forwarded_url,req).boxed());
让server=server::bind(&addr).service(未来服务);
如果让Err(Err)=server.await{
eprintln!(“服务器错误:{}”,err);
}
}
fn main(){
//设置要在其上运行套接字的地址。
让addr=SocketAddr::from([127,0,0,1],3000));
让url=”http://127.0.0.1:9061";
让futures\u 03\u future=运行服务器(url,addr);
运行(期货03期货);
}
首先,我在
run\u server
函数中收到了
server
的此错误:

trait
tower\u service::service通过此,为您需要在
hyper=“0.13.0-alpha.4”
中创建的每个连接创建一个新服务。您可以使用
make\u service\u fn
创建带有闭包的
MakeService

另外,我不能使用
hyper::rt::run
,因为它在
hyper=0.13.0-alpha.4
中的实现可能有所不同

正确,在调用tokio::run时,它已从api中删除,但目前我不知道原因。您可以通过自己调用
tokio::run
或使用
#[tokio::main]
注释来运行未来。为此,您需要将
东京
添加到您的货物中:

#this is the version of tokio inside hyper "0.13.0-alpha.4"
tokio = "=0.2.0-alpha.6" 
然后更改您的
运行\u服务器
,如下所示:

异步fn运行服务器(转发url:&'static str,addr:SocketAddr){ 让server=server::bind(&addr).service(make_service)fn(move | | |{ 异步移动{Ok::(服务| fn(移动|请求|调用(转发| url,请求)))} })); 如果让Err(Err)=server.await{ eprintln!(“服务器错误:{}”,err); } }
main

#[tokio::main]
异步fn main()->(){
//设置要在其上运行套接字的地址。
让addr=SocketAddr::from([127,0,0,1],3000));
让url=”http://www.google.com:80“;//我已经用谷歌测试过了
运行服务器(url,地址)。等待
}
#this is the version of tokio inside hyper "0.13.0-alpha.4"
tokio = "=0.2.0-alpha.6"