Implement `Key` for `NonZero…`
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.
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>
Oh that’s right. So Option<NonZero…> would have to wait or be put behind a feature = "unstable".
Noting that this is blocked on https://github.com/rust-lang/rust/issues/31844