Hey guys. I'm using the redb
crate which has quite some confusing lifetimes which seem to be getting in the way of implementing generic traits. I've made some progress, however the following error has me confused. I'll try to include as much code as possible to demonstrate the error. I have this trait:
pub trait MyGet<'a, K, V>: Sized + redb::ReadableTable<K, V> + 'a
where
for<'b> K: redb::Key<SelfType<'b> = K> + 'b,
for<'b> V: redb::Value<SelfType<'b> = V> + 'b,
{
fn my_get(&'a self, k: K) -> Result<Option<redb::AccessGuard<'_, V>>, redb::StorageError> {
self.get(k)
}
}
impl<'a, K, V> MyGet<'a, K, V> for redb::ReadOnlyTable<K, V>
where
for<'b> K: redb::Key<SelfType<'b> = K> + 'b,
for<'b> V: redb::Value<SelfType<'b> = V> + 'b,
{
}
The following test fails:
fn gets_table<K, V>() -> ReadOnlyTable<K, V>
where
for<'b> K: redb::Key<SelfType<'b> = K> + 'b,
for<'b> V: redb::Value<SelfType<'b> = V> + 'b,
{
unimplemented!()
}
#[test]
fn test_my_get_with_borrow() {
let table = gets_table::<&[u8; 16], &[u8; 16]>();
table.my_get(&[0; 16]);
}
Output:
error[E0599]: the method `my_get` exists for struct `ReadOnlyTable<&[u8; 16], &[u8; 16]>`, but its trait bounds were not satisfied
--> db/src/lib.rs:64:11
|
64 | table.my_get(&[0; 16]);
| ^^^^^^ method cannot be called on `ReadOnlyTable<&[u8; 16], &[u8; 16]>` due to unsatisfied trait bounds
|
::: /home/u/.cargo/git/checkouts/redb-24e44532b0b35edd/a813740/src/table.rs:426:1
|
426 | pub struct ReadOnlyTable<K: Key + 'static, V: Value + 'static> {
| -------------------------------------------------------------- doesn't satisfy `_: MyGet<'_, &[u8; 16], &[u8; 16]>`
|
note: trait bound `<&[u8; 16] as redb::Value>::SelfType<'b> = &[u8; 16]` was not satisfied
--> db/src/lib.rs:42:26
|
40 | impl<'a, K, V> MyGet<'a, K, V> for redb::ReadOnlyTable<K, V>
| --------------- -------------------------
41 | where
42 | for<'b> K: redb::Key<SelfType<'b> = K> + 'b,
| ^^^^^^^^^^^^^^^^ unsatisfied trait bound introduced here
43 | for<'b> V: redb::Value<SelfType<'b> = V> + 'b,
| ^^^^^^^^^^^^^^^^ unsatisfied trait bound introduced here
For more information about this error, try `rustc --explain E0599`.
error: could not compile `db` (lib test) due to 2 previous errors
However within the redb
crate, there are the following implementations:
pub trait Key: Value {
// ...
}
impl Value for &[u8] {
type SelfType<'a> = &'a [u8]
where
Self: 'a;
// ...
}
impl<const N: usize> Value for &[u8; N] {
type SelfType<'a> = &'a [u8; N]
where
Self: 'a;
// ...
}
I've tried my best to imitate the bounds of the crate but it is somewhat failing. I'm sure this is an issue of lifetimes, however the error message doesn't really communicate where exactly the error is. Could someone provide some guidance?