0

I have an ecs system defined through these 2 objects:

pub struct ECStorage
{
    pub(crate) entity_count : usize,
    pub(crate) entity_components_map : BTreeMap<Entity, HashSet<TypeId>>,
    pub(crate) components : BTreeMap<TypeId, Box<dyn Any>>,
}

pub(crate) struct ComponentStorage<T>
{
    entity_component_map : BTreeMap<Entity, usize>,
    components : Vec::<T>,
}

In this case the Any boxes in the ECS are all refcells of ComponentStorage.

I am running into an issue here:

pub fn component_query<T>(&mut self) -> std::slice::IterMut<T>
where T : 'static
{
    let type_id = TypeId::of::<T>();
    assert!(self.components.contains_key(&type_id));

    let storage : &mut RefCell<ComponentStorage::<T>> =
        self.components.get_mut(&type_id).unwrap().downcast_mut().unwrap();

    storage.borrow_mut().components.iter_mut()
}

The borrow there is very short lived, so the iterator doesn't live long enough to allow me to return it. How do I specify that the borrow lasts as long as the scope of the caller?

Aiden4
  • 2,504
  • 1
  • 7
  • 24
Makogan
  • 8,208
  • 7
  • 44
  • 112
  • [The duplicate applied to your case](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=5ca9786d96f33fbdf70b0a5655439445). – Chayim Friedman Sep 11 '22 at 03:10

0 Answers0