gilcu2
gilcu2

Reputation: 353

Ensure that a trait implementation in rust satisfy properties

I am making a trait to define a distance in a metric space like:

trait Metric<T> {
    fn distance(o1: &T, o2: &T) -> f64;
}

and I want that any implementation satisfy some properties, for example:

distance(o, o) = 0.0

Exist a way in rust to enforce that?

Upvotes: 2

Views: 404

Answers (1)

Some Guy
Some Guy

Reputation: 16190

You can use the trait_tests crate, though I do believe the crate is merely an experiment, so there may be rough edges. Specifically, I couldn't figure out how to actually test all implementations of Metric<T>, rather only for a concrete type, Metric<i32>.

For your example:

use trait_tests::*;

pub trait Metric<T> {
    fn distance(o1: &T, o2: &T) -> f64;
}

#[trait_tests]
pub trait MetricTests: Metric<i32> {
    fn test_distance() {
        // These could possibly be extended using quickcheck or proptest
        assert!(Self::distance(&42, &42) == 0.0);
    }
}

struct CartesianPlane {}

#[test_impl]
impl Metric<i32> for CartesianPlane {
    fn distance(o1: &i32, o2: &i32) -> f64 {
        (*o2 - *o1) as f64
    }
}

Then cargo test should include the auto-generated test for the implementors of the trait that are annotated with #[test_impl].

Upvotes: 2

Related Questions