I'm trying to implement an Iron handler that contains a reference to another struct. That struct holds the data and does all operations on the data.
[package]
name = "testcrate"
version = "0.1.0"
authors = ["me"]
[dependencies]
iron = "^0.2"
This is the code:
//! Handlers for the server.
extern crate iron;
use iron::{status, middleware};
use iron::IronResult;
use iron::prelude::Set;
use iron::request::Request;
use iron::response::Response;
/// The MyServer struct holds the data and provides methods
/// to manipulate or retrieve that data.
struct MyServer;
impl MyServer {
pub fn build_response() -> String {
"response".to_string()
}
}
/// The ReadHandler handles the creation of HTTP responses.
pub struct ReadHandler {
pub server: &MyServer,
}
impl middleware::Handler for ReadHandler {
/// Return the current status JSON.
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let body = self.server.build_response();
let response = Response::with((status::Ok, body));
Ok(response)
}
}
Unfortunately I'm getting errors about missing lifetime specifiers:
src/lib.rs:22:17: 22:26 error: missing lifetime specifier [E0106]
src/lib.rs:22 pub server: &MyServer,
^~~~~~~~~
src/lib.rs:22:17: 22:26 help: run `rustc --explain E0106` to see a detailed explanation
But when I add lifetime specifiers (which I didn't fully understand yet)...
pub struct ReadHandler<'a> {
pub server: &'a MyServer,
}
impl<'a> middleware::Handler for ReadHandler<'a> {
// ...
}
...I get another error message:
src/lib.rs:24:1: 32:2 error: the type `ReadHandler<'a>` does not fulfill the required lifetime
src/lib.rs:24 impl<'a> middleware::Handler for ReadHandler<'a> {
src/lib.rs:25
src/lib.rs:26 /// Return the current status JSON.
src/lib.rs:27 fn handle(&self, req: &mut Request) -> IronResult<Response> {
src/lib.rs:28 let body = self.server.build_response();
src/lib.rs:29 let response = Response::with((status::Ok, body));
...
note: type must outlive the static lifetime
There will be other handlers that will access the MyServer in a read-only or read-write mode.
What would be the correct way to add lifetime specifiers? Or do I maybe need a structure like an Arc?
The Handler
trait is a subtrait of Any
:
pub trait Handler: Send + Sync + Any {
fn handle(&self, &mut Request) -> IronResult<Response>;
}
And the Any
trait has a 'static
lifetime bound:
pub trait Any: Reflect + 'static {
fn get_type_id(&self) -> TypeId;
}
The 'static
bound means that your struct cannot contain references. You'll have to use another type of pointer. Since Handler
is also a subtrait of Send
and Sync
, you'll need to use Arc
, since Rc
is neither Send
nor Sync
.