You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Would you be open to changing the SharedState API not require use of Default for setting the initial value? (This would also make it more like the RwLock API.) In my case, I am using SharedState to wrap an object which I instantiate with a constructor called load because it needs to do filesystem IO. I could make it implement Default, but I'd prefer not to have a Default implementation that does IO because I want it to be clear that creating new instances of this object should be done sparingly. So I'd like to have some way to create a SharedState by passing in an initial value that I've already created.
I'd propose an API that better mirror's RwLock's API:
Maybe a problem with that proposal that SharedState::default() is not const which I'm gathering is maybe a use case you want to make easy for Relm users? If that's the case, maybe also adding pub const fn from_default() -> SharedState<Data> would solve that? (I'm pretty new to Rust, and not very familiar with const and with when lazy initialized statics are needed.)
An alternative (or additional) proposal would be to leave the existing SharedState::new, and add impl<Data> From<Data> for SharedState<Data>. Imo, this still leaves SharedState::new as being a bit weird for someone familiar with RwLock, but it would solve my need, and would avoid a breaking change to SharedState::new.
If there's agreement on an API change, I expect I'd be able to make a PR to implement it if that's helpful.
The text was updated successfully, but these errors were encountered:
I'm open for this change, in fact it would be relatively easy to add this. Shared state is based on once_cell::Lazy and the only change would be adding a method that passes a function instead of Default::default() to Lazy::new().
Would you be open to changing the
SharedState
API not require use ofDefault
for setting the initial value? (This would also make it more like theRwLock
API.) In my case, I am usingSharedState
to wrap an object which I instantiate with a constructor calledload
because it needs to do filesystem IO. I could make it implementDefault
, but I'd prefer not to have aDefault
implementation that does IO because I want it to be clear that creating new instances of this object should be done sparingly. So I'd like to have some way to create aSharedState
by passing in an initial value that I've already created.I'd propose an API that better mirror's
RwLock
's API:Maybe a problem with that proposal that
SharedState::default()
is notconst
which I'm gathering is maybe a use case you want to make easy for Relm users? If that's the case, maybe also addingpub const fn from_default() -> SharedState<Data>
would solve that? (I'm pretty new to Rust, and not very familiar withconst
and with when lazy initialized statics are needed.)An alternative (or additional) proposal would be to leave the existing
SharedState::new
, and addimpl<Data> From<Data> for SharedState<Data>
. Imo, this still leavesSharedState::new
as being a bit weird for someone familiar withRwLock
, but it would solve my need, and would avoid a breaking change toSharedState::new
.If there's agreement on an API change, I expect I'd be able to make a PR to implement it if that's helpful.
The text was updated successfully, but these errors were encountered: