Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Key for NonZero… #873

Open
Evrey opened this issue Oct 4, 2024 · 2 comments
Open

Implement Key for NonZero… #873

Evrey opened this issue Oct 4, 2024 · 2 comments

Comments

@Evrey
Copy link

Evrey commented Oct 4, 2024

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.

@cberner
Copy link
Owner

cberner commented Oct 5, 2024

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>

@Evrey
Copy link
Author

Evrey commented Oct 5, 2024

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

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

No branches or pull requests

2 participants