你如何将 ac# 抽象类映射到 Rust?

Bru*_*uce 5 c# traits rust

可能是一个措辞不佳的问题,但这里有一个例子:

给定这些结构;

pub struct Poll {
    _lat: f64,
    _lon: f64,
    _at: i64,
    _heading: f64,
    _speed: f64,
}

pub struct Waypoint {
    _lat: f64,
    _lon: f64,
}
Run Code Online (Sandbox Code Playgroud)

以及这个特质;

pub trait CoordMeasure {
    fn dist_to(&self, other: &Self ) -> f64;
}
Run Code Online (Sandbox Code Playgroud)

如何避免像我所做的那样复制此代码?

impl CoordMeasure for Poll {
    fn dist_to(&self, other: &Poll) -> f64 {
        super::core::distance(self, other)
    }
}

impl CoordMeasure for Waypoint {
    fn dist_to(&self, other: &Waypoint) -> f64 {
        super::core::distance(self, other)
    }
}
Run Code Online (Sandbox Code Playgroud)

我有两次调用相同的函数距离。

pub fn distance<T: Coord>(a: &T, b: &T ) -> f64 {
        let lat1_rads = (90.0 - a.lat()).to_radians();
        let lat2_rads = (90.0 - b.lat()).to_radians();
        let lon_rads = (b.lon() - a.lon()).to_radians();

        let cos_of_lat1 = lat1_rads.cos();
        let cos_of_lat2 = lat2_rads.cos();

        let sin_of_lat1 = lat1_rads.sin();
        let sin_of_lat2 = lat2_rads.sin();

        let cos_of_lons = lon_rads.cos();
        let equation = ((cos_of_lat2 * cos_of_lat1) + (sin_of_lat2 * sin_of_lat1 *    cos_of_lons)).acos();
        6334009.6 * equation
}
Run Code Online (Sandbox Code Playgroud)

这只是重复的一行代码,但在更好的示例中可能会更多。在 C# 中,此代码将在派生自 Waypoint 和 Poll 的抽象类中编写一次。处理这种情况的惯用Rust方式是什么?

Chr*_*gan 4

通用实现是可能的:

\n\n
impl<T: Coord> CoordMeasure for T {\n    fn dist_to(&self, other: &T) -> f64 {\n        super::core::distance(self, other)\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但在这种特殊情况下,您应该完全放弃并将其作为默认方法CoordMeasure实现:Coord

\n\n
trait Coord {\n    \xe2\x80\xa6\n    fn dist_to(&self, other: &Self) -> f64 {\n        super::core::distance(self, other)  // or move its contents in here\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

您可能还想使其能够处理其他类型的other(我不\xe2\x80\x99t 看到任何直接原因为什么other必须与以下类型相同self

\n\n
fn dist_to<Other: Coord>(&self, other: &Other) -> f64 {\n    let lat1_rads = (90.0 - self.lat()).to_radians();\n    let lat2_rads = (90.0 - other.lat()).to_radians();\n    let lon_rads = (b.lon() - self.lon()).to_radians();\n\n    let cos_of_lat1 = lat1_rads.cos();\n    let cos_of_lat2 = lat2_rads.cos();\n\n    let sin_of_lat1 = lat1_rads.sin();\n    let sin_of_lat2 = lat2_rads.sin();\n\n    let cos_of_lons = lon_rads.cos();\n    let equation = ((cos_of_lat2 * cos_of_lat1) + (sin_of_lat2 * sin_of_lat1 *    cos_of_lons)).acos();\n    6334009.6 * equation\n}\n
Run Code Online (Sandbox Code Playgroud)\n