1use std::time::Duration;
4use serde::{Deserialize, Serialize};
5
6pub const SPEED_OF_LIGHT_MPS: f64 = 299_792_458.0;
8
9#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
11pub struct Distance {
12 meters: f64,
13}
14
15impl Distance {
16 pub fn meters(m: f64) -> Self {
18 Self { meters: m }
19 }
20
21 pub fn kilometers(km: f64) -> Self {
23 Self { meters: km * 1000.0 }
24 }
25
26 pub fn miles(miles: f64) -> Self {
28 Self {
29 meters: miles * 1609.344,
30 }
31 }
32
33 pub fn light_seconds(ls: f64) -> Self {
35 Self {
36 meters: ls * SPEED_OF_LIGHT_MPS,
37 }
38 }
39
40 pub fn as_meters(&self) -> f64 {
42 self.meters
43 }
44
45 pub fn as_kilometers(&self) -> f64 {
47 self.meters / 1000.0
48 }
49
50 pub fn light_travel_time(&self) -> Duration {
52 let seconds = self.meters / SPEED_OF_LIGHT_MPS;
53 Duration::from_secs_f64(seconds)
54 }
55
56 pub fn light_travel_time_ms(&self) -> f64 {
58 (self.meters / SPEED_OF_LIGHT_MPS) * 1000.0
59 }
60
61 pub fn tokyo_to_nyc() -> Self {
63 Self::kilometers(10_900.0)
64 }
65
66 pub fn earth_to_moon() -> Self {
67 Self::kilometers(384_400.0)
68 }
69
70 pub fn earth_to_mars_min() -> Self {
71 Self::kilometers(54_600_000.0)
72 }
73
74 pub fn earth_to_mars_max() -> Self {
75 Self::kilometers(401_000_000.0)
76 }
77
78 pub fn one_au() -> Self {
79 Self::kilometers(149_597_870.7)
80 }
81}
82
83pub struct SpeedOfLight;
85
86impl SpeedOfLight {
87 pub fn meters_per_second() -> f64 {
89 SPEED_OF_LIGHT_MPS
90 }
91
92 pub fn kilometers_per_second() -> f64 {
94 SPEED_OF_LIGHT_MPS / 1000.0
95 }
96
97 pub fn time_to_travel(distance: Distance) -> Duration {
99 distance.light_travel_time()
100 }
101
102 pub fn distance_in_time(duration: Duration) -> Distance {
104 Distance::meters(SPEED_OF_LIGHT_MPS * duration.as_secs_f64())
105 }
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct TemporalAdvantage {
111 pub distance: Distance,
112 pub light_time: Duration,
113 pub prediction_time: Duration,
114 pub advantage: Duration,
115 pub effective_velocity_ratio: f64,
116}
117
118impl TemporalAdvantage {
119 pub fn calculate(distance: Distance, prediction_time: Duration) -> Self {
121 let light_time = distance.light_travel_time();
122
123 let advantage = if light_time > prediction_time {
124 light_time - prediction_time
125 } else {
126 Duration::ZERO
127 };
128
129 let effective_velocity_ratio = if prediction_time.as_secs_f64() > 0.0 {
130 light_time.as_secs_f64() / prediction_time.as_secs_f64()
131 } else {
132 f64::INFINITY
133 };
134
135 Self {
136 distance,
137 light_time,
138 prediction_time,
139 advantage,
140 effective_velocity_ratio,
141 }
142 }
143
144 pub fn is_ftl(&self) -> bool {
146 self.effective_velocity_ratio > 1.0
147 }
148
149 pub fn advantage_ms(&self) -> f64 {
151 self.advantage.as_secs_f64() * 1000.0
152 }
153
154 pub fn effective_velocity(&self) -> f64 {
156 if self.prediction_time.as_secs_f64() > 0.0 {
157 self.distance.as_meters() / self.prediction_time.as_secs_f64()
158 } else {
159 f64::INFINITY
160 }
161 }
162
163 pub fn describe(&self) -> String {
165 if self.is_ftl() {
166 format!(
167 "FTL achieved! Distance: {:.0}km, Light time: {:.1}ms, Prediction: {:.3}ms, Advantage: {:.1}ms ({}x light speed)",
168 self.distance.as_kilometers(),
169 self.light_time.as_secs_f64() * 1000.0,
170 self.prediction_time.as_secs_f64() * 1000.0,
171 self.advantage_ms(),
172 self.effective_velocity_ratio as u64
173 )
174 } else {
175 format!(
176 "Sub-light. Distance: {:.0}km, Light time: {:.1}ms, Prediction: {:.1}ms",
177 self.distance.as_kilometers(),
178 self.light_time.as_secs_f64() * 1000.0,
179 self.prediction_time.as_secs_f64() * 1000.0
180 )
181 }
182 }
183}
184
185pub struct RelativisticEffects;
187
188impl RelativisticEffects {
189 pub fn lorentz_factor(velocity: f64) -> f64 {
191 let beta = velocity / SPEED_OF_LIGHT_MPS;
192 if beta >= 1.0 {
193 f64::INFINITY
194 } else {
195 1.0 / (1.0 - beta * beta).sqrt()
196 }
197 }
198
199 pub fn time_dilation(velocity: f64, time: Duration) -> Duration {
201 let gamma = Self::lorentz_factor(velocity);
202 Duration::from_secs_f64(time.as_secs_f64() * gamma)
203 }
204
205 pub fn violates_causality(velocity: f64) -> bool {
207 velocity >= SPEED_OF_LIGHT_MPS
208 }
209
210 pub fn validate_information_velocity(
212 distance: Distance,
213 computation_time: Duration,
214 ) -> (bool, String) {
215 let effective_velocity = distance.as_meters() / computation_time.as_secs_f64();
216
217 if effective_velocity <= SPEED_OF_LIGHT_MPS {
218 (true, "Information velocity is sub-light".to_string())
219 } else {
220 (
222 true,
223 format!(
224 "Information appears to travel at {:.2}x light speed, but this is predictive computation, not physical signal transmission. No causality violation.",
225 effective_velocity / SPEED_OF_LIGHT_MPS
226 ),
227 )
228 }
229 }
230}
231
232#[cfg(test)]
233mod tests {
234 use super::*;
235
236 #[test]
237 fn test_distance_conversions() {
238 let d = Distance::kilometers(1.0);
239 assert_eq!(d.as_meters(), 1000.0);
240 }
241
242 #[test]
243 fn test_light_travel_time() {
244 let d = Distance::kilometers(300_000.0); let time = d.light_travel_time();
246 assert!((time.as_secs_f64() - 1.0).abs() < 0.01);
247 }
248
249 #[test]
250 fn test_temporal_advantage() {
251 let distance = Distance::tokyo_to_nyc();
252 let prediction_time = Duration::from_micros(100);
253 let advantage = TemporalAdvantage::calculate(distance, prediction_time);
254
255 assert!(advantage.is_ftl());
256 assert!(advantage.effective_velocity_ratio > 100.0);
257 }
258
259 #[test]
260 fn test_relativistic_validation() {
261 let distance = Distance::kilometers(1000.0);
262 let compute_time = Duration::from_micros(1);
263 let (valid, _msg) = RelativisticEffects::validate_information_velocity(distance, compute_time);
264 assert!(valid); }
266}