请求消息值必须在静态生命周期内有效
Request message value must be valid for the static lifetime
我想实现一个阻塞函数,它发送一个 POST 请求和一个 JSON 主体和 returns 响应的 JSON 对象:
extern crate tokio_core;
extern crate rustc_serialize;
extern crate hyper;
extern crate futures;
use std::str;
use rustc_serialize::json;
use rustc_serialize::{Decoder, Decodable};
use hyper::{Method, Uri};
use hyper::client::{Client, Request};
use self::tokio_core::reactor::Core;
use self::futures::{Future, Stream};
#[derive(Debug, Clone)]
pub struct FooBar {
pub foo: String,
pub bar: String
}
impl Decodable for FooBar {
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
d.read_struct("root", 0, |d| {
Ok(FooBar {
foo: try!(d.read_struct_field("foo", 0, |d| Decodable::decode(d))),
bar: try!(d.read_struct_field("bar", 1, |d| Decodable::decode(d)))
})
})
}
}
fn send_request(url: Uri, obj: json::Object) -> Option<FooBar> {
let mut core = Core::new().unwrap();
let client = Client::new(&core.handle());
let msg = json::encode(&obj).unwrap();
let mut request = Request::new(Method::Post, url);
request.set_body(msg.as_bytes());
let mut response = client.request(request).wait().unwrap();
assert_eq!(response.status(), hyper::Ok);
let res_vec = response.body().concat2().wait().unwrap().to_vec();
let res_str = str::from_utf8(&res_vec).unwrap();
return match json::decode(&res_str) {
Ok(res_obj) => Some(res_obj),
Err(err) => {
println!("{}", err);
None
}
}
}
我收到 msg
寿命不够长的错误:
error[E0597]: `msg` does not live long enough
--> src/test.rs:37:22
|
37 | request.set_body(msg.as_bytes());
| ^^^ does not live long enough
...
51 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
此时我有两个问题:
- 什么组件希望
msg
在静态生命周期内有效?从消息中看不清楚。
- 如何在不使
msg
在静态生命周期内有效的情况下实现这样的功能?就我而言,这不是一个可行的解决方案。
依赖关系:
rustc-serialize = "0.3"
futures = "0.1"
hyper = "0.11"
tokio-core = "0.1"
request.set_body()
takes a parameter that needs to be convertible into hyper::Body
(hyper::client::Request<B>
中 B
的默认值)。
如果你看一下 From
的列表(Into
) implementations for hyper::Body
的 "dual" 特征,你会看到 impl From<&'static [u8]> for Body
- 这是你静态的地方生命周期要求来自(没有 impl<'a> From<&'a [u8]> for Body
会引用 "bytes")。
但是您还会看到 impl From<String> for Body
- 所以只传递 msg
(据我所知应该是 String
)而不是 msg.as_bytes()
到 request.set_body()
。它将取得字符串 msg
的所有权,因此您以后不能再自己使用它了。
我想实现一个阻塞函数,它发送一个 POST 请求和一个 JSON 主体和 returns 响应的 JSON 对象:
extern crate tokio_core;
extern crate rustc_serialize;
extern crate hyper;
extern crate futures;
use std::str;
use rustc_serialize::json;
use rustc_serialize::{Decoder, Decodable};
use hyper::{Method, Uri};
use hyper::client::{Client, Request};
use self::tokio_core::reactor::Core;
use self::futures::{Future, Stream};
#[derive(Debug, Clone)]
pub struct FooBar {
pub foo: String,
pub bar: String
}
impl Decodable for FooBar {
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
d.read_struct("root", 0, |d| {
Ok(FooBar {
foo: try!(d.read_struct_field("foo", 0, |d| Decodable::decode(d))),
bar: try!(d.read_struct_field("bar", 1, |d| Decodable::decode(d)))
})
})
}
}
fn send_request(url: Uri, obj: json::Object) -> Option<FooBar> {
let mut core = Core::new().unwrap();
let client = Client::new(&core.handle());
let msg = json::encode(&obj).unwrap();
let mut request = Request::new(Method::Post, url);
request.set_body(msg.as_bytes());
let mut response = client.request(request).wait().unwrap();
assert_eq!(response.status(), hyper::Ok);
let res_vec = response.body().concat2().wait().unwrap().to_vec();
let res_str = str::from_utf8(&res_vec).unwrap();
return match json::decode(&res_str) {
Ok(res_obj) => Some(res_obj),
Err(err) => {
println!("{}", err);
None
}
}
}
我收到 msg
寿命不够长的错误:
error[E0597]: `msg` does not live long enough
--> src/test.rs:37:22
|
37 | request.set_body(msg.as_bytes());
| ^^^ does not live long enough
...
51 | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...
此时我有两个问题:
- 什么组件希望
msg
在静态生命周期内有效?从消息中看不清楚。 - 如何在不使
msg
在静态生命周期内有效的情况下实现这样的功能?就我而言,这不是一个可行的解决方案。
依赖关系:
rustc-serialize = "0.3"
futures = "0.1"
hyper = "0.11"
tokio-core = "0.1"
request.set_body()
takes a parameter that needs to be convertible into hyper::Body
(hyper::client::Request<B>
中 B
的默认值)。
如果你看一下 From
的列表(Into
) implementations for hyper::Body
的 "dual" 特征,你会看到 impl From<&'static [u8]> for Body
- 这是你静态的地方生命周期要求来自(没有 impl<'a> From<&'a [u8]> for Body
会引用 "bytes")。
但是您还会看到 impl From<String> for Body
- 所以只传递 msg
(据我所知应该是 String
)而不是 msg.as_bytes()
到 request.set_body()
。它将取得字符串 msg
的所有权,因此您以后不能再自己使用它了。