khonsulabs / bonsaidb

A developer-friendly document database that grows with you, written in Rust
https://bonsaidb.io/
Apache License 2.0
998 stars 37 forks source link

Added ViewSchema::MappedKey #288

Closed ecton closed 1 year ago

ecton commented 1 year ago

Closes #284

This commit adds an associated type to ViewSchema which is used in map and reduce. This type uses a generic associated lifetime, which allows for the map/reduce functions to utilize borrowed data.

The extra associated type is an unfortunate side effect of how lifetime extension with generic associated types currently works -- see #287 for where the exploration ended when trying to put the associated type on Key instead.

Thanks to @asonix for their help in various ideas while experimenting on how to make this work!

ecton commented 1 year ago

I've come back to this branch a few times over the past week, and at the end of the day, I think that default associated types is potentially the solution to improving the usability of this. I thought of introducing "alias" traits that automatically implement the base trait for T when it fits the requirements, and specifying Self for the associated type. However, I already want to do this for the View associated type -- and that was already something I decided I was willing to wait for the feature in Rust to improve the usability of -- or perhaps more derive macros.