Skip to content

Convert some methods to trait methods #30

@timschmidt

Description

@timschmidt

Via Andrei:

Take for example, this:

    /// Returns a new CSG translated by x, y, and z.
    ///
    pub fn translate(&self, x: Real, y: Real, z: Real) -> CSG<S> {
        self.translate_vector(Vector3::new(x, y, z))
    }
    
    /// Returns a new CSG translated by vector.
    ///
    pub fn translate_vector(&self, vector: Vector3<Real>) -> CSG<S> {
        let translation = Translation3::from(vector);
        
        // Convert to a Matrix4
        let mat4 = translation.to_homogeneous();
        self.transform(&mat4)
    }

Rust does not have function overloading, but what could be done in this case, is something like:

    pub fn translate(&self, vector: impl Into<Vector>) -> Self {
        let translation = Translation::from(vector);
        let mat = translation.to_homogenous();
        self.transform(&mat);
    }

If this method is on a trait, and the trait bounds are correct, then as long as CSG2D & CSG3D implement the transform(&self, ...) -> Self, they get translate for free, as the default trait method.
On top of that, Into<Vector> can be implemented for all kinds of stuff, so you could do a.transform((1, 2, 3)), a.transform([1.0, 2.0, 3.0]) if you'd like with just one method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions