Guest User

Untitled

a guest
Oct 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. const EARTH_YEAR_SECS: f64 = 31557600.0;
  2.  
  3. #[derive(Debug)]
  4. pub struct Duration {
  5. secs: f64,
  6. }
  7.  
  8. impl From<u64> for Duration {
  9. fn from(s: u64) -> Self {
  10. Duration { secs: s as f64 }
  11. }
  12. }
  13.  
  14. impl From<f64> for Duration {
  15. fn from(s: f64) -> Self {
  16. Duration { secs: s }
  17. }
  18. }
  19.  
  20. pub trait Planet {
  21. fn orbit_duration() -> Duration;
  22. fn years_during(d: &Duration) -> f64 {
  23. d.secs / Self::orbit_duration().secs
  24. }
  25. }
  26.  
  27. pub struct Mercury;
  28. pub struct Venus;
  29. pub struct Earth;
  30. pub struct Mars;
  31. pub struct Jupiter;
  32. pub struct Saturn;
  33. pub struct Uranus;
  34. pub struct Neptune;
  35.  
  36. impl Planet for Mercury {
  37. fn orbit_duration() -> Duration {
  38. Duration::from(0.2408467 * EARTH_YEAR_SECS)
  39. }
  40. }
  41.  
  42. impl Planet for Venus {
  43. fn orbit_duration() -> Duration {
  44. Duration::from(0.61519726 * EARTH_YEAR_SECS)
  45. }
  46. }
  47.  
  48. impl Planet for Earth {
  49. fn orbit_duration() -> Duration {
  50. Duration::from(EARTH_YEAR_SECS)
  51. }
  52. }
  53.  
  54. impl Planet for Mars {
  55. fn orbit_duration() -> Duration {
  56. Duration::from(1.8808158 * EARTH_YEAR_SECS)
  57. }
  58. }
  59.  
  60. impl Planet for Jupiter {
  61. fn orbit_duration() -> Duration {
  62. Duration::from(11.862615 * EARTH_YEAR_SECS)
  63. }
  64. }
  65.  
  66. impl Planet for Saturn {
  67. fn orbit_duration() -> Duration {
  68. Duration::from(29.447498 * EARTH_YEAR_SECS)
  69. }
  70. }
  71.  
  72. impl Planet for Uranus {
  73. fn orbit_duration() -> Duration {
  74. Duration::from(84.016846 * EARTH_YEAR_SECS)
  75. }
  76. }
  77.  
  78. impl Planet for Neptune {
  79. fn orbit_duration() -> Duration {
  80. Duration::from(164.79132 * EARTH_YEAR_SECS)
  81. }
  82. }
Add Comment
Please, Sign In to add comment