Skip to content

kvstore: singleton defaults - #468

Merged
nrc merged 2 commits into
mainfrom
nrc/kv-single-default2
Oct 31, 2026
Merged

nrc merged 2 commits into
mainfrom
nrc/kv-single-default2

Conversation

@nrc

@nrc nrc commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Closes #357

Most of the interesting stuff is in the first commit. This adds a flag default to singleton declarations in the store! macro, and these are never None. If they would be None, they are the Default::default value. 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 is SingletonSlot::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.

Signed-off-by: Nick Cameron <nrc@ncameron.org>
Signed-off-by: Nick Cameron <nrc@ncameron.org>
@nrc
nrc force-pushed the nrc/kv-single-default2 branch from 3aa7fe1 to 604aaac Compare October 29, 2026 03:13
Comment on lines +310 to +314
/// 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.,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)]?

@nrc
nrc merged commit 5dfd5a0 into main Oct 31, 2026
23 checks passed
@nrc
nrc deleted the nrc/kv-single-default2 branch October 31, 2026 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kvstore: defaults for singletons

2 participants