wait()
是一种反模式
我是锈病新手,所以我决定使用超级创建来构建服务器。我查看了超级文档,并决定尝试一些新的东西,以便对其他超级方法的工作原理有一个广泛的了解。我正在尝试读取Post方法的主体数据。
在第一种方法中,我concat2然后调用wait,现在调用wait,阻塞当前线程,使我的代码挂起。
在第二种方法中,我尝试使用poll和and_then,但是我总是得到NotReady,返回结果类型为futures :: poll :: Async] >>>
第一种方法
if Method::POST == req.method() { let body = req.into_body().concat2().wait(); // convert to json and do something with the data. Ok(Response::new(Body::from("OK: data submitted"))) }....
第二种方法
:如何解除对当前线程的阻止并返回结果?if Method::POST == req.method() { let body = req.into_body().concat2().poll().and_then(|e| { // do something Ok(e) }); match body { Ok(e) => println!("{:#?}", e)), Err(r) => println!("{:#?}", r), }; Ok(Response::new(Body::from(""))) }.....
问题1
问题2
:如何轮询然后返回结果?如果可能,请您说明如何处理futures :: poll :: Async
和wait()的良好做法。目前异步/等待结果到稳定Rust中的[[异步fn不稳定,所以我不能使用它。我是锈病新手,所以我决定使用超级创建来构建服务器。我查看了超级文档,并决定尝试一些新的东西,以便对其他超级...... >>>wait()
是一种反模式
hyper
路由接受实现IntoFuture
的返回值,该返回值以覆盖方式为IntoFuture
和Option<T>
实现。因此,您实际编写的内容根本不需要阻止-您只需要像这样组合期货:Result<T, E>
您甚至没有使用代码中的
if Method::POST == req.method() { req.into_body().concat2().map(|_body| { Response::new(Body::from("OK: data submitted")) }) }
内容,但我假设这是出于MCVE的目的。在body
组合器内部,该值当前显示为map()
,并允许您使用它进行任何操作。
wait()
是一种反模式