SergioBenitez / state

A Rust library for safe and effortless global and thread-local state management.
Other
209 stars 13 forks source link

Container Iterator #6

Closed SOF3 closed 3 years ago

SOF3 commented 4 years ago

There is no way to list all values from Container. Getting an iterator is probably not very useful since values have distinct types. What I really need is to have a way to debug-print the Container.

SergioBenitez commented 4 years ago

There's no way to implement this safely unless we add a Debug bound to all set methods. I'm not sure the trade-off is worth it. This is reminiscent of #3 where what we'd really like is to have some sort of polymorphic trait bounds. In some pseudo-Rust, we might write:

struct Container<bounds B>(...);

impl<bounds B> Container<B> {
    fn set<T: B>(value: T) { /* here we know `T: B`, even if we erase the type */ }
}

impl<bounds B: Debug> Debug for Container<B> {
    /* now we can make use of the fact that all erased `T`s implement `Debug` */
}

Alas, this does not exist in Rust. The alternative is to have different versions of Container with different bounds, or to have a macro that somehow safely generates a definition for Container.

SOF3 commented 4 years ago

What is missing in Rust here?

SergioBenitez commented 3 years ago

I've come up with a mechanism to make something ~like this work, albeit not fully generically. If you or anyone would like to implement variants of Container with Debug bounds, feel free to do so.

SergioBenitez commented 3 years ago

Container now implements a very basic Debug. I think this is as good as it gets, given all of the type erasure.