You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
x (strictly) outlives y: [starttime(x), endtime(x)] contains [starttime(y), endtime(y)].
Rules
References are never null
To make sure the references with the lifetime 'a in a struct T, defined by struct T<'a>, are never null(rule 1), the lifetime 'a must cover the lifetime of the struct T variable. That is, the references with lifetime 'a in Toutlive the variable with struct T in an inclusive manner, and therefore we can make sure the references with lifetime 'a in T must be alive when T variable is alive.
it's natural that a variable outlives the references to it(The referents outlive references). For example, the variable with type T referred by r outlives the reference r with lifetime 'a (without impl Drop for T), where 'r is defined by r: &'a T. For example, the x must live at least as long as 'a in the code Struct T {..} Struct S<'a> { r: &'a T } let x: T = ...; let s = S { r: &x };.
By Drop Check, the presence of Drop for T will force that struct T variable strictly outlives any reference to the struct T variable. That is, r: &'a mut T with impl Drop for T requires the variable referred by r strictly outlives the reference r. For example, the x must live longer than 'a in the code Struct T {..} impl Drop for T {..} Struct S<'a> { r: &'a T } let x: T = ...; let s = S { r: &x };.
Syntax
Lifetime Subtyping: 'a: 'b means the references with 'a outlives the references with 'b. That is, lifetime 'a that lives at least as long as 'b, or 'a
Discussion
Will the lifetime 'a be different for a reference with &'a T and a reference with &'a mut T ?
No ?
What is the relationship of 'a, 'b, and lifetime(x) within code Struct S<'a, 'b> { r: &'a T<'b> }; let x: T = ...; let s = S { r: &x } ?
By rule 2: T<'b> implies 'b outlives the variable x
Without implementing Drop for T, by rule 3: &'a T implies x outlives a
'b outlives x and x outlives a
With implementing Drop for T, by rule 4: &'a T implies xstrictly outlives a
'b outlives x and xstrictly outlives a
Thus, in any case, 'b must outlive 'a. We should add 'b: 'a in Struct S: Struct S<'a, 'b: 'a> { r: &'a T<'b> };.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When 'a and 'b are referred as the lifetimes of two different variables a and b
, where the lifetimes(c) is the range from starttime(c) to endtime(c) (both values are included)
, denoted by: [starttime(c), endtime(c)]
, the meanings of "a outlives b" and "astrictly outlives b"
can be represented by the following figures with the definitions:
'a = lifetime(a) = [starttime(a), endtime(a)]
'b = lifetime(b) = [starttime(b), endtime(b)]
a outlives b
|
+---+- 'b --+- 'a
| | |
| | |
+---+-------+
|
v
Time
starttime(a) = starttime(b)
endtime(a) = endtime(b)
|
+---+- 'b --+- 'a
| | |
| | |
+---+ |
| |
+-----------+
|
v
Time
starttime(a) = starttime(b)
endtime(a) > endtime(b)
|
+-----------+- 'a
| |
+---+- 'b |
| | |
| | |
+---+-------+
|
v
Time
starttime(a) < starttime(b)
endtime(a) = endtime(b)
|
+-----------+- 'a
| |
+---+- 'b |
| | |
| | |
+---+ |
| |
+-----------+
|
v
Time
starttime(a) < starttime(b)
endtime(a) > endtime(b)
Thus, a outlives b can be represented as 'a ⊇ 'b.
That is, lifetime(a) ⊇ lifetime(b).
astrictly outlives b
|
+---+- 'b --+- 'a
| | |
| | |
+---+ |
| |
+-----------+
|
v
Time
starttime(a) = starttime(b)
endtime(a) > endtime(b)
|
+-----------+- 'a
| |
+---+- 'b |
| | |
| | |
+---+ |
| |
+-----------+
|
v
Time
starttime(a) < starttime(b)
endtime(a) > endtime(b)
Thus, astrictly outlives b can be represented as ('a ⊃ 'b) ∧ (end of 'a > end of 'b).
That is, (lifetime(a) ⊃ lifetime(b)) ∧ (endtime(a) > endtime(b)).