Skip to content

Commit

Permalink
Allow to construct constant Locations (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlafeldt authored Aug 5, 2022
1 parent 7f988b5 commit 03873c8
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,22 @@ mod formula;

pub use formula::Distance;

/// Location defines a point using it's latitude and longitude.
/// Location defines a point using its latitude and longitude.
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Location(f64, f64);

impl Location {
/// Create a new Location with it's degree values of latitude and longitude.
/// Create a new Location with its degree values of latitude and longitude.
pub fn new<T: Into<f64>>(lat: T, lon: T) -> Self {
Location(lat.into(), lon.into())
}

/// Create a new const Location with its degree values of latitude and longitude.
pub const fn new_const(lat: f64, lon: f64) -> Self {
Location(lat, lon)
}

/// Get the latitude.
pub fn latitude(&self) -> f64 {
self.0
Expand Down Expand Up @@ -143,4 +148,12 @@ mod tests {
assert_eq!(l.0, 54.743683);
assert_eq!(l.1, 25.033239);
}

#[test]
fn test_const_location() {
const JAKARTA: Location = Location::new_const(-6.125556, 106.655833);
let jakarta = Location::new(-6.125556, 106.655833);

assert_eq!(JAKARTA, jakarta)
}
}

0 comments on commit 03873c8

Please sign in to comment.