tuple::new can take any type of number
This commit is contained in:
1
features/Cargo.lock
generated
1
features/Cargo.lock
generated
@@ -22,6 +22,7 @@ name = "features"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"approx",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -7,3 +7,4 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
approx = "0.5"
|
||||
num-traits = "0.2"
|
||||
@@ -1,5 +1,6 @@
|
||||
#[macro_use]
|
||||
extern crate approx;
|
||||
extern crate num_traits;
|
||||
|
||||
pub mod structs;
|
||||
pub mod color;
|
||||
|
||||
@@ -10,12 +10,12 @@ pub struct Tuple {
|
||||
}
|
||||
|
||||
impl Tuple {
|
||||
pub fn new(x: f32, y: f32, z: f32, w: f32) -> Tuple {
|
||||
pub fn new<T: num_traits::NumCast>(x: T, y: T, z: T, w: T) -> Tuple {
|
||||
Tuple {
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
w,
|
||||
x: num_traits::cast(x).unwrap(),
|
||||
y: num_traits::cast(y).unwrap(),
|
||||
z: num_traits::cast(z).unwrap(),
|
||||
w: num_traits::cast(w).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -424,4 +424,12 @@ mod tests {
|
||||
let b_cross_a = Tuple::vector(1.0, -2.0, 1.0);
|
||||
assert_eq!(b_cross_a, b.cross(&a));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn works_with_i32() {
|
||||
let a = Tuple::new(1, 2, 3);
|
||||
assert_eq!(1.0, a.x());
|
||||
assert_eq!(2.0, a.y());
|
||||
assert_eq!(3.0, a.z());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user