I still managed to cope with a lifelong problem, which I seem to be unable to solve on my own.
I have this trait
pub trait MiddlewareHandler: Clone { fn invoke (&self, req: &Request, res: &mut Response) -> bool { true } // we need this because otherwise clone() would be ambiguous fn clone_box(&self) -> Box<MiddlewareHandler> { box self.clone() as Box<MiddlewareHandler> } } impl MiddlewareHandler for fn (req: &Request, res: &mut Response) -> bool { fn invoke(&self, req: &Request, res: &mut Response) -> bool{ (*self).invoke(req, res) } } impl Clone for Box<MiddlewareHandler> { fn clone(&self) -> Box<MiddlewareHandler> { self.clone_box() } }
What I am implementing for fn (req: &Request, res: &mut Response) -> bool in order to be able to simultaneously use lightweight functions and heavier MiddlewareHandler constructors.
I store them as a Vec<Box<MiddlewareHandler>>
pub struct Middleware { handlers: Vec<Box<MiddlewareHandler>> }
Now the problem is that the compiler is yelling at me right here:
pub fn add<T: MiddlewareHandler> (&mut self, handler: T) { self.handlers.push(box handler); }
It says:
error: value may contain references; add `'static` bound to `T` self.handlers.push(box handler);
The implementation should be very similar to the one used here:
https://github.com/iron/iron/blob/master/src/chain/stackchain.rs#L67
However, it seems I do not see the difference: - /
If someone wants to give me a hand, I moved the code in github to the static branch:
https://github.com/floor-org/floor/tree/static