7 ms·
Can somebody shed a light on what the ''a means in the Request type? pub struct Request<''a> { pub origin: &''a Request, pub params: HashMap<Stri
by itsadok 12y ago
Can somebody shed a light on what the ''a means in the Request type?
pub struct Request<''a> {
pub origin: &''a Request,
pub params: HashMap<String, String>,
}
- dbaupp 12y agoThe doubled ' is just a bug in the documentation generator (fixed by https://github.com/mozilla/rust/pull/14900 https://github.com/mozilla/rust/pull/14900, which is unfortunately (and strangely) failing tests at the moment). Its true form is pub struct Request<'a> { pub origin: &'a Request, pub params: HashMap<String, String> } where the 'a is representing the lifetime of the origin reference (that is, it is asserting that a Request struct is only usable for as long as the reference in the `origin` field is valid). Lifetimes/references are one of the core features of Rust guaranteeing both memory safety and performance. See also: http://doc.rust-lang.org/master/guide-lifetimes.html http://doc.rust-lang.org/master/guide-lifetimes.html
- dbaupp 12y ago(Sorry, I misspoke, #14900 is fixing a different rustdoc issue: the fix for '' is https://github.com/mozilla/rust/pull/14906 https://github.com/mozilla/rust/pull/14906 .)
- thomaslee 12y agoLooks like it's supposed to be a lifetime name, but it got munged in the docs: https://github.com/cburgdorf/Floor/blob/master/src/request.rs https://github.com/cburgdorf/Floor/blob/master/src/request.r... If you're unfamiliar, Rust has some fairly strict rules about lifetimes. Basically this syntax means that the thing referenced by `origin` needs to live for as long as the Request that contains it.
- pornel 12y agoIt means that the `origin` will be safely usable for as long as the `Request` structure owning it is alive. In rust "lifetime" can be a part of the type, so Rust compiler can ensure that no code can keep reference to `origin` for longer than it keeps reference to the structure that owns it.
- shadowmint 12y agoI think the point to be made here is that the Request type of origin is an http.Request, not a request.Request, so this isn't an insane recursive structure. What it means is the local request.Request object Floor passes to handlers takes an exclusive immutable borrow of the incoming http.Request object; the 'a is effectively just house keeping to make the compiler happy, because we know the http.Request, coming from a higher call will always exist for the lifetime of the request.Request object in the handler. ...so what you said is true, but the point is that we're asserting using prior knowledge that the compiler cant infer about the lifetime of that borrowed http.Request object. At least, thats my take on it.