From a5af28e206424f55b5b6dd017e4e0a30847de6a3 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 12 Apr 2020 12:10:49 +0200 Subject: [PATCH] Replace all `Into` impls with `From` impls As per `Into`'s documentation, `Into` should not be implemented, if possible, and `From` should be implemented instead. This is more flexible, as there is a blanket impl that implements `Into` for all `From`. --- src/macros.rs | 6 +++--- src/point.rs | 24 ++++++++++++------------ src/size.rs | 42 +++++++++++++++++++++--------------------- src/transform2d.rs | 10 +++++----- src/transform3d.rs | 12 ++++++------ src/translation.rs | 24 ++++++++++++------------ src/vector.rs | 28 ++++++++++++++-------------- 7 files changed, 73 insertions(+), 73 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 9cc392eb..69279402 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -19,10 +19,10 @@ macro_rules! mint_vec { } } #[cfg(feature = "mint")] - impl Into> for $name { - fn into(self) -> mint::$std_name { + impl From<$name> for mint::$std_name { + fn from(v: $name) -> Self { mint::$std_name { - $( $field: self.$field, )* + $( $field: v.$field, )* } } } diff --git a/src/point.rs b/src/point.rs index a4a31355..b3fcd8e9 100644 --- a/src/point.rs +++ b/src/point.rs @@ -753,9 +753,9 @@ impl Point2D { } } -impl Into<[T; 2]> for Point2D { - fn into(self) -> [T; 2] { - [self.x, self.y] +impl From> for [T; 2] { + fn from(p: Point2D) -> Self { + [p.x, p.y] } } @@ -765,9 +765,9 @@ impl From<[T; 2]> for Point2D { } } -impl Into<(T, T)> for Point2D { - fn into(self) -> (T, T) { - (self.x, self.y) +impl From> for (T, T) { + fn from(p: Point2D) -> Self { + (p.x, p.y) } } @@ -1567,9 +1567,9 @@ impl Point3D { } } -impl Into<[T; 3]> for Point3D { - fn into(self) -> [T; 3] { - [self.x, self.y, self.z] +impl From> for [T; 3] { + fn from(p: Point3D) -> Self { + [p.x, p.y, p.z] } } @@ -1579,9 +1579,9 @@ impl From<[T; 3]> for Point3D { } } -impl Into<(T, T, T)> for Point3D { - fn into(self) -> (T, T, T) { - (self.x, self.y, self.z) +impl From> for (T, T, T) { + fn from(p: Point3D) -> Self { + (p.x, p.y, p.z) } } diff --git a/src/size.rs b/src/size.rs index f634c1cf..62ca89be 100644 --- a/src/size.rs +++ b/src/size.rs @@ -678,12 +678,12 @@ impl From> for Size2D { } } #[cfg(feature = "mint")] -impl Into> for Size2D { +impl From> for mint::Vector2 { #[inline] - fn into(self) -> mint::Vector2 { + fn from(s: Size2D) -> Self { mint::Vector2 { - x: self.width, - y: self.height, + x: s.width, + y: s.height, } } } @@ -695,10 +695,10 @@ impl From> for Size2D { } } -impl Into<[T; 2]> for Size2D { +impl From> for [T; 2] { #[inline] - fn into(self) -> [T; 2] { - [self.width, self.height] + fn from(s: Size2D) -> Self { + [s.width, s.height] } } @@ -709,10 +709,10 @@ impl From<[T; 2]> for Size2D { } } -impl Into<(T, T)> for Size2D { +impl From> for (T, T) { #[inline] - fn into(self) -> (T, T) { - (self.width, self.height) + fn from(s: Size2D) -> Self { + (s.width, s.height) } } @@ -1605,13 +1605,13 @@ impl From> for Size3D { } } #[cfg(feature = "mint")] -impl Into> for Size3D { +impl From> for mint::Vector3 { #[inline] - fn into(self) -> mint::Vector3 { + fn from(s: Size3D) -> Self { mint::Vector3 { - x: self.width, - y: self.height, - z: self.depth, + x: s.width, + y: s.height, + z: s.depth, } } } @@ -1623,10 +1623,10 @@ impl From> for Size3D { } } -impl Into<[T; 3]> for Size3D { +impl From> for [T; 3] { #[inline] - fn into(self) -> [T; 3] { - [self.width, self.height, self.depth] + fn from(s: Size3D) -> Self { + [s.width, s.height, s.depth] } } @@ -1637,10 +1637,10 @@ impl From<[T; 3]> for Size3D { } } -impl Into<(T, T, T)> for Size3D { +impl From> for (T, T, T) { #[inline] - fn into(self) -> (T, T, T) { - (self.width, self.height, self.depth) + fn from(s: Size3D) -> Self { + (s.width, s.height, s.depth) } } diff --git a/src/transform2d.rs b/src/transform2d.rs index 85eb426b..61d97ca7 100644 --- a/src/transform2d.rs +++ b/src/transform2d.rs @@ -659,12 +659,12 @@ impl From> for Transform2D { } } #[cfg(feature = "mint")] -impl Into> for Transform2D { - fn into(self) -> mint::RowMatrix3x2 { +impl From> for mint::RowMatrix3x2 { + fn from(t: Transform2D) -> Self { mint::RowMatrix3x2 { - x: mint::Vector2 { x: self.m11, y: self.m12 }, - y: mint::Vector2 { x: self.m21, y: self.m22 }, - z: mint::Vector2 { x: self.m31, y: self.m32 }, + x: mint::Vector2 { x: t.m11, y: t.m12 }, + y: mint::Vector2 { x: t.m21, y: t.m22 }, + z: mint::Vector2 { x: t.m31, y: t.m32 }, } } } diff --git a/src/transform3d.rs b/src/transform3d.rs index 2ea4730a..53f8adaf 100644 --- a/src/transform3d.rs +++ b/src/transform3d.rs @@ -1141,13 +1141,13 @@ impl From> for Transform3D { } } #[cfg(feature = "mint")] -impl Into> for Transform3D { - fn into(self) -> mint::RowMatrix4 { +impl From> for mint::RowMatrix4 { + fn from(t: Transform3D) -> Self { mint::RowMatrix4 { - x: mint::Vector4 { x: self.m11, y: self.m12, z: self.m13, w: self.m14 }, - y: mint::Vector4 { x: self.m21, y: self.m22, z: self.m23, w: self.m24 }, - z: mint::Vector4 { x: self.m31, y: self.m32, z: self.m33, w: self.m34 }, - w: mint::Vector4 { x: self.m41, y: self.m42, z: self.m43, w: self.m44 }, + x: mint::Vector4 { x: t.m11, y: t.m12, z: t.m13, w: t.m14 }, + y: mint::Vector4 { x: t.m21, y: t.m22, z: t.m23, w: t.m24 }, + z: mint::Vector4 { x: t.m31, y: t.m32, z: t.m33, w: t.m34 }, + w: mint::Vector4 { x: t.m41, y: t.m42, z: t.m43, w: t.m44 }, } } } diff --git a/src/translation.rs b/src/translation.rs index 45126e8d..75e3fd21 100644 --- a/src/translation.rs +++ b/src/translation.rs @@ -296,18 +296,18 @@ impl From> for Translation2D { } } -impl Into> for Translation2D { - fn into(self) -> Vector2D { - vec2(self.x, self.y) +impl From> for Vector2D { + fn from(t: Translation2D) -> Self { + vec2(t.x, t.y) } } -impl Into> for Translation2D +impl From> for Transform2D where T: Zero + One, { - fn into(self) -> Transform2D { - Transform2D::translation(self.x, self.y) + fn from(t: Translation2D) -> Self { + Transform2D::translation(t.x, t.y) } } @@ -624,18 +624,18 @@ impl From> for Translation3D { } } -impl Into> for Translation3D { - fn into(self) -> Vector3D { - vec3(self.x, self.y, self.z) +impl From> for Vector3D { + fn from(t: Translation3D) -> Self { + vec3(t.x, t.y, t.z) } } -impl Into> for Translation3D +impl From> for Transform3D where T: Zero + One, { - fn into(self) -> Transform3D { - Transform3D::translation(self.x, self.y, self.z) + fn from(t: Translation3D) -> Self { + Transform3D::translation(t.x, t.y, t.z) } } diff --git a/src/vector.rs b/src/vector.rs index 1a23bbed..2a06d7e9 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -876,9 +876,9 @@ impl, U> ApproxEq> for Vector2D { } } -impl Into<[T; 2]> for Vector2D { - fn into(self) -> [T; 2] { - [self.x, self.y] +impl From> for [T; 2] { + fn from(v: Vector2D) -> Self { + [v.x, v.y] } } @@ -888,9 +888,9 @@ impl From<[T; 2]> for Vector2D { } } -impl Into<(T, T)> for Vector2D { - fn into(self) -> (T, T) { - (self.x, self.y) +impl From> for (T, T) { + fn from(v: Vector2D) -> Self { + (v.x, v.y) } } @@ -901,8 +901,8 @@ impl From<(T, T)> for Vector2D { } impl From> for Vector2D { - fn from(size: Size2D) -> Self { - vec2(size.width, size.height) + fn from(s: Size2D) -> Self { + vec2(s.width, s.height) } } @@ -1774,9 +1774,9 @@ impl, U> ApproxEq> for Vector3D { } } -impl Into<[T; 3]> for Vector3D { - fn into(self) -> [T; 3] { - [self.x, self.y, self.z] +impl From> for [T; 3] { + fn from(v: Vector3D) -> Self { + [v.x, v.y, v.z] } } @@ -1786,9 +1786,9 @@ impl From<[T; 3]> for Vector3D { } } -impl Into<(T, T, T)> for Vector3D { - fn into(self) -> (T, T, T) { - (self.x, self.y, self.z) +impl From> for (T, T, T) { + fn from(v: Vector3D) -> Self { + (v.x, v.y, v.z) } }