Skip to content

Prevent invalid implementation of PrimaryKey and Prefixer outside of storage-plus #10

@hashedone

Description

@hashedone

Implementing PrimaryKey and Prefixer outside of storage-plus doesn't seems scary at all (you just want to use your type as a key, why not to?), but there is an issue - if done improperly it may break things. Trivial example is making a type like:

struct FancyKeyType {
  secondary: U32Key,
  primary: U32Key,
};

With an Ord implementation which compares firstly on primary, then on secondary. Then when calling .range(..) on such key, I expect them being sorted with my Ord implementation, but they come in different one, because they are sorted basing on their internal binary representation (basically bitwise lexografical order is used). And even worse, when using Bounds on such key I would get random items.

I have 3 proposal to work it around:

  • Making PrimaryKey and Prefixer unsafe - this would not prevent from implementing them externally, but it would bring attention to the reasons why they are marked as not safe to implement. In the documentation there should be an additional explanation about how keys are compared and how to properly implement them.
  • Making PrimaryKey and Prefixer sealed traits which is trivial, but prevents implementing them outside the crate at all. (which might be useful for some custom keys when done correctly)
  • Making internal comparisons using actual Ord, but it would require reversing conversions of key to &[u8], so it is blocked by Better return values from range/prefix cw-plus#198.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions