From d30af48faba77a87ecfc29b80412cec0646a0346 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sun, 3 Dec 2023 18:46:51 +0100 Subject: [PATCH 1/4] yote --- crates/bevy_math/src/primitives/dim2.rs | 12 +++++++----- crates/bevy_math/src/primitives/dim3.rs | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 35e5363dc6acd..a5b4afaf93bc3 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -5,13 +5,15 @@ use crate::Vec2; #[derive(Clone, Copy, Debug)] pub struct Direction2d(Vec2); -impl From for Direction2d { - fn from(value: Vec2) -> Self { - Self(value.normalize()) +impl Direction2d { + /// Create a direction from a finite, nonzero [`Vec2`]. + /// + /// If the input is zero (or very close to zero), or non-finite, + /// the result of this operation will be `None`. + pub fn new(value: Vec2) -> Option { + value.try_normalize().map(|value| Self(value)) } -} -impl Direction2d { /// Create a direction from a [`Vec2`] that is already normalized pub fn from_normalized(value: Vec2) -> Self { debug_assert!(value.is_normalized()); diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 891f51558fd3b..f9767b3598d7f 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -5,13 +5,15 @@ use crate::Vec3; #[derive(Clone, Copy, Debug)] pub struct Direction3d(Vec3); -impl From for Direction3d { - fn from(value: Vec3) -> Self { - Self(value.normalize()) +impl Direction3d { + /// Create a direction from a finite, nonzero [`Vec3`]. + /// + /// If the input is zero (or very close to zero), or non-finite, + /// the result of this operation will be `None`. + pub fn new(value: Vec3) -> Option { + value.try_normalize().map(|value| Self(value)) } -} -impl Direction3d { /// Create a direction from a [`Vec3`] that is already normalized pub fn from_normalized(value: Vec3) -> Self { debug_assert!(value.is_normalized()); From 056456e973efb9ec4858563e45b2666d34007289 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sun, 3 Dec 2023 20:08:54 +0100 Subject: [PATCH 2/4] fmt --- crates/bevy_math/src/primitives/dim2.rs | 2 +- crates/bevy_math/src/primitives/dim3.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index a5b4afaf93bc3..5bda1009c3707 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -11,7 +11,7 @@ impl Direction2d { /// If the input is zero (or very close to zero), or non-finite, /// the result of this operation will be `None`. pub fn new(value: Vec2) -> Option { - value.try_normalize().map(|value| Self(value)) + value.try_normalize().map(|value| Self(value)) } /// Create a direction from a [`Vec2`] that is already normalized diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index f9767b3598d7f..ef8d0b906739b 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -11,7 +11,7 @@ impl Direction3d { /// If the input is zero (or very close to zero), or non-finite, /// the result of this operation will be `None`. pub fn new(value: Vec3) -> Option { - value.try_normalize().map(|value| Self(value)) + value.try_normalize().map(|value| Self(value)) } /// Create a direction from a [`Vec3`] that is already normalized From 54127235d7bc1351e3ef410436706c724e1a0b77 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sun, 3 Dec 2023 20:14:38 +0100 Subject: [PATCH 3/4] clippy --- crates/bevy_math/src/primitives/dim2.rs | 2 +- crates/bevy_math/src/primitives/dim3.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 5bda1009c3707..1a9f5b23e07c8 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -11,7 +11,7 @@ impl Direction2d { /// If the input is zero (or very close to zero), or non-finite, /// the result of this operation will be `None`. pub fn new(value: Vec2) -> Option { - value.try_normalize().map(|value| Self(value)) + value.try_normalize().map(Self) } /// Create a direction from a [`Vec2`] that is already normalized diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index ef8d0b906739b..3ae964f4d745c 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -11,7 +11,7 @@ impl Direction3d { /// If the input is zero (or very close to zero), or non-finite, /// the result of this operation will be `None`. pub fn new(value: Vec3) -> Option { - value.try_normalize().map(|value| Self(value)) + value.try_normalize().map(Self) } /// Create a direction from a [`Vec3`] that is already normalized From 5f0a993b220823d9113d948bf92969033b3cb504 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sun, 3 Dec 2023 21:49:46 +0100 Subject: [PATCH 4/4] Reword the docs --- crates/bevy_math/src/primitives/dim2.rs | 3 +-- crates/bevy_math/src/primitives/dim3.rs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 1a9f5b23e07c8..a60cd3d61956f 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -8,8 +8,7 @@ pub struct Direction2d(Vec2); impl Direction2d { /// Create a direction from a finite, nonzero [`Vec2`]. /// - /// If the input is zero (or very close to zero), or non-finite, - /// the result of this operation will be `None`. + /// Returns `None` if the input is zero (or very close to zero), or non-finite. pub fn new(value: Vec2) -> Option { value.try_normalize().map(Self) } diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 3ae964f4d745c..60594c8824dfa 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -8,8 +8,7 @@ pub struct Direction3d(Vec3); impl Direction3d { /// Create a direction from a finite, nonzero [`Vec3`]. /// - /// If the input is zero (or very close to zero), or non-finite, - /// the result of this operation will be `None`. + /// Returns `None` if the input is zero (or very close to zero), or non-finite. pub fn new(value: Vec3) -> Option { value.try_normalize().map(Self) }