aboutsummaryrefslogtreecommitdiff
path: root/rust/space-age/src/lib.rs
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2021-11-06 10:49:12 +0000
committerFederico Igne <git@federicoigne.com>2021-11-06 10:49:12 +0000
commitb6d7705471f0a583f1d115472ddbc8c4f8a420a9 (patch)
treea4bddec8b89f3dc2d8cc77e6e7893e5ba3b9e99b /rust/space-age/src/lib.rs
parentc689e2ad8273b22e1ce8ae4ec58013d3e43010dc (diff)
downloadexercism-b6d7705471f0a583f1d115472ddbc8c4f8a420a9.tar.gz
exercism-b6d7705471f0a583f1d115472ddbc8c4f8a420a9.zip
[rust] Space Age
Diffstat (limited to 'rust/space-age/src/lib.rs')
-rw-r--r--rust/space-age/src/lib.rs37
1 files changed, 37 insertions, 0 deletions
diff --git a/rust/space-age/src/lib.rs b/rust/space-age/src/lib.rs
new file mode 100644
index 0000000..9d24481
--- /dev/null
+++ b/rust/space-age/src/lib.rs
@@ -0,0 +1,37 @@
1#[derive(Debug)]
2pub struct Duration(f64);
3
4impl From<u64> for Duration {
5 fn from(s: u64) -> Self {
6 Self(s as f64)
7 }
8}
9
10pub trait Planet {
11 const EARTH_SECONDS: f64 = 31557600.;
12 const SECONDS_IN_YEAR: f64;
13 fn years_during(d: &Duration) -> f64 { d.0 / Self::SECONDS_IN_YEAR }
14}
15
16
17macro_rules! planets {
18 ( $( $planet:ident -> $ratio:expr ),* ) => {
19 $(
20 pub struct $planet;
21 impl Planet for $planet {
22 const SECONDS_IN_YEAR: f64 = $ratio * Self::EARTH_SECONDS;
23 }
24 )*
25 }
26}
27
28planets!(
29 Mercury -> 0.2408467,
30 Venus -> 0.61519726,
31 Earth -> 1.0,
32 Mars -> 1.8808158,
33 Jupiter -> 11.862615,
34 Saturn -> 29.447498,
35 Uranus -> 84.016846,
36 Neptune -> 164.79132
37);