Iron middleware - how to specify serde Deserialize lifetime?

Hello!
I work on generic query parser for my web-service and got a trouble. This is simplified main function

impl<'a, Q> BeforeMiddleware for QueryParser<Q>
where
    Q: QueryKey + Send + Sync,
    <Q as Key>::Value: Query + Deserialize<'a>,
{
    fn before(&self, request: &mut Request) -> IronResult<()> {
        let query_data = qs::from_str(request.url.query().unwrap());

        let mut query: Q::Value = query_data.unwrap();
        request.extensions.insert::<Q>(query);
        Ok(())
    }
}

It's not compile because of lifetime conflict. And I understand why - the query() method return &str, then the qs::from_str (serde in fact) parse it and store some parts of the source query string in Q::Value's fields. So Rust can't be sure that the query sting will live as long as result structure. It's correct.
But I store the parse result in the Request object, so it should live as long as the Request itself! Is there some way to explain this to Rust? Or I should use DeserializeOwned?

The constraint you want is for<'a> Deserialize<'a>, however DeserializeOwned is equivalent to that.

Thanks! I forgot about HRTB.
But is it possible to parse a query into a Q::Value as references?

The trouble is that Q is defined in a larger scope than the lifetime on the reference to the request, so it cannot depend on that lifetime.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.