What is the meaning of static in this context?
fn foo<F: Human + 'static>(param: F) {}
fn main() {
let kate = Kate { age: 30 };
foo(kate);
}
I understand marking global_variable
static
is similar to what static means ie. in c#, space for the variable is allocated in a separate segment of the memory, existing for the whole execution of the program.
static global_variable: i32 = 5;
But what I don't understand what giving 'static constraint means. Is kate
somehow promoted, and her lifetime extended so it now lives for the whole execution of program too?
Or does it simply mean it'll be deallocated as soon as foo
stops using it?
Just like static variables in a class, constraints can be declared as static . A static constraint is shared across all the class instances. Constraints are affected by the static keyword only if they are turned on and off using constraint_mode() method.
Method's behavior is not dependent at all on instance variables. Such method can be declared static, which means no instance of the class is required to run the method.
Static Constraints in SystemVerilog A constraint block can be defined as static by including the static keyword in its definition. constraint block with the static keyword followed by constraint keyword is called as a static constraint. the static constraint is shared across all the class instances.
Putting a constraint like T: 'a
means that all lifetime parameters of the type T
must satisfy the lifetime constraint 'a
(thus, must outlive it).
For example, if I have this struct:
struct Kate<'a, 'b> {
address: &'a str,
lastname: &'b str
}
Kate<'a, 'b>
will satisfy the constraint F: Human + 'static
only if 'a == 'static
and 'b == 'static
.
However, a struct without any lifetime parameter will always satisfy any lifetime constraint.
So as a summary, a constraint like F: 'static
means that either:
F
has no lifetime parameterF
are 'static
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With