I'd like to implement the Responder
trait for my Hero
structure, but the following method signature (respond_to
):
use rocket::{http::Status, response::Responder, Request, Response};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Hero {
pub id: Option<i32>,
pub name: String,
pub identity: String,
pub hometown: String,
pub age: i32,
}
impl Hero {
pub fn new(
num: Option<i32>,
name: String,
identity: String,
hometown: String,
age: i32,
) -> Hero {
Hero {
id: num,
name,
identity,
hometown,
age,
}
}
}
impl<'r> Responder<'r> for Hero {
fn respond_to(self, _request: &Request) -> Result<Response, Status> {
unimplemented!()
}
}
Throws a compilation error:
error[E0106]: missing lifetime specifier
--> src/main.rs:32:55
|
32 | fn respond_to(self, _request: &Request) -> Result<Response, Status> {
| ^^^^^^^^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say which one of `_request`'s 2 lifetimes it is borrowed from
Dependencies:
[dependencies]
rocket = "0.4.2"
serde = {version = "1.0.99", features=["derive"]}
The documentation doesn't provide an example of how to provide lifetimes when a type is returned. How to specify lifetime when a type is returned?
The Responder
trait is defined as:
pub trait Responder<'r> {
fn respond_to(self, request: &Request) -> response::Result<'r>;
}
The response::Result<'r>
is defined as:
pub type Result<'r> = ::std::result::Result<self::Response<'r>, ::http::Status>;
Your method signature is:
fn respond_to(self, request: &Request) -> Result<Response, Status>;
As you can see, you just forgot to specify lifetime for the Response
. The correct method signature is:
impl<'r> Responder<'r> for Hero {
fn respond_to(self, request: &Request) -> Result<Response<'r>, Status> {
unimplemented!()
}
}