Rust Traits with Type Bounds

Viewed 391

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:

  1. 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().
  2. 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.
1 Answers

You can place a trait bound on an associated type:

pub trait Database {
    type Item: Keyable;

Then, you can use that trait's associated type(s) in function signatures:

    fn get(&self, id: &<Self::Item as Keyable>::Key) -> Result<Self::Item>;    
    fn upsert(&self, item: Self::Item) -> Result<<Self::Item as Keyable>::Key>;

(I also added an & because get() probably shouldn't be required to consume the key data.)

Putting that all together to a compilable example:

pub enum DbError { Something }
pub type Result<T> = std::result::Result<T, DbError>;

pub trait Keyable {
    type Key;
    fn key(&self) -> &Self::Key;
}

pub trait Database {
    type Item: Keyable;

    fn get(&self, id: &<Self::Item as Keyable>::Key) -> Result<Self::Item>;    
    fn upsert(&self, item: Self::Item) -> Result<<Self::Item as Keyable>::Key>;
}
Related