profile
viewpoint

Ask questionsFix design of engine_traits for mutual associated types / type bound equality

Part of https://github.com/tikv/tikv/issues/4184, abstracting the storage engine.

My most recent work in https://github.com/tikv/tikv/pull/6122 ran into a problem with the way associated types are used in engine_traits.

The basic problem is that multiple traits have different associated types which must resolve to the same type in generic functions, but I don't know how to make that happen. Here is a simplified example:

trait KvEngine {
    type Snapshot: Snapshot;
    
    fn get_snapshot(&self) -> Self::Snapshot;
}

trait Snapshot {
    type KvEngine: KvEngine;
    
    fn snap(&self, e: &Self::KvEngine);
}

fn do_thing<E>(e: &E) where E: KvEngine {
    let snapshot = e.get_snapshot();
    snapshot.snap(&e);
}

This doesn't compile because:

   |
15 |     snapshot.snap(e);
   |                   ^ expected associated type, found type parameter
   |
   = note: expected type `&<<E as KvEngine>::Snapshot as Snapshot>::KvEngine`
              found type `&E`

I don't know the idiomatic solution to this today.

tikv/tikv

Answer questions nrc

I think Snapshot should not have the engine assoc type - assoc types should be a DAG, IMO.

You can then do something like:

trait KvEngine {
    type Snapshot: Snapshot;
    
    fn get_snapshot(&self) -> Self::Snapshot;
}

trait Snapshot: Sized {
    fn snap<K: KvEngine<Snapshot = Self>>(&self, e: &K);
}

fn do_thing<E>(e: &E) where E: KvEngine {
    let snapshot = e.get_snapshot();
    snapshot.snap(e);
}

You might need some intermediate bounds too - like a trait which is a sub-trait of KvEngine and abstracts over the things Snapshot requires from a KvEngine.

useful!

Related questions

Test failed with error `too many open files` hot 1
combine all binaries into one hot 1
PCP: test hot 1
Deploy tidb with docker swarm, fail to send snap between tikv of different hosts hot 1
Needs protoc dependency hot 1
Failpoint tests will not run when make dev hot 1
Running cargo bench causes panic hot 1
Better memory observation for tidb_query executors hot 1
how to disable gRPC batch mechanism hot 1
Github User Rank List