redb icon indicating copy to clipboard operation
redb copied to clipboard

Implement `Key` for `NonZero…`

Open Evrey opened this issue 1 year ago • 3 comments

Hello, there!

So, i’m modeling a bunch of relational data in redb, and i’m using all the NonZero… types as primary keys for their Option<NonZero…> optimisations where i store foreign keys. Thanks to the orphan rule, i currently have to jump through ugly wrapper type hoops to get that done. It would be nice if a built-in solution could be provided instead.

Hypothetically, it is possible that an invalid primary key 0 somehow makes its way into the database file, which means that reading a NonZero… primary key from the database is fallible. Here’s a few suggestions for what could be done in that should-not-happen case, and which one i’d favour:

  • Treat reading a 0 key as »no entry found«, returning None. I’d prefer this behaviour.
    • Keeps the app running, delegating this case to special logic an app already has to consider.
    • However, this means that ID=0 data inside the database is a zombie data resource leak.
  • Add a new error return code, for ID=0 is an example of database corruption.
    • Pretty harsh, though, for otherwise the DB seems to work fine, and it’s just unreachable data.

It would be good to then also specialise Option<NonZero…> to just map to .unwrap_or(0) as a redb::Value.

Evrey avatar Oct 04 '24 22:10 Evrey

I think the Rust specialization feature is required to implement it for Option<NonZero> because there is already an impl for Option<T>. You probably need to implement your own MyKey that wraps an Option<NonZero>

cberner avatar Oct 05 '24 15:10 cberner

Oh that’s right. So Option<NonZero…> would have to wait or be put behind a feature = "unstable".

Evrey avatar Oct 05 '24 19:10 Evrey

Noting that this is blocked on https://github.com/rust-lang/rust/issues/31844

cberner avatar Jun 07 '25 00:06 cberner