Skip to content

Instantly share code, notes, and snippets.

@Aatch
Created July 9, 2013 02:04
Show Gist options
  • Save Aatch/5954101 to your computer and use it in GitHub Desktop.
Save Aatch/5954101 to your computer and use it in GitHub Desktop.
Multiple Lifetimes - The issue
/*
This is some hypothetical builder object that mutably
borrows a `Map` then does stuff to it. But the map itself
has a bounded lifetime.
What compiles is this:
*/
struct Builder<'self> {
state: ~[int],
map: &'self mut Map<'self>
}
/*
But the issue is that this says the borrow and the
object lifetime are the same. This becomes an issue as
it causes an "irreversible borrow", the borrow lasts for
the entire lifetime of the object being borrowed.
What I _want_ to write is this:
*/
struct Builder<'a,'b> {
state: ~[int],
map: &'a mut Map<'b>
}
/*
This allows the compiler to seperately calculate the lifetimes
for the borrow and the map, while still ensuring that they last
for the life of the `Builder` object.
I'm currently referring to this bug as "lifetime collision" as
it causes two otherwise unrelated lifetimes to "collide", due to
lack of expressivity
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment