Skip to main content

Angle

Trait Angle 

Source
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§

Source

const MIN: Self

The minimum legal value of this angle.

For Latitude this is -90°; for Longitude this is -180°.

Source

const MAX: Self

The maximum legal value of this angle.

For Latitude this is +90°; for Longitude this is +180°.

Required Methods§

Source

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§

Source

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.

Source

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);
Source

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);
Source

fn is_zero(&self) -> bool

Returns true if the angle is exactly zero.

Source

fn is_nonzero_positive(&self) -> bool

Returns true if the angle is positive and non-zero.

Source

fn is_nonzero_negative(&self) -> bool

Returns true if the angle is negative and non-zero.

Source

fn degrees(&self) -> i32

The signed integer degrees component (carries the sign for negative angles).

Source

fn minutes(&self) -> u32

The unsigned minutes component (always in 0..60).

Source

fn seconds(&self) -> f32

The unsigned seconds component (always in 0.0..60.0).

Source

fn abs(self) -> Self
where 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());
Source

fn modulo_max(self) -> Self
where 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());
Source

fn checked_abs(self) -> Option<Self>
where Self: Sized,

Checked absolute value. Computes self.abs(), returning None if self == MIN.

Source

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.

Source

fn saturating_abs(self) -> Self
where Self: Sized,

Saturating absolute value. Computes self.abs(), returning MAX if self == MIN instead of overflowing.

Source

fn strict_abs(self) -> Self
where Self: Sized,

Strict absolute value. Computes self.abs(), panicking if self == MIN.

Source

fn unchecked_abs(self) -> Self
where 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.

Source

fn wrapping_abs(self) -> Self
where 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".

Implementors§

Source§

impl Angle for Latitude

Source§

const MIN: Self

Source§

const MAX: Self

Source§

impl Angle for Longitude

Source§

const MIN: Self

Source§

const MAX: Self