0

I have this Parameter trait that I use to wrap access to resources which are acquired based on the type of the Parameter implementation.

The closure on the call method is supposed to take a reference to the acquired resource but I cannot figure out how to specify the trait so that the life of the parameter is tied to that of the call function.

trait Parameter where Self: Sized {
    fn call<F: FnOnce(Self)>(orchestrator: &Orchestrator, systems_data: &std::sync::RwLockReadGuard<'_, SystemsData>, closure: F);
}

impl <'a, T: Entity + 'a> Parameter for &'a T {
    fn call<F: FnOnce(Self)>(orchestrator: &Orchestrator, systems_data: &std::sync::RwLockReadGuard<'_, SystemsData>, closure: F) {
        let arc = &systems_data.systems[&systems_data.systems_by_name[T::class()]];
        let b = arc.read().unwrap();
        let b = b.downcast_ref::<T>().unwrap();

        closure(b);
    }
}

I always ended up locking myself into a corner, so any other way to approach this problem is also valid.

Facundo Villa
  • 111
  • 2
  • 12

0 Answers0