struct Vector {
data: [f32; 2]
}
impl Vector {
//many methods
}
今度はNormal
を作成したいと思いますが、ほぼVector
のように動作しますが、タイプを区別する必要があります。たとえば、法線を変換することはベクトルを変換することとは異なります。たとえば、トランジションされた(逆の)行列で変換する必要があります。Rustの機能を共有するには?
今、私はこのようにそれを行うことができます:
struct Normal {
v: Vector
}
そして私は、私はまた、その後PhantomData
struct VectorType;
struct NormalType;
struct PointType;
struct Vector<T = VectorType> {
data: [f32; 2],
_type: PhantomData<T>,
}
type Normal = Vector<NormalType>;
でそれを行うしかし、と思うすべての機能
impl Normal {
fn dot(self, other: Normal) -> f32 {
Vector::dot(self.v, other.v)
}
....
}
を再実装特定のタイプの機能を実装する方法も必要です。
を追加することができるように、すべての場合にはadd
のように実装するのは簡単です。
それとも何らかの形
impl Vector<NormalType> {..} // Normal specific stuff
私はサブレンジのための機能を実装する方法を確認しない固有の機能を提供します。例えば、ドットプロダクトは法線とベクトルだけに意味があり、点は意味を成さないかもしれません。
特性境界のブール表現を表現することは可能ですか?
trait NormalTrait;
trait VectorTrait;
impl NormalTrait for NormalType {}
impl VectorTrait for VectorType {}
impl<T: PointTrait or VectorTrait> for Vector<T> {
fn dot(self, other: Vector<T>) -> f32 {..}
}
私の選択肢には何がありますか?
「PhantomData」の必要はありません。単に 'T 'と置き換えてください。 –
Shepmaster
(私は思う)。バイトオーダーの仕方のようなもの。 – Shepmaster
特性境界にブール式のRFCがありますか? –