proto-types 0.2.2

⚙️ Implementations for various common protobuf types.
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use crate::Duration;
use core::cmp::Ordering;
use core::ops::{Add, Div, Mul, Sub};
use core::time::Duration as StdDuration;

impl PartialEq<StdDuration> for Duration {
	#[inline]
	fn eq(&self, other: &StdDuration) -> bool {
		self.total_nanos() == other.as_nanos().cast_signed()
	}
}

impl PartialEq<Duration> for StdDuration {
	#[inline]
	fn eq(&self, other: &Duration) -> bool {
		other == self
	}
}

impl PartialOrd<StdDuration> for Duration {
	#[inline]
	fn partial_cmp(&self, other: &StdDuration) -> Option<Ordering> {
		Some(
			self.total_nanos()
				.cmp(&other.as_nanos().cast_signed()),
		)
	}
}

impl PartialOrd<Duration> for StdDuration {
	#[inline]
	fn partial_cmp(&self, other: &Duration) -> Option<Ordering> {
		other.partial_cmp(self).map(Ordering::reverse)
	}
}

#[cfg(feature = "chrono")]
impl PartialEq<chrono::TimeDelta> for Duration {
	#[inline]
	fn eq(&self, other: &chrono::TimeDelta) -> bool {
		let other_total = (i128::from(other.num_seconds()) * Self::NANOS_PER_SEC_I128)
			+ i128::from(other.subsec_nanos());

		self.total_nanos() == other_total
	}
}

#[cfg(feature = "chrono")]
impl PartialEq<Duration> for chrono::TimeDelta {
	#[inline]
	fn eq(&self, other: &Duration) -> bool {
		other == self
	}
}

#[cfg(feature = "chrono")]
impl PartialOrd<chrono::TimeDelta> for Duration {
	#[inline]
	fn partial_cmp(&self, other: &chrono::TimeDelta) -> Option<Ordering> {
		let other_total = (i128::from(other.num_seconds()) * Self::NANOS_PER_SEC_I128)
			+ i128::from(other.subsec_nanos());

		Some(self.total_nanos().cmp(&other_total))
	}
}

#[cfg(feature = "chrono")]
impl PartialOrd<Duration> for chrono::TimeDelta {
	#[inline]
	fn partial_cmp(&self, other: &Duration) -> Option<Ordering> {
		other.partial_cmp(self).map(Ordering::reverse)
	}
}

impl Add<StdDuration> for Duration {
	type Output = Self;

	#[inline]
	fn add(self, rhs: StdDuration) -> Self::Output {
		let rhs_s = i64::try_from(rhs.as_secs()).expect("overflow in duration addition");
		let rhs_n = i64::from(rhs.subsec_nanos());

		self.checked_add_raw(rhs_s, rhs_n)
			.expect("overflow in duration addition")
	}
}

impl Add for Duration {
	type Output = Self;
	#[inline]
	fn add(self, rhs: Self) -> Self::Output {
		self.checked_add(&rhs)
			.expect("overflow in duration addition")
	}
}

impl Sub for Duration {
	type Output = Self;

	#[inline]
	fn sub(self, other: Self) -> Self {
		self.checked_sub(&other)
			.expect("overflow in duration subtraction")
	}
}

impl Sub<StdDuration> for Duration {
	type Output = Self;

	#[inline]
	fn sub(self, rhs: StdDuration) -> Self::Output {
		let rhs_s = i64::try_from(rhs.as_secs()).expect("overflow in duration subtraction");
		let rhs_n = i64::from(rhs.subsec_nanos());

		self.checked_sub_raw(rhs_s, rhs_n)
			.expect("overflow in duration subtraction")
	}
}

#[cfg(feature = "chrono")]
impl Add<chrono::TimeDelta> for Duration {
	type Output = Self;

	#[inline]
	fn add(self, rhs: chrono::TimeDelta) -> Self::Output {
		self.checked_add_raw(rhs.num_seconds(), i64::from(rhs.subsec_nanos()))
			.expect("overflow in duration addition")
	}
}

#[cfg(feature = "chrono")]
impl Sub<chrono::TimeDelta> for Duration {
	type Output = Self;

	#[inline]
	fn sub(self, rhs: chrono::TimeDelta) -> Self::Output {
		self.checked_sub_raw(rhs.num_seconds(), i64::from(rhs.subsec_nanos()))
			.expect("overflow in duration subtraction")
	}
}

impl Mul<i64> for Duration {
	type Output = Self;

	#[inline]
	fn mul(self, rhs: i64) -> Self {
		self.checked_mul(rhs)
			.expect("Duration multiplication by i64 overflowed")
	}
}

impl Mul<i32> for Duration {
	type Output = Self;

	#[inline]
	fn mul(self, rhs: i32) -> Self {
		self.checked_mul(i64::from(rhs)) // Simply cast to i64 and use the i64 implementation
			.expect("Duration multiplication by i32 overflowed")
	}
}

impl Div<i64> for Duration {
	type Output = Self;

	#[inline]
	fn div(self, rhs: i64) -> Self {
		self.checked_div(rhs)
			.expect("Duration division by i64 overflowed or divided by zero")
	}
}

impl Div<i32> for Duration {
	type Output = Self;

	#[inline]
	fn div(self, rhs: i32) -> Self {
		self.checked_div(i64::from(rhs))
			.expect("Duration division by i32 overflowed or divided by zero")
	}
}

impl Duration {
	const NANOS_PER_SEC: i64 = 1_000_000_000;
	const NANOS_PER_SEC_I128: i128 = 1_000_000_000;

	fn align_signs(mut s: i64, mut n: i32) -> Option<Self> {
		if s > 0 && n < 0 {
			s = s.checked_sub(1)?;
			n += 1_000_000_000;
		} else if s < 0 && n > 0 {
			s = s.checked_add(1)?;
			n -= 1_000_000_000;
		}
		Some(Self {
			seconds: s,
			nanos: n,
		})
	}

	fn checked_add_raw(&self, rhs_s: i64, rhs_n: i64) -> Option<Self> {
		let mut s = self.seconds.checked_add(rhs_s)?;
		let mut n_total = i64::from(self.nanos) + rhs_n;

		if n_total >= 1_000_000_000 {
			s = s.checked_add(1)?;
			n_total -= 1_000_000_000;
		} else if n_total <= -1_000_000_000 {
			s = s.checked_sub(1)?;
			n_total += 1_000_000_000;
		}

		if s > 0 && n_total < 0 {
			s = s.checked_sub(1)?;
			n_total += 1_000_000_000;
		} else if s < 0 && n_total > 0 {
			s = s.checked_add(1)?;
			n_total -= 1_000_000_000;
		}

		Some(Self {
			seconds: s,
			#[allow(clippy::cast_possible_truncation)]
			nanos: n_total as i32,
		})
	}

	fn checked_sub_raw(&self, rhs_s: i64, rhs_n: i64) -> Option<Self> {
		let mut s = self.seconds.checked_sub(rhs_s)?;
		let mut n_total = i64::from(self.nanos) - rhs_n;

		if n_total >= Self::NANOS_PER_SEC {
			s = s.checked_add(1)?;
			n_total -= Self::NANOS_PER_SEC;
		} else if n_total <= -Self::NANOS_PER_SEC {
			s = s.checked_sub(1)?;
			n_total += Self::NANOS_PER_SEC;
		}

		#[allow(clippy::cast_possible_truncation)]
		Self::align_signs(s, n_total as i32)
	}

	/// Returns the total nanoseconds for this instance.
	#[inline]
	#[must_use]
	pub const fn total_nanos(&self) -> i128 {
		(self.seconds as i128) * (Self::NANOS_PER_SEC_I128) + (self.nanos as i128)
	}

	/// Creates a new normalized instance from a given amount of nanoseconds.
	#[must_use]
	#[inline]
	pub fn from_total_nanos(total: i128) -> Option<Self> {
		let factor = Self::NANOS_PER_SEC_I128;

		// Integer division truncates towards zero (correct for seconds)
		let seconds_val = total / factor;
		let seconds = i64::try_from(seconds_val).ok()?;

		// Remainder has same sign as dividend
		let nanos_val = total % factor;
		// Remainder guaranteed to fit in i32
		#[allow(clippy::cast_possible_truncation)]
		let nanos = nanos_val as i32;

		Some(Self { seconds, nanos })
	}

	/// Multiplies the Duration by an i64 scalar, returning `Some(Duration)` or `None` on overflow.
	#[must_use]
	#[inline]
	pub fn checked_mul(&self, rhs: i64) -> Option<Self> {
		let total = self.total_nanos().checked_mul(i128::from(rhs))?;
		Self::from_total_nanos(total)
	}

	/// Adds another Duration to this one, returning `Some(Duration)` or `None` on overflow.
	#[must_use]
	#[inline]
	pub fn checked_add(&self, other: &Self) -> Option<Self> {
		self.checked_add_raw(other.seconds, other.nanos.into())
	}

	/// Subtracts another Duration from this one, returning `Some(Duration)` or `None` on overflow.
	#[must_use]
	#[inline]
	pub fn checked_sub(&self, other: &Self) -> Option<Self> {
		self.checked_sub_raw(other.seconds, other.nanos.into())
	}

	/// Divides the Duration by an i64 scalar, returning `Some(Duration)` or `None` on overflow.
	#[must_use]
	#[inline]
	pub fn checked_div(&self, rhs: i64) -> Option<Self> {
		if rhs == 0 {
			return None;
		}
		let total = self.total_nanos().checked_div(i128::from(rhs))?;
		Self::from_total_nanos(total)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use core::cmp::Ordering;

	macro_rules! get_duration {
		(duration, $secs:literal, $nanos:literal) => {
			Duration::new($secs, $nanos)
		};

		(std, $secs:literal, $nanos:literal) => {
			StdDuration::new($secs, $nanos)
		};

		(chrono, $secs:literal, $nanos:literal) => {
			TimeDelta::new($secs, $nanos).unwrap()
		};
	}

	macro_rules! test_ops {
		($duration:ident) => {
			#[test]
			fn test_add_sub() {
				// 1. Add causing carry
				let d1 = dur(1, 900_000_000);
				let d2 = get_duration!($duration, 0, 200_000_000);
				let sum = d1 + d2;
				assert_eq!(sum.seconds, 2);
				assert_eq!(sum.nanos, 100_000_000);

				// 2. Sub causing borrow
				let d1 = dur(2, 100_000_000);
				let d2 = get_duration!($duration, 0, 200_000_000);
				let diff = d1 - d2;
				assert_eq!(diff.seconds, 1);
				assert_eq!(diff.nanos, 900_000_000);

				// 3. Sub causing negative result
				let d1 = dur(1, 0);
				let d2 = get_duration!($duration, 2, 0);
				let diff = d1 - d2;
				// -1s
				assert_eq!(diff.seconds, -1);
				assert_eq!(diff.nanos, 0);
			}

			#[test]
			fn test_eq_ord() {
				let d1 = dur(1, 500);
				let d2 = get_duration!($duration, 1, 600);
				let d3 = get_duration!($duration, 2, 0);

				assert!(d1 < d2);
				assert!(d2 < d3);

				// Test normalization-independent comparison
				// 0s + 1.5B nanos vs 1s + 500M nanos (Should be Equal)
				let unnormalized = dur(0, 1_500_000_000);
				let normalized = get_duration!($duration, 1, 500_000_000);
				assert_eq!(unnormalized.partial_cmp(&normalized), Some(Ordering::Equal));
			}
		};
	}

	test_ops!(duration);

	mod std_test {
		use super::*;

		test_ops!(std);
	}

	#[cfg(feature = "chrono")]
	mod chrono_test {
		use super::*;
		use chrono::TimeDelta;

		test_ops!(chrono);
	}

	fn dur(s: i64, n: i32) -> Duration {
		Duration {
			seconds: s,
			nanos: n,
		}
	}

	#[test]
	fn test_mul_overflow_checks() {
		// 1. Basic
		let d = dur(10, 0);
		assert_eq!(d * 2, dur(20, 0));

		// 2. Overflow i64 seconds via huge multiplier
		let huge = dur(i64::MAX / 2 + 100, 0);
		assert!(huge.checked_mul(2).is_none());

		// 3. Nanos overflow contributing to seconds overflow
		let edge = dur(i64::MAX, 0);
		assert!(edge.checked_mul(1).is_some());

		// i64::MAX s + 1s via nanos carry -> Overflow
		let edge_nanos = dur(i64::MAX - 1, 600_000_000);
		// * 2 -> (MAX-1)*2 s + 1.2s -> Overflow
		assert!(edge_nanos.checked_mul(2).is_none());
	}

	#[test]
	fn test_div_bug_fix() {
		// Before the i128-based impl, this would have failed
		let numerator = dur(10_000_000_000, 0); // 10B seconds
		let divisor = 11_000_000_000; // 11B

		// Expected: 0 seconds, ~0.909s (909,090,909 nanos)
		let res = numerator.checked_div(divisor).unwrap();
		assert_eq!(res.seconds, 0);
		// 10B * 1e9 / 11B = 10 * 1e9 / 11 = 909090909
		assert_eq!(res.nanos, 909_090_909);
	}

	#[test]
	fn test_div_edge_cases() {
		// Division by zero
		let d = dur(1, 0);
		assert!(d.checked_div(0).is_none());

		// Clean division
		let d = dur(1, 0);
		let res: Duration = d / 2;
		assert_eq!(res.seconds, 0);
		assert_eq!(res.nanos, 500_000_000);

		// Negative division
		let d = dur(-1, 0);
		let res: Duration = d / 2;
		assert_eq!(res.seconds, 0);
		assert_eq!(res.nanos, -500_000_000);
	}

	#[test]
	fn test_get_data_smoke_test() {
		// Just verifying the math helper runs without panic
		// 1 Year + 1 Month + 1 Day ...
		let d = dur(31_557_600 + 1, 0); // Approx 1 year + 1 sec
		let data = d.get_data();
		assert_eq!(data.years.value, 1);
	}
}