Skip to content

Commit c208aa5

Browse files
committed
1 parent 3d9c56c commit c208aa5

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/geometry/point/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,28 @@ use generic_array::{ArrayLength, GenericArray};
44
pub struct Point<T, N: ArrayLength<T>> {
55
pub components: GenericArray<T, N>,
66
}
7+
8+
impl<T, N: ArrayLength<T>, I, X> From<I> for Point<T, N>
9+
where I: IntoIterator<Item=T, IntoIter=X>,
10+
X: ExactSizeIterator<Item=T>,
11+
{
12+
fn from(iter: I) -> Self {
13+
Self { components: GenericArray::from_exact_iter(iter).unwrap() }
14+
}
15+
}
16+
17+
// NotEq is a marker trait that's auto-implemented on all 2-tuples...
18+
pub auto trait NotEq { }
19+
20+
// ...except those with two values of the same type...
21+
impl<T> !NotEq for (T, T) { }
22+
23+
impl<T, U, N: ArrayLength<T> + ArrayLength<U>> From<Point<U, N>> for Point<T, N>
24+
where (T, U): NotEq, // ...which lets us add a trait bound that avoids a conflict
25+
T: From<U>, // with the auto-implemented From<T> trait.
26+
U: Copy,
27+
{
28+
fn from(p: Point<U, N>) -> Self {
29+
p.components.iter().map(|&a| a.into()).into()
30+
}
31+
}

src/geometry/point2/test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,12 @@ mod conversions {
5050
assert_eq!(subject.x(), 1);
5151
assert_eq!(subject.y(), 2);
5252
}
53+
54+
#[test]
55+
fn it_can_build_a_point2_from_a_point2_with_different_component_types() {
56+
let subject: Point2f = Point2i::new(1, 2).into();
57+
58+
assert_eq!(subject.x(), 1.0);
59+
assert_eq!(subject.y(), 2.0);
60+
}
5361
}

src/geometry/point3/test.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ mod default {
4141
assert_eq!(subject.z(), 0);
4242
}
4343
}
44+
45+
mod conversions {
46+
use super::*;
47+
48+
#[test]
49+
fn it_can_build_a_point3_from_a_point3_with_different_component_types() {
50+
let subject: Point3f = Point3i::new(1, 2, 3).into();
51+
52+
assert_eq!(subject.x(), 1.0);
53+
assert_eq!(subject.y(), 2.0);
54+
assert_eq!(subject.z(), 3.0);
55+
}
56+
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(optin_builtin_traits)]
2+
13
mod geometry;
24

35
fn main() {

0 commit comments

Comments
 (0)