Repository navigation
kvstore: singleton defaults - #468
Conversation
Signed-off-by: Nick Cameron <nrc@ncameron.org>
Signed-off-by: Nick Cameron <nrc@ncameron.org>
3aa7fe1 to
604aaac
Compare
| /// By default (ha!), a singleton is optional: reading it returns an `Option`, and deleting it | ||
| /// leaves it with no value (`None`). A singleton declared with `default` always has a value, which is | ||
| /// initially `ValueType::default()`: reading it returns the bare value (not an `Option`), and | ||
| /// deleting it resets it to `ValueType::default()`. To use `default`, the type of the value | ||
| /// must implement `Default`. E.g., |
There was a problem hiding this comment.
Hmm, why have split cases here? Option<T> always implements Default, so you could require that singletons impl Default and have the user pass Option<T> if they want the optionality, no?
There was a problem hiding this comment.
Ah, I guess reading further you may want to have the semantics of singletons being added and removed? Hmm, I'm not sure I buy that you want those ops to even be possible for singletons -- my mental model would be that they exist for all time (since the storage slot for them does) and the kvstore's job is primarily to provide the relevant acid properties, as opposed to making them look like special at-most-one-row tables. Not pushing very hard on that, this works fine for me, just thoughts
There was a problem hiding this comment.
Oh, right, and I forgot singletons are now held in a HashMap rather than as fields, so you do save something this way (but only if they may not ever be written)
There was a problem hiding this comment.
SIngletons are fields and they're stored as Option (VersionedValue<Option<T>>), so using Option explicitly would even save us some memory (if the type being stored doesn't have a niche). I guess the question is just whether we want the explicit Options in the API?
| /// Storage for a singleton declared with `default`, which always has a value. | ||
| /// | ||
| /// Deleting the value is just writing the default value (so it is notified as an upsert). | ||
| #[doc(hidden)] |
There was a problem hiding this comment.
idle thought: might it be worth moving the hidden api surface to a different crate (ts_kv_store_raw or similar) so you don't have to annotate everything #[doc(hidden)]?
Closes #357
Most of the interesting stuff is in the first commit. This adds a flag
defaultto singleton declarations in thestore!macro, and these are never None. If they would be None, they are theDefault::defaultvalue. This is quite a significant change to the internals since we do this in a type-safe way (i.e., we know statically whether a function will return an Option or not). The key type for that isSingletonSlot::Maybe.The second commit is a bunch of refactorings which are either possible because of the changes to the internals or which occurred to me while doing the first part.
AI decl: I used Claude a lot for this PR. I have iterated and reviewed.