pub trait Angle:
Clone
+ Copy
+ Debug
+ Default
+ Display
+ PartialEq
+ Eq
+ PartialOrd
+ Ord
+ Hash
+ TryFrom<f64, Error = Error>
+ TryFrom<OrderedFloat<f64>, Error = Error>
+ Into<OrderedFloat<f64>>
+ Into<f64> {
const MIN: Self;
const MAX: Self;
Show 18 methods
// Required method
fn new(degrees: i32, minutes: u32, seconds: f32) -> Result<Self, Error>
where Self: Sized;
// Provided methods
fn as_float(&self) -> OrderedFloat<f64> { ... }
fn to_radians(&self) -> f64 { ... }
fn from_radians(radians: f64) -> Result<Self, Error>
where Self: Sized { ... }
fn is_zero(&self) -> bool { ... }
fn is_nonzero_positive(&self) -> bool { ... }
fn is_nonzero_negative(&self) -> bool { ... }
fn degrees(&self) -> i32 { ... }
fn minutes(&self) -> u32 { ... }
fn seconds(&self) -> f32 { ... }
fn abs(self) -> Self
where Self: Sized { ... }
fn modulo_max(self) -> Self
where Self: Sized { ... }
fn checked_abs(self) -> Option<Self>
where Self: Sized { ... }
fn overflowing_abs(self) -> (Self, bool)
where Self: Sized { ... }
fn saturating_abs(self) -> Self
where Self: Sized { ... }
fn strict_abs(self) -> Self
where Self: Sized { ... }
fn unchecked_abs(self) -> Self
where Self: Sized { ... }
fn wrapping_abs(self) -> Self
where Self: Sized { ... }
}Expand description
Shared interface for angular geographic values (Latitude and Longitude).
Angle captures the operations that apply to both kinds of geographic
angle: construction from degrees–minutes–seconds, conversion to and from
decimal degrees and radians, and the various flavours of absolute value
(mirroring those provided by Rust’s primitive integer types).
Implementations are guaranteed to be Copy, totally ordered (via
OrderedFloat), and hashable — making them suitable as keys in
BTreeMap/HashMap collections.
§Examples
use lat_long::{Angle, Latitude};
// Construct from degrees / minutes / seconds.
let lat = Latitude::new(45, 30, 0.0).expect("valid latitude");
// Decompose back into components.
assert_eq!(lat.degrees(), 45);
assert_eq!(lat.minutes(), 30);
// Convert to and from radians.
let radians = lat.to_radians();
let round_trip = Latitude::from_radians(radians).unwrap();
assert_eq!(lat, round_trip);Required Associated Constants§
Required Methods§
Sourcefn new(degrees: i32, minutes: u32, seconds: f32) -> Result<Self, Error>where
Self: Sized,
fn new(degrees: i32, minutes: u32, seconds: f32) -> Result<Self, Error>where
Self: Sized,
Construct a new angle from degrees, minutes, and seconds.
For negative angles, only degrees carries the sign — minutes and
seconds are always non-negative.
§Errors
Returns an Error variant if any component is out of range:
Error::InvalidLatitudeDegrees / Error::InvalidLongitudeDegrees
for an out-of-range degrees value, Error::InvalidMinutes for
minutes ≥ 60, or Error::InvalidSeconds for seconds < 0.0 or
seconds ≥ 60.0.
§Examples
use lat_long::{Angle, Latitude, Longitude};
let lat = Latitude::new(45, 30, 0.0).unwrap();
let lon = Longitude::new(-122, 19, 59.0).unwrap();
assert!(lat.is_nonzero_positive());
assert!(lon.is_nonzero_negative());
// Out-of-range values are rejected.
assert!(Latitude::new(91, 0, 0.0).is_err());
assert!(Longitude::new(0, 60, 0.0).is_err());Provided Methods§
Sourcefn as_float(&self) -> OrderedFloat<f64>
fn as_float(&self) -> OrderedFloat<f64>
Returns the underlying decimal-degree value as an OrderedFloat<f64>.
Useful when interoperating with collections that require total ordering
(e.g. BTreeMap) or with APIs that use OrderedFloat directly.
Sourcefn to_radians(&self) -> f64
fn to_radians(&self) -> f64
Returns the angle converted from decimal degrees to radians.
§Examples
use lat_long::{Angle, Latitude};
let lat = Latitude::new(180 / 2, 0, 0.0).unwrap(); // 90°
// 90° is π/2 radians.
assert!((lat.to_radians() - std::f64::consts::FRAC_PI_2).abs() < 1e-12);Sourcefn from_radians(radians: f64) -> Result<Self, Error>where
Self: Sized,
fn from_radians(radians: f64) -> Result<Self, Error>where
Self: Sized,
Construct an angle from a value in radians.
§Errors
Returns an Error if the resulting decimal-degree value is outside
the legal range for the target type, or is not finite.
§Examples
use lat_long::{Angle, Latitude};
let lat = Latitude::from_radians(std::f64::consts::FRAC_PI_4).unwrap();
assert!((f64::from(lat) - 45.0).abs() < 1e-12);Sourcefn is_nonzero_positive(&self) -> bool
fn is_nonzero_positive(&self) -> bool
Returns true if the angle is positive and non-zero.
Sourcefn is_nonzero_negative(&self) -> bool
fn is_nonzero_negative(&self) -> bool
Returns true if the angle is negative and non-zero.
Sourcefn degrees(&self) -> i32
fn degrees(&self) -> i32
The signed integer degrees component (carries the sign for negative angles).
Sourcefn abs(self) -> Selfwhere
Self: Sized,
fn abs(self) -> Selfwhere
Self: Sized,
Returns the absolute value of this angle.
Because both -MIN and +MAX round-trip safely for Latitude and
Longitude (they are symmetric about zero), this method never panics
for valid input. For symmetry with the primitive-integer *_abs
family, prefer checked_abs,
saturating_abs, etc., when you want to
document overflow intent explicitly.
§Examples
use lat_long::{Angle, Latitude};
let south = Latitude::new(-30, 0, 0.0).unwrap();
assert_eq!(south.abs(), Latitude::new(30, 0, 0.0).unwrap());Sourcefn modulo_max(self) -> Selfwhere
Self: Sized,
fn modulo_max(self) -> Selfwhere
Self: Sized,
Returns this angle taken modulo MAX.
Useful for wrapping a longitude into the canonical (-180°, +180°]
range, or a latitude into (-90°, +90°].
§Examples
use lat_long::{Angle, Longitude};
// 180° is already at the antimeridian; modulo MAX yields 0°.
let lon = Longitude::new(180, 0, 0.0).unwrap();
assert!(lon.modulo_max().is_zero());Sourcefn checked_abs(self) -> Option<Self>where
Self: Sized,
fn checked_abs(self) -> Option<Self>where
Self: Sized,
Checked absolute value. Computes self.abs(), returning None if self == MIN.
Sourcefn overflowing_abs(self) -> (Self, bool)where
Self: Sized,
fn overflowing_abs(self) -> (Self, bool)where
Self: Sized,
Computes the absolute value of self.
Returns a tuple of the absolute version of self along with a boolean
indicating whether an overflow happened. If self is the minimum value
Self::MIN``, then the minimum value will be returned again and true`
will be returned for an overflow happening.
Sourcefn saturating_abs(self) -> Selfwhere
Self: Sized,
fn saturating_abs(self) -> Selfwhere
Self: Sized,
Saturating absolute value. Computes self.abs(), returning MAX
if self == MIN instead of overflowing.
Sourcefn strict_abs(self) -> Selfwhere
Self: Sized,
fn strict_abs(self) -> Selfwhere
Self: Sized,
Strict absolute value. Computes self.abs(), panicking if self == MIN.
Sourcefn unchecked_abs(self) -> Selfwhere
Self: Sized,
fn unchecked_abs(self) -> Selfwhere
Self: Sized,
Unchecked absolute value. Computes self.abs(), assuming overflow cannot occur.
Calling x.unchecked_abs() is semantically equivalent to calling
x.checked_abs().unwrap_unchecked().
If you’re just trying to avoid the panic in debug mode, then do not use
this. Instead, you’re looking for wrapping_abs.
Sourcefn wrapping_abs(self) -> Selfwhere
Self: Sized,
fn wrapping_abs(self) -> Selfwhere
Self: Sized,
Wrapping (modular) absolute value. Computes self.abs(), wrapping around at
the boundary of the type.
The only case where such wrapping can occur is when one takes the absolute
value of the negative minimal value for the type; this is a positive value
that is too large to represent in the type. In such a case, this function
returns MIN itself.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".