I am using the specs ECS library and I have the following type
trait TradeableResource{}
#[derive(Component)]
struct MarketMaker<T: TradeableResource + std::marker::Send + std::marker::Sync + 'static>{
lot_size:T
}
This does not compile without the 'static
constraint. I was initially concerned that this would mean that all values of this struct will have to live for the entire life of the program, but looking around, it seems the concern is invalid. Those links clearly deal with the topic but I feel they don't use language that results in a clear mental model for me. So I'd like to make my own statement about the topic, and you tell me if I am correct.
My statement
By using the 'static
constraint, any type that fills the place of T
must either
- Consist entirely of owned values, or
- If it contains referenced values, those must live as long as the lifetime of the program.
So the following specific case would not require anything to live the entire life of the program
#[derive(Component)]
struct Food(f64);
impl TradeableResource for Food{}
fn main() {
let mut world = World::new();
world.register::<MarketMaker<Food>>();
world.create_entity().with(MarketMaker { lot_size: Food(4.0)}).build();
}
Because the type Food
contains only owned values, no references.
Have I got that right?