Home > Blockchain >  Is it possible to combine type constraints in Rust?
Is it possible to combine type constraints in Rust?

Time:12-17

I've been working in quite a number of statically-typed programming languages (C , Haskell, ...), but am relatively new to Rust.

I often end up writing code like this:

struct LeafNode<K: Ord   Default   Clone, V: Default   Clone> {
  keys: Vec<K>,
  values: Vec<V>,
}

impl <K: Ord   Default   Clone, V: Default   Clone> LeafNode<K, V> {
  // ...
}

There is a lot of repetition here in the type constraints. This is compounded further when LeafNode is used inside something else (say, when building a tree of nodes). Whenever one of these constraints changes during implementation, there are many places where the code needs to be altered as they are named in many of the different struct definitions and impl blocks.

Is there a way to create some kind of 'type constraint alias' from K: Ord Default Clone and V: Default Clone?

If it exists, it probably has some name that I am not aware of, which makes searching for this technique very difficult. Hence this question.

CodePudding user response:

You can accomplish this by making your own trait that takes the other traits as a bound, then add a blanket implementation for it:

trait MyConstraint: Ord   Default   Clone {}
impl <T: Ord   Default   Clone> MyConstraint for T {}

struct LeafNode<K: MyConstraint> {
    keys: Vec<K>
}

impl<K: MyConstraint> LeafNode<K> {
    fn keys(&self) -> &[K] {
        &self.keys
    }
}

CodePudding user response:

As a complement answer.

You can use trait alias (currently unstable):

#![feature(trait_alias)]

trait MyConstraint = Ord   Default   Clone;

struct LeafNode<K: MyConstraint> {
    keys: Vec<K>
}

impl<K: MyConstraint> LeafNode<K> {
    fn keys(&self) -> &[K] {
        &self.keys
    }
}

Playground

  • Related