Trait MetricSpace

Source
pub trait MetricSpace<UserImplementationType = ()> {
    type UserData;
    type Distance: Copy + PartialOrd + Bounded + Add<Output = Self::Distance>;

    // Required method
    fn distance(
        &self,
        other: &Self,
        user_data: &Self::UserData,
    ) -> Self::Distance;
}
Expand description

Elements you’re searching for must be comparable using this trait.

You can ignore UserImplementationType if you’re implementing MetricSpace for your custom type. However, if you’re implementing MetricSpace for a type from std or another crate, then you need to uniquely identify your implementation (that’s because of Rust’s Orphan Rules).

impl MetricSpace for MyInt {/*…*/}

/// That dummy struct disambiguates between yours and everyone else's impl for a tuple:
struct MyXYCoordinates;
impl MetricSpace<MyXYCoordinates> for (f32,f32) {/*…*/}

Required Associated Types§

Source

type UserData

This is used as a context for comparisons. Use () if the elements already contain all the data you need.

Source

type Distance: Copy + PartialOrd + Bounded + Add<Output = Self::Distance>

This is a fancy way of saying it should be f32 or u32

Required Methods§

Source

fn distance(&self, other: &Self, user_data: &Self::UserData) -> Self::Distance

This function must return distance between two items that meets triangle inequality. Specifically, it MUST NOT return a squared distance (you must use sqrt if you use Euclidean distance)

  • user_data —Whatever you want. Passed from new_with_user_data_*()

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§