I want to write a basic abstraction of a database, using Rust traits.
pub trait Keyable {
type Key;
fn key(&self) -> &Self::Key;
}
and
// type alias for result - DbError are defined elsewhere
pub type Result<T> = Result<T, DbError>;
pub trait Database {
type Item;
fn get(&self, id: Keyable) -> Result<Self::Item>;
fn upsert(&self, item: Self::Item) -> Result<Keyable>;
}
I am struggling to express this: Database::Item must be at least Keyable.
There are two use cases:
- I can use some arbitrary type which knows how to return a key() in my Database::get(k: Keyable) function and yet return some Item (which is likely to have quite a bit more than just the key().
- In my fn Database::upsert() function, I want to be able to use the fact that the item is at least Keyable and therefore access the key() to lookup the database to make a decision if I simply insert or update.