Using rust specs register_with_storage

https://docs.rs/specs/0.14.0/specs/world/struct.World.html#method.register_with_storage

Does anyone have an example of how to use that function?

  1. it is what I want – I want the world to specify what underlying storage is used (despite whatever default the Component wants)

  2. I have grepped through source & skimmed through docs – I see definition of this function, but I don’t see any examples on how to use it

Thanks!

    pub fn register<T: Component>(&mut self)
    where
        T::Storage: Default,
    {
        self.register_with_storage::<_, T>(Default::default);
    }

    /// Registers a new component with a given storage.
    ///
    /// Does nothing if the component was already registered.
    pub fn register_with_storage<F, T>(&mut self, storage: F)
    where
        F: FnOnce() -> T::Storage,
        T: Component,
    {
        Self::register_with_storage_internal::<F, T>(&mut self.res, storage);
    }

    /// Registers a new component with a given storage.
    ///
    /// Does nothing if the component was already registered.
    pub(crate) fn register_with_storage_internal<F, T>(res: &mut Resources, storage: F)
    where
        F: FnOnce() -> T::Storage,
        T: Component,
    {
        res.entry()
            .or_insert_with(move || MaskedStorage::<T>::new(storage()));
        res.fetch_mut::<MetaTable<AnyStorage>>()
            .register(&*res.fetch::<MaskedStorage<T>>());
    }

In particular, in the above function definitions, how do we force it to use DenseVec ?

I’m not a specs user, but that’s not what that method does - note the closure given to register_with_storage() must return T::Storage - so the world has no say in the type of it. So it’s just a case of you, the caller, specifying the construction of the storage type, but the type must be the same as what the component specifies.

You’re right. I completely mis read the type signature of “register_with_storage.”

No wonder I couldn’t find useful examples. :slight_smile:

Thanks for explaining.