cosmocalc/units/
length.rs

1use std::ops::{Add, Sub};
2
3use crate::units::{macros::floating_point_unit_impl, traits::FloatingPointUnit};
4
5floating_point_unit_impl! { Meter }
6floating_point_unit_impl! { Kilometer }
7floating_point_unit_impl! { Mpc }
8
9// Conversions
10pub const KILOMETER_TO_METER: f64 = 1000.;
11pub const MPC_TO_METERS: f64 = 3.086e+22;
12pub const MPC_TO_KILOMETERS: f64 = 3.086e+19;
13
14impl From<Kilometer> for Meter {
15    fn from(km: Kilometer) -> Meter {
16        Meter(1000. * km.0)
17    }
18}
19
20impl From<Mpc> for Meter {
21    fn from(mpc: Mpc) -> Self {
22        Meter(mpc.0 * MPC_TO_METERS)
23    }
24}
25
26impl From<Mpc> for Kilometer {
27    fn from(mpc: Mpc) -> Self {
28        Kilometer(mpc.0 * MPC_TO_KILOMETERS)
29    }
30}
31
32impl From<Meter> for Mpc {
33    fn from(meter: Meter) -> Self {
34        Mpc(meter.0 / MPC_TO_METERS)
35    }
36}