iggy icon indicating copy to clipboard operation
iggy copied to clipboard

Refactor `Storage` trait

Open spetz opened this issue 1 year ago • 3 comments

For example, the load() method expects an empty struct to be passed to load the data, while it's not the case anymore for some components.

spetz avatar Oct 13 '23 15:10 spetz

Seems that refactor to async fn load(&self) -> Result<T, Error>; would be better?

YHM404 avatar Jan 11 '24 14:01 YHM404

Yes, this sounds good, ideally, the storage would just return the data and do not expect mutable struct passed into it to change its internal state. Most of the new methods already work in this way, but the older ones left intact.

spetz avatar Jan 11 '24 15:01 spetz

Yes, this sounds good, ideally, the storage would just return the data and do not expect mutable struct passed into it to change its internal state. Most of the new methods already work in this way, but the older ones left intact.

I have the following suggestions for refactoring the Storage trait to make it more intuitive and extensible

pub trait WithKey {
    type Key;
    fn get_key(&self) -> &Self::Key;
}

#[async_trait]
pub trait NewStorage<T, K>: Sync + Send
where
    T: WithKey<Key = K>,
{
    async fn load(&self, key: &K) -> Result<(), Error>;
    async fn save(&self, component: T) -> Result<(), Error>;
    async fn delete(&self, key: &K) -> Result<(), Error>;
}

but it's a bit difficult to refactor, do you have any suggestions for step-by-step refactoring?

YHM404 avatar Jan 11 '24 15:01 YHM404