From 2f7ead093b1eba883c8fefa972a8c0a857e19bcd Mon Sep 17 00:00:00 2001 From: Ivan Ladelshchikov Date: Tue, 20 Oct 2020 19:47:57 +0500 Subject: [PATCH] fix warnings --- src/coord/lat.rs | 8 ++++---- src/coord/lon.rs | 6 +++--- src/utils.rs | 24 ++++++++++++------------ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/coord/lat.rs b/src/coord/lat.rs index a1c448a..c068622 100644 --- a/src/coord/lat.rs +++ b/src/coord/lat.rs @@ -104,9 +104,9 @@ impl AngleAndDirection for Latitude { } } -impl ParsedCoordinate for Latitude +impl ParsedCoordinate for Latitude where - A: FromStr::ParseErr>, + A: Angle + FromStr::ParseErr>, A::ParseErr: From, { fn with_angle_only(angle: A) -> Option { @@ -147,7 +147,7 @@ impl TryFrom for Latitude { type Error = A::NumErr; fn try_from(value: f64) -> Result { - let (value, is_north) = value.unsigned_abs(); + let (value, is_north) = value.abs_and_sign(); let angle = value.try_into()?; Self::with_angle_and_direction(angle, is_north.into()) } @@ -162,7 +162,7 @@ where fn try_from(value: (i16, u8, u8, u16)) -> Result { let (deg, min, sec, milli) = value; - let (deg, sign) = deg.unsigned_abs(); + let (deg, sign) = deg.abs_and_sign(); let angle = (deg, min, sec, milli).try_into()?; Self::with_angle_and_direction(angle, sign.into()) diff --git a/src/coord/lon.rs b/src/coord/lon.rs index 65bcff7..e4f0deb 100644 --- a/src/coord/lon.rs +++ b/src/coord/lon.rs @@ -169,7 +169,7 @@ impl AngleAndDirection for Longitude { } } -impl ParsedCoordinate for Longitude +impl ParsedCoordinate for Longitude where A: Angle + FromStr::ParseErr>, A::ParseErr: From, @@ -197,7 +197,7 @@ impl TryFrom for Longitude { type Error = A::NumErr; fn try_from(value: f64) -> Result { - let (value, is_east) = value.unsigned_abs(); + let (value, is_east) = value.abs_and_sign(); let angle = value.try_into()?; Self::with_angle_and_direction(angle, is_east.into()) } @@ -212,7 +212,7 @@ where fn try_from(value: (i16, u8, u8, u16)) -> Result { let (deg, min, sec, milli) = value; - let (deg, is_east) = deg.unsigned_abs(); + let (deg, is_east) = deg.abs_and_sign(); let angle = (deg, min, sec, milli).try_into()?; Self::with_angle_and_direction(angle, is_east.into()) diff --git a/src/utils.rs b/src/utils.rs index eb2776a..341cb90 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -21,8 +21,8 @@ pub trait ToUnsigned: Default + Copy + PartialOrd + Neg { /// represent the source (signed) type as target (unsigned) type fn as_type(self) -> U; - /// Converts to unsigned absolute value - fn unsigned_abs(self) -> (U, bool) { + /// Converts to unsigned absolute value, also preserving the 'is negative' flag + fn abs_and_sign(self) -> (U, bool) { if self >= Self::default() { (self.as_type(), true) } else { @@ -31,7 +31,7 @@ pub trait ToUnsigned: Default + Copy + PartialOrd + Neg { } } -macro_rules! impl_unsigned_abs { +macro_rules! impl_abs_and_sign { ($from: tt -> $to: ty) => { impl ToUnsigned<$to> for $from { fn as_type(self) -> $to { @@ -49,11 +49,11 @@ macro_rules! impl_unsigned_abs { }; } -impl_unsigned_abs!(i8 -> u8); -impl_unsigned_abs!(i16 -> u16); -impl_unsigned_abs!(i32 -> u32); -impl_unsigned_abs!(i64 -> u64); -impl_unsigned_abs!(f64); +impl_abs_and_sign!(i8 -> u8); +impl_abs_and_sign!(i16 -> u16); +impl_abs_and_sign!(i32 -> u32); +impl_abs_and_sign!(i64 -> u64); +impl_abs_and_sign!(f64); /// Strip the given character from the beginning or the end pub trait StripChar { @@ -160,11 +160,11 @@ mod tests { #[test] fn unsigned() { - assert_eq!(7_i8.unsigned_abs(), (7_u8, true)); - assert_eq!((-7_i8).unsigned_abs(), (7_u8, false)); + assert_eq!(7_i8.abs_and_sign(), (7_u8, true)); + assert_eq!((-7_i8).abs_and_sign(), (7_u8, false)); - assert_eq!(1283_i16.unsigned_abs(), (1283_u16, true)); - assert_eq!((-25_038_i16).unsigned_abs(), (25_038_u16, false)); + assert_eq!(1283_i16.abs_and_sign(), (1283_u16, true)); + assert_eq!((-25_038_i16).abs_and_sign(), (25_038_u16, false)); } //noinspection SpellCheckingInspection