diff --git a/src/lib.rs b/src/lib.rs index 64aab1b..24c73f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,30 @@ impl From<&Point> for robust::Coord { } } +impl From<(f64, f64)> for Point { + fn from((x, y): (f64, f64)) -> Self { + Point { x, y } + } +} + +impl From<[f64; 2]> for Point { + fn from([x, y]: [f64; 2]) -> Self { + Point { x, y } + } +} + +impl From for (f64, f64) { + fn from(pt: Point) -> Self { + (pt.x, pt.y) + } +} + +impl From for [f64; 2] { + fn from(pt: Point) -> Self { + [pt.x, pt.y] + } +} + impl Point { fn dist2(&self, p: &Self) -> f64 { let dx = self.x - p.x; diff --git a/tests/tests.rs b/tests/tests.rs index dd711da..e3a63d6 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -177,6 +177,18 @@ fn invalid_nan_sequence() { triangulate(&points); } +#[test] +/// The test demonstrates and validates our tuple and array round tripping of `Point` +fn tuple_array_conv() { + // Tuple/Array --> Point + assert_eq!(Into::::into((1., 2.)), Point { x: 1., y: 2. }); + assert_eq!(Into::::into([1., 2.]), Point { x: 1., y: 2. }); + + // Point --> Tuple/Array + assert_eq!(Into::<(f64, f64)>::into(Point { x: 1., y: 2. }), (1., 2.)); + assert_eq!(Into::<[f64; 2]>::into(Point { x: 1., y: 2. }), [1., 2.]); +} + fn scale_points(points: &[Point], scale: f64) -> Vec { let scaled: Vec = points .iter()