gearbox 3.0.0

Excessive tooling for Rust, boosting productivity and operations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::time::constants::{MILLIS_PER_SEC, NANOS_PER_SEC};
use crate::time::NANOS_PER_MILLI;
use core::ops::{Add, Sub};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Duration {
    Negative(u64, u32),
    Positive(u64, u32),
    Zero,
}

impl Duration {
    pub fn zero() -> Self {
        Self::Zero
    }
    pub fn from_secs(sec: i64) -> Self {
        if sec.is_negative() {
            Self::Negative(sec.abs() as u64, 0)
        } else {
            Self::Positive(sec.abs() as u64, 0)
        }
    }

    pub fn from_nanos(nanos: i128) -> Self {
        let new_sec = (nanos / NANOS_PER_SEC as i128) as u64;
        let new_nanos = (NANOS_PER_SEC as i128 % nanos) as u32;
        if nanos.is_negative() {
            Self::Negative(new_sec, new_nanos)
        } else {
            Self::Positive(new_sec, new_nanos)
        }
    }

    pub fn from_secs_nanos(sec: &i64, nanos: &u32) -> Self {
        if sec.is_negative() {
            Self::Negative(sec.abs() as u64, *nanos)
        } else {
            Self::Positive(*sec as u64, *nanos)
        }
    }

    pub fn as_secs(&self) -> i64 {
        match self {
            Self::Negative(sec, _) => -(sec.clone() as i64),
            Self::Positive(sec, _) => sec.clone() as i64,
            Self::Zero => 0,
        }
    }

    pub fn as_millis(&self) -> i64 {
        match self {
            Self::Negative(sec, nanos) => {
                -((sec.clone() as i64 * MILLIS_PER_SEC as i64)
                    + (nanos.clone() as i64 / NANOS_PER_MILLI as i64))
            }
            Self::Positive(sec, nanos) => {
                (sec.clone() as i64 * MILLIS_PER_SEC as i64)
                    + (nanos.clone() as i64 / NANOS_PER_MILLI as i64)
            }
            Self::Zero => 0,
        }
    }

    pub fn as_nanos(&self) -> i128 {
        match self {
            Self::Negative(sec, nanos) => {
                -(sec.clone() as i128 * NANOS_PER_SEC as i128 + nanos.clone() as i128)
            }
            Self::Positive(sec, nanos) => {
                sec.clone() as i128 * NANOS_PER_SEC as i128 + nanos.clone() as i128
            }
            Self::Zero => 0,
        }
    }

    pub fn as_sub_nanos(&self) -> u32 {
        match self {
            Self::Negative(_sec, nanos) => nanos.clone(),
            Self::Positive(_sec, nanos) => nanos.clone(),
            Self::Zero => 0,
        }
    }

    pub fn into_nanos_as_i64_unchecked(&self) -> i64 {
        match self {
            Self::Negative(sec, nanos) => {
                -(sec.clone() as i64 * NANOS_PER_SEC as i64 + nanos.clone() as i64)
            }
            Self::Positive(sec, nanos) => {
                sec.clone() as i64 * NANOS_PER_SEC as i64 + nanos.clone() as i64
            }
            Self::Zero => 0,
        }
    }

    pub fn is_negative(&self) -> bool {
        match self {
            Self::Negative(_, _) => true,
            _ => false,
        }
    }

    pub fn is_positive(&self) -> bool {
        match self {
            Self::Positive(_, _) => true,
            _ => false,
        }
    }

    pub fn add_secs(&mut self, secs: u64) {
        match self {
            Self::Negative(ref sec, _) => {
                let new_sec = *sec as i64 - secs as i64;
                if new_sec.is_negative() {
                    *self = Self::Negative(new_sec.abs() as u64, 0);
                } else {
                    *self = Self::Positive(new_sec as u64, 0);
                }
            }
            Self::Positive(sec, _) => {
                *sec += secs;
            }
            Self::Zero => *self = Self::Positive(secs, 0),
        }
    }

    pub fn remove_secs(&mut self, secs: u64) {
        match self {
            Self::Negative(sec, _) => {
                *sec += secs;
            }
            Self::Positive(ref sec, _) => {
                let new_sec = *sec as i64 - secs as i64;
                if new_sec.is_negative() {
                    *self = Self::Positive(new_sec.abs() as u64, 0);
                } else {
                    *self = Self::Negative(new_sec as u64, 0);
                }
            }
            Self::Zero => *self = Self::Negative(secs, 0),
        }
    }

    pub fn add_nanos(&mut self, nanos: u128) {
        let mut add_secs = (nanos / NANOS_PER_SEC as u128) as i64;
        let add_nanos = (nanos % NANOS_PER_SEC as u128) as u32;
        match self {
            Self::Negative(ref mut sec, ref mut n) => {
                let total_nanos = *n as i64 - add_nanos as i64;
                if total_nanos.is_negative() {
                    // If subtracting nanos results in negative nanoseconds, adjust the seconds
                    add_secs += 1; // Borrow one second to adjust nanos
                    *n = (NANOS_PER_SEC as i64 + total_nanos) as u32; // Adjust nanos to be positive
                } else {
                    *n = total_nanos as u32;
                }
                let new_secs = *sec as i64 - add_secs;
                if new_secs.is_negative() {
                    // If total seconds are non-negative, flip to Positive
                    *self = Self::Positive(new_secs.abs() as u64, *n);
                } else {
                    *sec = new_secs as u64;
                }
            }
            Self::Positive(ref mut sec, ref mut n) => {
                let mut total_nanos = *n + add_nanos;
                add_secs += (total_nanos / NANOS_PER_SEC) as i64;
                total_nanos = total_nanos % NANOS_PER_SEC;
                *sec += add_secs as u64;
                *n = total_nanos;
            }
            Self::Zero => *self = Self::Positive(add_secs as u64, add_nanos as u32),
        }
    }

    pub fn remove_nanos(&mut self, nanos: u128) {
        let mut remove_secs = (nanos / NANOS_PER_SEC as u128) as i64;
        let remove_nanos = (nanos % NANOS_PER_SEC as u128) as u32;
        match self {
            Self::Negative(ref mut sec, ref mut n) => {
                let mut total_nanos = *n + remove_nanos;
                remove_secs += (total_nanos / NANOS_PER_SEC) as i64;
                total_nanos = total_nanos % NANOS_PER_SEC;
                *sec += remove_secs as u64;
                *n = total_nanos;
            }
            Self::Positive(ref mut sec, ref mut n) => {
                let total_nanos = *n as i64 - remove_nanos as i64;
                if total_nanos.is_negative() {
                    // If subtracting nanos results in negative nanoseconds, adjust the seconds
                    remove_secs += 1; // Borrow one second to adjust nanos
                    *n = (NANOS_PER_SEC as i64 + total_nanos) as u32; // Adjust nanos to be positive
                } else {
                    *n = total_nanos as u32;
                }
                let new_secs = *sec as i64 - remove_secs;
                if new_secs.is_negative() {
                    // If total seconds are non-negative, flip to Positive
                    *self = Self::Negative(new_secs.abs() as u64, *n);
                } else {
                    *sec = new_secs as u64;
                }
            }
            Self::Zero => *self = Self::Negative(remove_secs as u64, remove_nanos as u32),
        }
    }

    pub fn duration_since<D: Into<Duration>>(&self, earlier: D) -> Self {
        self - &(earlier.into())
    }

    pub(crate) fn subtract_time(&mut self, time: Self) {
        match self {
            Self::Negative(ref mut sec, ref mut n) => match time {
                Self::Negative(sec2, n2) => {
                    let new_nanos = n.clone() as i32 - n2 as i32;
                    if new_nanos.is_negative() {
                        *n = (NANOS_PER_SEC as i32 + new_nanos) as u32;
                        *sec -= 1;
                    } else {
                        *n = new_nanos as u32;
                    }
                    let new_secs = sec.clone() as i64 - sec2 as i64;
                    if new_secs.is_negative() {
                        *self = Self::Positive(new_secs.abs() as u64, *n);
                    } else {
                        *sec = new_secs as u64;
                    }
                }
                Self::Positive(sec2, n2) => {
                    let new_nanos = n.clone() as i32 + n2 as i32;
                    if new_nanos >= NANOS_PER_SEC as i32 {
                        *n = (new_nanos - NANOS_PER_SEC as i32) as u32;
                        *sec += 1;
                    } else {
                        *n = new_nanos as u32;
                    }
                    *sec = (sec.clone() as i64 + sec2 as i64) as u64;
                }
                Self::Zero => {}
            },
            Self::Positive(ref mut sec, ref mut n) => match time {
                Self::Negative(s2, n2) => {
                    let new_nanos = n.clone() as i32 + n2 as i32;
                    if new_nanos >= NANOS_PER_SEC as i32 {
                        *n = (new_nanos - NANOS_PER_SEC as i32) as u32;
                        *sec += 1;
                    } else {
                        *n = new_nanos as u32;
                    }
                    *sec = (sec.clone() as i64 + s2 as i64) as u64;
                }
                Self::Positive(s2, n2) => {
                    let new_nanos = n.clone() as i32 - n2 as i32;
                    if new_nanos.is_negative() {
                        *n = (new_nanos + NANOS_PER_SEC as i32) as u32;
                        *sec -= 1;
                    } else {
                        *n = new_nanos as u32;
                    }
                    let new_secs = sec.clone() as i64 - s2 as i64;
                    if new_secs.is_negative() {
                        *self = Self::Negative(new_secs.abs() as u64, *n);
                    } else {
                        *sec = new_secs as u64;
                    }
                }
                Duration::Zero => {}
            },
            Self::Zero => match time {
                Duration::Negative(s, n) => {
                    *self = Duration::Positive(s, n);
                }
                Duration::Positive(s, n) => {
                    *self = Duration::Negative(s, n);
                }
                Duration::Zero => {}
            },
        }
    }
}

#[cfg(feature = "std")]
impl From<std::time::Duration> for Duration {
    fn from(duration: std::time::Duration) -> Self {
        let sec = duration.as_secs() as i64;
        let nanos = duration.subsec_nanos();
        Duration::from_secs_nanos(&sec, &nanos)
    }
}

impl From<i64> for Duration {
    fn from(duration: i64) -> Self {
        Duration::from_secs(duration)
    }
}

impl From<(i64, u64)> for Duration {
    fn from((sec, nanos): (i64, u64)) -> Self {
        Duration::from_secs_nanos(&sec, &(nanos as u32))
    }
}

impl From<(i64, u32)> for Duration {
    fn from((sec, nanos): (i64, u32)) -> Self {
        Duration::from_secs_nanos(&sec, &nanos)
    }
}
impl From<(u64, u32)> for Duration {
    fn from((sec, nanos): (u64, u32)) -> Self {
        Duration::from_secs_nanos(&(sec as i64), &nanos)
    }
}
impl From<(u32, u32)> for Duration {
    fn from((sec, nanos): (u32, u32)) -> Self {
        Duration::from_secs_nanos(&(sec as i64), &nanos)
    }
}
impl From<(i32, i32)> for Duration {
    fn from((sec, nanos): (i32, i32)) -> Self {
        Duration::from_secs_nanos(&(sec as i64), &(nanos as u32))
    }
}
impl From<(i32, u32)> for Duration {
    fn from((sec, nanos): (i32, u32)) -> Self {
        Duration::from_secs_nanos(&(sec as i64), &nanos)
    }
}

impl Add for Duration {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        let self_nanos = self.as_sub_nanos();
        let other_nanos = other.as_sub_nanos();
        let mut calc_nanos = self_nanos + other_nanos;

        let self_secs = self.as_secs();
        let other_secs = other.as_secs();
        let mut calc_secs = self_secs + other_secs;

        if calc_nanos > NANOS_PER_SEC {
            calc_secs += (calc_nanos / NANOS_PER_SEC) as i64;
            calc_nanos = calc_nanos % NANOS_PER_SEC;
        }
        if calc_secs.is_negative() {
            Self::Negative(calc_secs.abs() as u64, calc_nanos as u32)
        } else {
            Self::Positive(calc_secs as u64, calc_nanos as u32)
        }
    }
}

impl Sub for Duration {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        // Assume these methods convert durations to a total of nanoseconds within the current second
        let self_nanos = self.as_sub_nanos();
        let other_nanos = other.as_sub_nanos();

        // Initial calculation might result in negative nanoseconds
        let mut calc_nanos = self_nanos as i64 - other_nanos as i64;

        // Convert durations to total seconds
        let self_secs = self.as_secs() as i64;
        let other_secs = other.as_secs() as i64;
        let mut calc_secs = self_secs - other_secs;

        // Adjust for negative nanoseconds, ensuring calc_nanos is within 0..NANOS_PER_SEC
        if calc_nanos < 0 {
            calc_secs -= 1;
            calc_nanos += NANOS_PER_SEC as i64; // Adjust nanos back to positive within a second
        }

        // Convert back to Duration, deciding on Positive, Negative, or Zero
        if calc_secs > 0 || (calc_secs == 0 && calc_nanos > 0) {
            Self::Positive(calc_secs as u64, calc_nanos as u32)
        } else if calc_secs < 0 || (calc_secs == 0 && calc_nanos < 0) {
            Self::Negative(calc_secs.abs() as u64, calc_nanos.abs() as u32)
        } else {
            Self::Zero
        }
    }
}

impl Sub for &Duration {
    type Output = Duration;

    fn sub(self, rhs: Self) -> Self::Output {
        self.clone().sub(rhs.clone())
    }
}

#[cfg(test)]
mod test {
    use crate::time::Duration;

    #[test]
    fn test_add_sub_time() {
        let epoch = Duration::from_secs_nanos(&0, &0);
        let sec_10 = Duration::from_secs_nanos(&10, &0);

        assert_eq!(10, (epoch.clone() + sec_10.clone()).as_secs());
        assert_eq!(-10, (epoch.clone() - sec_10.clone()).as_secs());
    }
}