1use std::fmt;
38use std::ops::Deref;
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
64pub struct Seid(pub u64);
65
66impl Seid {
67 #[inline]
69 pub const fn new(value: u64) -> Self {
70 Self(value)
71 }
72
73 #[inline]
75 pub const fn value(&self) -> u64 {
76 self.0
77 }
78}
79
80impl From<u64> for Seid {
81 #[inline]
82 fn from(value: u64) -> Self {
83 Self(value)
84 }
85}
86
87impl From<Seid> for u64 {
88 #[inline]
89 fn from(seid: Seid) -> Self {
90 seid.0
91 }
92}
93
94impl Deref for Seid {
95 type Target = u64;
96
97 #[inline]
98 fn deref(&self) -> &Self::Target {
99 &self.0
100 }
101}
102
103impl fmt::Display for Seid {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 write!(f, "0x{:016X}", self.0)
106 }
107}
108
109impl fmt::LowerHex for Seid {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 fmt::LowerHex::fmt(&self.0, f)
112 }
113}
114
115impl fmt::UpperHex for Seid {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 fmt::UpperHex::fmt(&self.0, f)
118 }
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
149pub struct SequenceNumber(pub u32);
150
151impl SequenceNumber {
152 pub const MAX: u32 = 0x00FFFFFF;
154
155 #[inline]
157 pub const fn new(value: u32) -> Self {
158 Self(value & Self::MAX)
159 }
160
161 #[inline]
163 pub const fn value(&self) -> u32 {
164 self.0
165 }
166
167 #[inline]
181 pub const fn next(&self) -> Self {
182 Self((self.0 + 1) & Self::MAX)
183 }
184
185 #[inline]
189 pub const fn is_valid(&self) -> bool {
190 self.0 <= Self::MAX
191 }
192}
193
194impl From<u32> for SequenceNumber {
195 #[inline]
196 fn from(value: u32) -> Self {
197 Self::new(value)
198 }
199}
200
201impl From<SequenceNumber> for u32 {
202 #[inline]
203 fn from(seq: SequenceNumber) -> Self {
204 seq.0
205 }
206}
207
208impl Deref for SequenceNumber {
209 type Target = u32;
210
211 #[inline]
212 fn deref(&self) -> &Self::Target {
213 &self.0
214 }
215}
216
217impl fmt::Display for SequenceNumber {
218 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 write!(f, "{}", self.0)
220 }
221}
222
223#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
246pub struct Teid(pub u32);
247
248impl Teid {
249 #[inline]
251 pub const fn new(value: u32) -> Self {
252 Self(value)
253 }
254
255 #[inline]
257 pub const fn value(&self) -> u32 {
258 self.0
259 }
260}
261
262impl From<u32> for Teid {
263 #[inline]
264 fn from(value: u32) -> Self {
265 Self(value)
266 }
267}
268
269impl From<Teid> for u32 {
270 #[inline]
271 fn from(teid: Teid) -> Self {
272 teid.0
273 }
274}
275
276impl Deref for Teid {
277 type Target = u32;
278
279 #[inline]
280 fn deref(&self) -> &Self::Target {
281 &self.0
282 }
283}
284
285impl fmt::Display for Teid {
286 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
287 write!(f, "0x{:08X}", self.0)
288 }
289}
290
291impl fmt::LowerHex for Teid {
292 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
293 fmt::LowerHex::fmt(&self.0, f)
294 }
295}
296
297impl fmt::UpperHex for Teid {
298 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
299 fmt::UpperHex::fmt(&self.0, f)
300 }
301}
302
303#[cfg(test)]
308mod tests {
309 use super::*;
310
311 mod seid_tests {
313 use super::*;
314
315 #[test]
316 fn test_seid_new() {
317 let seid = Seid::new(0x123456789ABCDEF0);
318 assert_eq!(seid.value(), 0x123456789ABCDEF0);
319 }
320
321 #[test]
322 fn test_seid_default() {
323 let seid = Seid::default();
324 assert_eq!(*seid, 0);
325 }
326
327 #[test]
328 fn test_seid_from_u64() {
329 let seid: Seid = 42u64.into();
330 assert_eq!(*seid, 42);
331 }
332
333 #[test]
334 fn test_seid_into_u64() {
335 let seid = Seid(100);
336 let value: u64 = seid.into();
337 assert_eq!(value, 100);
338 }
339
340 #[test]
341 fn test_seid_deref() {
342 let seid = Seid(0xDEADBEEF);
343 assert_eq!(*seid, 0xDEADBEEF);
344 }
345
346 #[test]
347 fn test_seid_display() {
348 let seid = Seid(0x123456789ABCDEF0);
349 assert_eq!(format!("{}", seid), "0x123456789ABCDEF0");
350 }
351
352 #[test]
353 fn test_seid_equality() {
354 let seid1 = Seid(100);
355 let seid2 = Seid(100);
356 let seid3 = Seid(200);
357 assert_eq!(seid1, seid2);
358 assert_ne!(seid1, seid3);
359 }
360
361 #[test]
362 fn test_seid_clone() {
363 let seid = Seid(42);
364 let cloned = seid;
365 assert_eq!(seid, cloned);
366 }
367
368 #[test]
369 fn test_seid_hash() {
370 use std::collections::HashSet;
371 let mut set = HashSet::new();
372 set.insert(Seid(1));
373 set.insert(Seid(2));
374 set.insert(Seid(1));
375 assert_eq!(set.len(), 2);
376 }
377 }
378
379 mod sequence_number_tests {
381 use super::*;
382
383 #[test]
384 fn test_sequence_number_new() {
385 let seq = SequenceNumber::new(42);
386 assert_eq!(seq.value(), 42);
387 }
388
389 #[test]
390 fn test_sequence_number_max() {
391 assert_eq!(SequenceNumber::MAX, 0x00FFFFFF);
392 }
393
394 #[test]
395 fn test_sequence_number_masking() {
396 let seq = SequenceNumber::new(0xFF123456);
397 assert_eq!(*seq, 0x123456);
398 }
399
400 #[test]
401 fn test_sequence_number_default() {
402 let seq = SequenceNumber::default();
403 assert_eq!(*seq, 0);
404 }
405
406 #[test]
407 fn test_sequence_number_next() {
408 let seq = SequenceNumber::new(100);
409 assert_eq!(*seq.next(), 101);
410 }
411
412 #[test]
413 fn test_sequence_number_next_wrap() {
414 let seq = SequenceNumber::new(SequenceNumber::MAX);
415 assert_eq!(*seq.next(), 0);
416 }
417
418 #[test]
419 fn test_sequence_number_from_u32() {
420 let seq: SequenceNumber = 50u32.into();
421 assert_eq!(*seq, 50);
422 }
423
424 #[test]
425 fn test_sequence_number_into_u32() {
426 let seq = SequenceNumber::new(75);
427 let value: u32 = seq.into();
428 assert_eq!(value, 75);
429 }
430
431 #[test]
432 fn test_sequence_number_deref() {
433 let seq = SequenceNumber::new(999);
434 assert_eq!(*seq, 999);
435 }
436
437 #[test]
438 fn test_sequence_number_display() {
439 let seq = SequenceNumber::new(12345);
440 assert_eq!(format!("{}", seq), "12345");
441 }
442
443 #[test]
444 fn test_sequence_number_ordering() {
445 let seq1 = SequenceNumber::new(10);
446 let seq2 = SequenceNumber::new(20);
447 assert!(seq1 < seq2);
448 assert!(seq2 > seq1);
449 }
450
451 #[test]
452 fn test_sequence_number_is_valid() {
453 let seq = SequenceNumber::new(100);
454 assert!(seq.is_valid());
455
456 let seq_max = SequenceNumber::new(SequenceNumber::MAX);
457 assert!(seq_max.is_valid());
458 }
459 }
460
461 mod teid_tests {
463 use super::*;
464
465 #[test]
466 fn test_teid_new() {
467 let teid = Teid::new(0x12345678);
468 assert_eq!(teid.value(), 0x12345678);
469 }
470
471 #[test]
472 fn test_teid_default() {
473 let teid = Teid::default();
474 assert_eq!(*teid, 0);
475 }
476
477 #[test]
478 fn test_teid_from_u32() {
479 let teid: Teid = 42u32.into();
480 assert_eq!(*teid, 42);
481 }
482
483 #[test]
484 fn test_teid_into_u32() {
485 let teid = Teid(100);
486 let value: u32 = teid.into();
487 assert_eq!(value, 100);
488 }
489
490 #[test]
491 fn test_teid_deref() {
492 let teid = Teid(0xDEADBEEF);
493 assert_eq!(*teid, 0xDEADBEEF);
494 }
495
496 #[test]
497 fn test_teid_display() {
498 let teid = Teid(0x12345678);
499 assert_eq!(format!("{}", teid), "0x12345678");
500 }
501
502 #[test]
503 fn test_teid_equality() {
504 let teid1 = Teid(100);
505 let teid2 = Teid(100);
506 let teid3 = Teid(200);
507 assert_eq!(teid1, teid2);
508 assert_ne!(teid1, teid3);
509 }
510
511 #[test]
512 fn test_teid_clone() {
513 let teid = Teid(42);
514 let cloned = teid;
515 assert_eq!(teid, cloned);
516 }
517
518 #[test]
519 fn test_teid_hash() {
520 use std::collections::HashSet;
521 let mut set = HashSet::new();
522 set.insert(Teid(1));
523 set.insert(Teid(2));
524 set.insert(Teid(1));
525 assert_eq!(set.len(), 2);
526 }
527 }
528}