首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rust回调: Error: Lifetime‘静态必填

Rust回调: Error: Lifetime‘静态必填
EN

Stack Overflow用户
提问于 2018-08-28 03:51:44
回答 1查看 2K关注 0票数 3

我正在尝试编写一个将回调作为参数的泛型函数。但是,我总是收到以下错误消息:

代码语言:javascript
复制
error[E0310]: the parameter type `C` may not live long enough
  --> src/lib.rs:36:5
   |
17 | pub fn helper<'de, Q, S, C>(mut state : State, callback : C) -> Box<HandlerFuture>
   |                          - help: consider adding an explicit lifetime bound `C: 'static`...
...
36 |     Box::new(f)
   |     ^^^^^^^^^^^
   |
note: ...so that the type `futures::Then<futures::stream::Concat2<hyper::Body>, futures::FutureResult<(gotham::state::State, hyper::Response), (gotham::state::State, gotham::handler::HandlerError)>, [closure@src/lib.rs:24:15: 34:10 callback:&C, state:gotham::state::State]>` will meet its required lifetime bounds
  --> src/lib.rs:36:5
   |
36 |     Box::new(f)
   |     ^^^^^^^^^^^

这是一个生成错误消息的最小可编译示例:

代码语言:javascript
复制
extern crate futures;
extern crate gotham;
extern crate hyper;
extern crate mime;
extern crate serde;
extern crate serde_json;

use futures::{future, Future, Stream};
use gotham::handler::{HandlerFuture, IntoHandlerError};
use gotham::http::response::create_response;
use gotham::state::{FromState, State};
use hyper::{Body, StatusCode};
use mime::APPLICATION_JSON;
use serde::{Deserialize, Serialize};
use serde_json::{from_str, to_string};

pub fn helper<'de, Q, S, C>(mut state : State, callback : C) -> Box<HandlerFuture>
    where Q : Deserialize<'de>,
          S : Serialize,
          C : Fn(Q) -> S
{
    let f = Body::take_from(&mut state)
        .concat2()
        .then(|full_body| match full_body {
            Ok(valid_body) => {
                let body_content = String::from_utf8(valid_body.to_vec()).unwrap();
                let body_json = from_str::<Q>(&body_content).unwrap();
                let resp_json = callback(body_json);
                let resp_content = to_string(&resp_json).unwrap().into_bytes();
                let res = create_response(&state, StatusCode::Ok, Some((resp_content, APPLICATION_JSON)));
                future::ok((state, res))
            }
            Err(e) => return future::err((state, e.into_handler_error()))
        });

    Box::new(f)
}

按照rustc的建议将C : 'static添加到where子句中,我得到了以下错误消息:

代码语言:javascript
复制
error[E0597]: `body_content` does not live long enough
  --> src/lib.rs:28:48
   |
28 |                 let body_json = from_str::<Q>(&body_content).unwrap();
   |                                                ^^^^^^^^^^^^ borrowed value does not live long enough
...
33 |             }
   |             - borrowed value only lives until here
   |
note: borrowed value must be valid for the lifetime 'de as defined on the function body at 17:1...
  --> src/lib.rs:17:1
   |
17 | / pub fn helper<'de, Q, S, C>(mut state : State, callback : C) -> Box<HandlerFuture>
18 | |     where Q : Deserialize<'de>,
19 | |           S : Serialize,
20 | |           C : Fn(Q) -> S,
...  |
37 | |     Box::new(f)
38 | | }
   | |_^

这是我的Cargo.toml的最小内容

代码语言:javascript
复制
[package]
name = "test"
version = "0.1.0"

[dependencies]
futures = "0.1"
gotham = "0.2"
hyper = "0.11"
mime = "0.3"
serde = "1.0"
serde_json = "1.0"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-28 06:07:18

在rustc的建议和this article的帮助下,我能够解决所有的错误:

  1. I添加了C : 'staticwhere子句
  2. I更改了闭包|full_body|,以便它将绑定的类型从Deserialize<'de>更改为Deserialize<'de>

我最终得到了这段似乎可以工作的代码:

代码语言:javascript
复制
pub fn helper<Q, S, C>(mut state : State, callback : C) -> Box<HandlerFuture>
    where Q : DeserializeOwned,
          S : Serialize,
          C : Fn(Q) -> S,
          C : 'static
{
    let f = Body::take_from(&mut state)
        .concat2()
        .then(move |full_body| match full_body {
            Ok(valid_body) => {
                let body_content = String::from_utf8(valid_body.to_vec()).unwrap();
                let body_json = from_str::<Q>(&body_content).unwrap();
                let resp_json = callback(body_json);
                let resp_content = to_string(&resp_json).unwrap().into_bytes();
                let res = create_response(&state, StatusCode::Ok, Some((resp_content, APPLICATION_JSON)));
                future::ok((state, res))
            }
            Err(e) => return future::err((state, e.into_handler_error()))
        });

    Box::new(f)
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52046118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档