cberner / redb

An embedded key-value database in pure Rust
https://www.redb.org
Apache License 2.0
3.07k stars 137 forks source link

ReadableTable impl for an Either<Table, ReadOnlyTable> #794

Closed rklaehn closed 3 months ago

rklaehn commented 3 months ago

I got some complex functions that I want to work with either a Table<'a> or a ReadOnlyTable.

The trait ReadableTable is made for this. You can have a function that takes an impl ReadableTable<'a, K, V>, and pass in either a Table<'a, K, V> or a ReadOnlyTable<K, V>.

However, there are some downsides to having to take an impl Trait, and it is not possible to construct a concrete type for either a Table or ReadOnlyTable. In my case I want to define a complex iterator that uses multiple tables, and having to drag around the table type is very inconvenient.

Implementing such a concrete type would be simple enough. You need some kind of Either type, and then implement ReadableTable for it assuming that both Left and Right also implement ReadableTable. But the ReadableTable trait is sealed, so this can not be done outside redb.

So either make the trait non-sealed (I suspect there are good reasons against this), or add an either type and the above impl to the crate. The same would have to be done for the ReadableMultimapTable.

All in all it would have to be one either type and impls for all the "readonly view abstraction" traits in the crate.

cberner commented 3 months ago

Actually they were just sealed because I couldn't think of a good use case for them being unsealed, and wanted to minimize the API surface area :)

rklaehn commented 3 months ago

Thanks for the quick response!