Skip to main content

Vector3Arg

Trait Vector3Arg 

Source
pub trait Vector3Arg<T: Vector3Coordinate> {
    // Required method
    fn borrow_vec(&self) -> &Vector3<T>;
}
Expand description

Trait for accepting either a reference to a Vector3 or a Vector3 by value (if it is Copy).

§Examples

Copy types (like f64) can be passed by value or reference:

use vec3_rs::Vector3;
let v1: Vector3<f64> = Vector3::new(1.0, 0.0, 0.0);
let v2: Vector3<f64> = Vector3::new(0.0, 1.0, 0.0);

// Both work perfectly because `f64` is `Copy`
let _ = v1.dot(v2);
let _ = v1.dot(&v2);
let _ = v1.angle(v2);
let _ = v1.angle(&v2);

Non-Copy types can ONLY be passed by reference:

use vec3_rs::{Vector3, Vector3Coordinate};

fn test_non_copy_by_value<T: Vector3Coordinate>(v1: Vector3<T>, v2: Vector3<T>) {
    // This fails to compile because `T` is not guaranteed to be `Copy`.
    // `Vector3Arg` restricts pass-by-value strictly to `Copy` types.
    v1.dot(v2);
}

But passing by reference works fine for generic non-Copy types:

use vec3_rs::{Vector3, Vector3Coordinate};

fn test_non_copy_by_ref<T: Vector3Coordinate>(v1: Vector3<T>, v2: Vector3<T>) {
    // This works because passing by reference never consumes the target!
    v1.dot(&v2);
}

Required Methods§

Source

fn borrow_vec(&self) -> &Vector3<T>

Returns a reference to the underlying Vector3.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§