Function life in Rust

So, I'm trying to compress callbacks to this enumeration ( Visual) option - which will be saved in a vector, as shown inside a struct EntityComponents:

enum Component {
    Position([f64; 2]),
    Visual(& Fn(Entity) -> ()),
}

struct EntityComponents {
    components_of_entity: HashMap<TypeId, Vec<Component>>,
}

However, Rust requires me to provide explicit lifetime parameters here. My idea is that I want the function reference to be at least as long as its argument ( Entity), but I don't know what the syntax for this will look like? Is it possible?

The idea is that as long as it Entityhas a component Visual, we can use this callback to display it!

+4
source share
1 answer

Few things:

  • fn() &Fn(). Fn, ol. ( , ), , - .
    • , , , , Box<Fn()>, .
    • , , Visual(fn(Entity)),. . , .
    • , Fn, - ? , :
    • Entity: .
    • - : <'a>
  • Fn(Entity) -> (). -> () , .
+4

Source: https://habr.com/ru/post/1684244/


All Articles