我应该使用enum来模拟多态性还是将trait与Box< trait>一起使用?反而? [英] Should I use enum to emulate the polymorphism or use trait with Box<trait> instead?

查看:101
本文介绍了我应该使用enum来模拟多态性还是将trait与Box< trait>一起使用?反而?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用enum Axes限制CoordinateQuaternion:

#[derive(Clone)]
pub enum Axes {
    Coordinate {x: f64, y: f64, z: f64, reserve: Vec<f64>,},
    Quaternion {x: f64, y: f64, z: f64},
}

impl Axes {
    pub fn shift(&mut self, Sample: &Axes) -> () {
        let Dup: Axes = self.clone();
        match Dup {
            Axes::Coordinate {x, y, z, reserve} => {
                match &Sample {
                    Axes::Coordinate {x, y, z, reserve} => {
                        *self = Axes::Coordinate {x: *x, y: *y, z: *z, reserve: reserve.to_vec()};
                    }
                    _ => panic!(),
                }
            }
            Axes::Quaternion {x, y, z} => {
                match &Sample {
                    Axes::Quaternion {x, y, z} => {
                        *self = Axes::Quaternion {x: *x, y: *y, z: *z};
                    }
                    _ => panic!(),
                }
            }
        }
    }
}

使用特征Axes链接struct CoordinateQuaternion:

pub trait Axes {
    fn shift(&mut self, Sample: &Axes) -> ();
    fn fold(&mut self, Sample: &Axes) -> ();
}

pub struct Coordinate {
    pub x: f64,
    pub y: f64,
    pub z: f64,
    pub reserve: Vec<f64>,
}

pub struct Quaternion {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Axes for Coordinate {
    fn shift(&mut self, Sample: &Axes) -> () { }
    fn fold(&mut self, Sample: &Axes) -> () { }
}

impl Axes for Quaternion {
    fn shift(&mut self, Sample: &Axes) -> () { }
    fn fold(&mut self, Sample: &Axes) -> () { }
}

在这种情况下,struct的特征是否更易于访问和更有效?我对在什么情况下使用哪种感到困惑.

Is trait with struct in this case more accessible and more efficient? I am sort of confused of which to use under what cases.

推荐答案

根据情况使用特征和枚举之间的最大区别之一是它们的可扩展性.如果将Axes设为枚举,则这两个选项将硬编码到类型中.如果要添加轴的第三种形式,则必须修改类型本身,这可能会涉及使用Axes的代码的大量修改(例如,您可能需要在Axes上匹配的任何位置)进行更改).另一方面,如果使Axes为特征,则可以通过定义新类型并编写适当的实现方式来添加其他类型的轴,而无需完全修改现有代码.这甚至可以从库外部完成,例如由用户.

One of the big differences between using traits and enums for your situation is their extensibility. If you make Axes an enum, then the two options are hardcoded into the type. If you want to add some third form of axis, you'll have to modify the type itself, which will probably involve a lot of modifications to the code with uses Axes (e.g. anywhere you match on an Axes will probably need to be changed). On the other hand, if you make Axes a trait, you can add other types of axes by just defining a new type and writing an appropriate implementation, without modifying existing code at all. This could even be done from outside of the library, e.g. by a user.

要考虑的另一重要事项是您需要对结构的内部进行多少访问.使用枚举,您可以完全访问结构中存储的所有数据.如果要编写使用特征可以同时在CoordinateQuaternion上运行的函数,则只能执行Axes特征中描述的那些操作(在本例中为ShiftShift Fold).例如,给出您提供的Axes实现,将无法通过Axes接口简单地检索(X,Y,Z)元组.如果需要在某个时候这样做,则必须添加一个新方法.

The other important thing to consider is how much access you need to the internals of the structs. With an enum, you get full access to all the data stored within the struct. If you want to write a function which can operate on both Coordinate and Quaternion using a trait, then the only operations you will be able to perform are those described in the Axes trait (in this case Shift and Fold). For instance, giving the implementation of Axes you gave, there would be no way for you to simply retrieve the (X,Y,Z) tuple via the Axes interface. If you needed to do that at some point, you would have to add a new method.

在不了解您计划如何使用这些类型的更多信息的情况下,很难确定这些选项中哪个是更好的选择,但是如果是我,我可能会使用一个枚举.最终,它很大程度上取决于偏好,但是希望这可以使您对做出决定时要考虑的事情有所了解.

Without knowing more about how you plan to use these types it's difficult to say for sure which of these options is the better choice, but if it were me I would probably use an enum. Ultimately, it comes down largely to preference, but hopefully this will give you some idea of the sorts of things to be thinking about when making your decision.

这篇关于我应该使用enum来模拟多态性还是将trait与Box&lt; trait&gt;一起使用?反而?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆