tetanus 0.3.0

A custom utils library for some common unsafe 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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#![allow(unused_macros)]

//! # Tetanus - Extended Rust Standard Library
//! 
//! `tetanus` is a utility library that extends Rust's standard library with additional
//! functionality, convenient macros, and ergonomic tools for common programming tasks.

use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::Hash;
use std::io::{self, Read};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// Creates a vector with given elements
#[macro_export]
macro_rules! vec_of {
    ($elem:expr; $n:expr) => {
        vec![$elem; $n]
    };
}

/// Creates a HashMap from key-value pairs
#[macro_export]
macro_rules! map {
    ($($key:expr => $value:expr),* $(,)?) => {{
        let mut map = HashMap::new();
        $(
            map.insert($key, $value);
        )*
        map
    }};
}

/// Creates a HashSet from elements
#[macro_export]
macro_rules! set {
    ($($element:expr),* $(,)?) => {{
        let mut set = HashSet::new();
        $(
            set.insert($element);
        )*
        set
    }};
}

/// Extension trait for Option types
pub trait OptionExt<T> {
    fn is_none_or<F>(&self, f: F) -> bool where F: FnOnce(&T) -> bool;
    fn map_ref<U, F>(&self, f: F) -> Option<U> where F: FnOnce(&T) -> U;
}

impl<T> OptionExt<T> for Option<T> {
    fn is_none_or<F>(&self, f: F) -> bool where F: FnOnce(&T) -> bool {
        match self {
            None => true,
            Some(x) => f(x)
        }
    }

    fn map_ref<U, F>(&self, f: F) -> Option<U> where F: FnOnce(&T) -> U {
        self.as_ref().map(f)
    }
}

/// Thread-safe mutable container
#[derive(Default)]
pub struct Atomic<T>(Arc<Mutex<T>>);

impl<T> Atomic<T> {
    pub fn new(value: T) -> Self {
        Self(Arc::new(Mutex::new(value)))
    }

    pub fn get_mut(&self) -> std::sync::MutexGuard<T> {
        self.0.lock().unwrap()
    }

    pub fn clone(&self) -> Self {
        Self(Arc::clone(&self.0))
    }
}

/// Extension trait for strings
pub trait StringExt {
    fn truncate(&mut self, max_chars: usize);
    fn to_snake_case(&self) -> String;
    fn to_camel_case(&self) -> String;
}

impl StringExt for String {
    fn truncate(&mut self, max_chars: usize) {
        let char_count = self.chars().count();
        if char_count > max_chars {
            let truncated: String = self.chars()
                .take(max_chars)
                .collect();
            self.clear();
            self.push_str(&truncated);
            self.push_str("...");
        }
    }

    fn to_snake_case(&self) -> String {
        let mut result = String::with_capacity(self.len() * 2);
        let mut chars = self.chars().peekable();
        
        while let Some(current) = chars.next() {
            if current.is_uppercase() {
                if !result.is_empty() && !result.ends_with('_') {
                    let next_is_lower = chars.peek().map_or(false, |next| next.is_lowercase());
                    if next_is_lower {
                        result.push('_');
                    }
                }
                result.extend(current.to_lowercase());
            } else {
                result.push(current);
            }
        }
        result
    }

    fn to_camel_case(&self) -> String {
        let mut result = String::with_capacity(self.len());
        let mut capitalize_next = true;
        
        for c in self.chars() {
            if c == '_' {
                capitalize_next = true;
            } else if capitalize_next {
                result.extend(c.to_uppercase());
                capitalize_next = false;
            } else {
                result.push(c);
            }
        }
        result
    }
}

/// Extension trait for vectors
pub trait VecExt<T> {
    fn remove_all<F>(&mut self, predicate: F) where F: Fn(&T) -> bool;
    fn replace_all<F>(&mut self, predicate: F, replacement: T) where F: Fn(&T) -> bool, T: Clone;
    fn insert_sorted(&mut self, element: T) where T: Ord;
}

impl<T> VecExt<T> for Vec<T> {
    fn remove_all<F>(&mut self, predicate: F) where F: Fn(&T) -> bool {
        self.retain(|x| !predicate(x));
    }

    fn replace_all<F>(&mut self, predicate: F, replacement: T) where F: Fn(&T) -> bool, T: Clone {
        for item in self.iter_mut() {
            if predicate(item) {
                *item = replacement.clone();
            }
        }
    }

    fn insert_sorted(&mut self, element: T) where T: Ord {
        match self.binary_search(&element) {
            Ok(pos) | Err(pos) => self.insert(pos, element),
        }
    }
}

/// A fixed-size ring buffer
pub struct RingBuffer<T> {
    buffer: VecDeque<T>,
    capacity: usize,
}

impl<T> RingBuffer<T> {
    pub fn new(capacity: usize) -> Self {
        Self {
            buffer: VecDeque::with_capacity(capacity),
            capacity,
        }
    }

    pub fn push(&mut self, item: T) {
        if self.buffer.len() == self.capacity {
            self.buffer.pop_front();
        }
        self.buffer.push_back(item);
    }

    pub fn iter(&self) -> impl Iterator<Item = &T> {
        self.buffer.iter()
    }

    pub fn clear(&mut self) {
        self.buffer.clear();
    }

    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    pub fn is_full(&self) -> bool {
        self.buffer.len() == self.capacity
    }
}

/// Result extension methods
pub trait ResultExt<T, E> {
    fn on_error<F>(self, f: F) -> Self where F: FnOnce(&E);
    fn ignore_err(self) -> Option<T>;
}

impl<T, E> ResultExt<T, E> for Result<T, E> {
    fn on_error<F>(self, f: F) -> Self where F: FnOnce(&E) {
        if let Err(ref e) = self {
            f(e);
        }
        self
    }

    fn ignore_err(self) -> Option<T> {
        self.ok()
    }
}

/// Rate limiter with token bucket algorithm
pub struct RateLimiter {
    capacity: u32,
    tokens: u32,
    refill_rate: f64,
    last_refill: Instant,
}

impl RateLimiter {
    pub fn new(capacity: u32, refill_rate_per_second: f64) -> Self {
        Self {
            capacity,
            tokens: capacity,
            refill_rate: refill_rate_per_second,
            last_refill: Instant::now(),
        }
    }

    pub fn try_acquire(&mut self) -> bool {
        self.refill();
        if self.tokens > 0 {
            self.tokens -= 1;
            true
        } else {
            false
        }
    }

    fn refill(&mut self) {
        let now = Instant::now();
        let elapsed = now.duration_since(self.last_refill).as_secs_f64();
        let new_tokens = (elapsed * self.refill_rate) as u32;
        if new_tokens > 0 {
            self.tokens = (self.tokens + new_tokens).min(self.capacity);
            self.last_refill = now;
        }
    }
}

/// Timing utilities
pub mod timing {
    use super::*;

    /// High-precision timer
    pub struct Timer(Instant);

    impl Timer {
        pub fn new() -> Self {
            Self(Instant::now())
        }

        pub fn elapsed(&self) -> Duration {
            self.0.elapsed()
        }

        pub fn elapsed_ms(&self) -> u128 {
            self.elapsed().as_millis()
        }
    }

    /// Simple stopwatch for multiple timing measurements
    pub struct Stopwatch {
        start: Instant,
        splits: Vec<Duration>,
    }

    impl Stopwatch {
        pub fn new() -> Self {
            Self {
                start: Instant::now(),
                splits: Vec::new(),
            }
        }

        pub fn split(&mut self) -> Duration {
            let split = self.start.elapsed();
            self.splits.push(split);
            split
        }

        pub fn splits(&self) -> &[Duration] {
            &self.splits
        }

        pub fn reset(&mut self) {
            self.start = Instant::now();
            self.splits.clear();
        }
    }
}

/// Creates a memoized function
#[macro_export]
macro_rules! memoize {
    (fn $name:ident($($arg:ident: $type:ty),*) -> $ret:ty $body:block) => {
        fn $name($($arg: $type),*) -> $ret {
            use std::collections::HashMap;
            use std::sync::Mutex;
            use std::sync::Once;
            
            static INIT: Once = Once::new();
            static mut CACHE: Option<Mutex<HashMap<($($type),*), $ret>>> = None;
            
            INIT.call_once(|| {
                unsafe {
                    CACHE = Some(Mutex::new(HashMap::new()));
                }
            });
            
            let cache = unsafe { CACHE.as_ref().unwrap() };
            let mut cache = cache.lock().unwrap();
            
            if let Some(result) = cache.get(&($($arg),*)) {
                return result.clone();
            }
            
            let result = (|| $body)();
            cache.insert(($($arg),*), result.clone());
            result
        }
    };
}

/// Extension trait for iterators to provide statistical operations
pub trait StatisticsExt: Iterator + Sized
where
    Self::Item: Into<f64> + Copy,
{
    fn mean(mut self) -> Option<f64> {
        let mut count = 0;
        let mut sum = 0.0;
        
        while let Some(value) = self.next() {
            count += 1;
            sum += value.into();
        }
        
        if count > 0 {
            Some(sum / count as f64)
        } else {
            None
        }
    }

    fn variance(mut self) -> Option<f64> {
        let mut values: Vec<f64> = Vec::new();
        while let Some(value) = self.next() {
            values.push(value.into());
        }
        
        if values.is_empty() {
            return None;
        }
        
        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance = values.iter()
            .map(|&x| (x - mean).powi(2))
            .sum::<f64>() / values.len() as f64;
            
        Some(variance)
    }

    fn std_dev(self) -> Option<f64> {
        self.variance().map(|v| v.sqrt())
    }
}

impl<T: Iterator> StatisticsExt for T 
where
    T::Item: Into<f64> + Copy
{}

/// A thread-safe cache with automatic expiration of entries
pub struct ExpiringCache<K, V> {
    cache: Arc<Mutex<HashMap<K, (V, Instant)>>>,
    ttl: Duration,
}

impl<K: Eq + Hash + Clone, V: Clone> ExpiringCache<K, V> {
    pub fn new(ttl: Duration) -> Self {
        Self {
            cache: Arc::new(Mutex::new(HashMap::new())),
            ttl,
        }
    }

    pub fn insert(&self, key: K, value: V) {
        let mut cache = self.cache.lock().unwrap();
        cache.insert(key, (value, Instant::now()));
    }

    pub fn get(&self, key: &K) -> Option<V> {
        let mut cache = self.cache.lock().unwrap();
        
        if let Some((value, timestamp)) = cache.get(key) {
            if timestamp.elapsed() > self.ttl {
                cache.remove(key);
                None
            } else {
                Some(value.clone())
            }
        } else {
            None
        }
    }

    pub fn cleanup(&self) {
        let mut cache = self.cache.lock().unwrap();
        cache.retain(|_, (_, timestamp)| timestamp.elapsed() <= self.ttl);
    }
}

/// A retry mechanism with exponential backoff
pub struct RetryWithBackoff {
    max_attempts: u32,
    initial_delay: Duration,
    max_delay: Duration,
    factor: f64,
}

impl RetryWithBackoff {
    pub fn new(max_attempts: u32, initial_delay: Duration, max_delay: Duration, factor: f64) -> Self {
        Self {
            max_attempts,
            initial_delay,
            max_delay,
            factor,
        }
    }

    pub async fn retry<F, T, E>(&self, mut operation: F) -> Result<T, E>
    where
        F: FnMut() -> Result<T, E>,
    {
        let mut attempts = 0;
        let mut delay = self.initial_delay;

        loop {
            match operation() {
                Ok(value) => return Ok(value),
                Err(e) => {
                    attempts += 1;
                    if attempts >= self.max_attempts {
                        return Err(e);
                    }
                    
                    std::thread::sleep(delay);
                    delay = Duration::from_secs_f64(
                        (delay.as_secs_f64() * self.factor)
                            .min(self.max_delay.as_secs_f64())
                    );
                }
            }
        }
    }
}

/// Extension trait for reading chunks of data with progress tracking
pub trait ChunkedReadExt: Read {
    fn read_chunks_with_progress<F>(
        &mut self,
        chunk_size: usize,
        mut progress_callback: F
    ) -> io::Result<Vec<u8>>
    where
        F: FnMut(usize, usize)
    {
        let mut buffer = Vec::new();
        let mut chunk = vec![0; chunk_size];
        let mut total_bytes = 0;
        
        loop {
            match self.read(&mut chunk) {
                Ok(0) => break,
                Ok(n) => {
                    buffer.extend_from_slice(&chunk[..n]);
                    total_bytes += n;
                    progress_callback(n, total_bytes);
                }
                Err(e) => return Err(e),
            }
        }
        
        Ok(buffer)
    }
}

impl<R: Read> ChunkedReadExt for R {}

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

    #[test]
    fn test_vec_of() {
        let v = vec_of!(42; 3);
        assert_eq!(v, vec![42, 42, 42]);
    }

    #[test]
    fn test_map() {
        let m = map! {
            "a" => 1,
            "b" => 2,
        };
        assert_eq!(m.get("a"), Some(&1));
        assert_eq!(m.get("b"), Some(&2));
    }

    #[test]
    fn test_set() {
        let s = set![1, 2, 3];
        assert!(s.contains(&1));
        assert!(s.contains(&2));
        assert!(s.contains(&3));
    }

    #[test]
    fn test_option_ext() {
        let some_val = Some(42);
        let none_val: Option<i32> = None;

        assert!(!some_val.is_none_or(|x| x > 100));
        assert!(some_val.is_none_or( |x| x == 42));
        assert!(none_val.is_none_or( |x| x > 0));

        assert_eq!(some_val.map_ref(|&x| x * 2), Some(84));
        assert_eq!(none_val.map_ref(|&x| x * 2), None);
    }

    #[test]
    fn test_vec_ext() {
        let mut v = vec![1, 2, 3, 4, 5];
        v.remove_all(|&x| x % 2 == 0);
        assert_eq!(v, vec![1, 3, 5]);

        let mut v = vec![1, 2, 3];
        v.replace_all(|&x| x > 1, 0);
        assert_eq!(v, vec![1, 0, 0]);

        let mut v = vec![1, 3, 5];
        v.insert_sorted(4);
        assert_eq!(v, vec![1, 3, 4, 5]);
    }

    #[test]
    fn test_ring_buffer() {
        let mut buffer = RingBuffer::new(3);
        buffer.push(1);
        buffer.push(2);
        buffer.push(3);
        assert!(buffer.is_full());
        
        buffer.push(4);
        let items: Vec<_> = buffer.iter().copied().collect();
        assert_eq!(items, vec![2, 3, 4]);
    }

    #[test]
    fn test_rate_limiter() {
        let mut limiter = RateLimiter::new(3, 1.0);
        assert!(limiter.try_acquire());
        assert!(limiter.try_acquire());
        assert!(limiter.try_acquire());
        assert!(!limiter.try_acquire());
    }

    #[test]
    fn test_stopwatch() {
        use std::thread::sleep;
        
        let mut sw = timing::Stopwatch::new();
        sleep(Duration::from_millis(10));
        let split1 = sw.split();
        assert!(split1.as_millis() >= 10);
        
        sleep(Duration::from_millis(10));
        let split2 = sw.split();
        assert!(split2.as_millis() >= 20);
        
        assert_eq!(sw.splits().len(), 2);
    }
}

#[cfg(test)]
mod extension_tests {
    use super::*;
    use std::io::Cursor;
    use std::thread;

    #[test]
    fn test_statistics_ext() {
        let numbers = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        
        // Test mean
        assert_eq!(
            numbers.iter().copied().mean(),
            Some(3.0)
        );
        
        // Test variance
        let variance = numbers.iter().copied().variance().unwrap();
        assert!((variance - 2.0).abs() < 1e-10);
        
        // Test standard deviation
        let std_dev = numbers.iter().copied().std_dev().unwrap();
        assert!((std_dev - 1.4142135623730951).abs() < 1e-10);
        
        // Test empty iterator
        let empty: Vec<f64> = vec![];
        assert_eq!(empty.iter().copied().mean(), None);
        assert_eq!(empty.iter().copied().variance(), None);
        assert_eq!(empty.iter().copied().std_dev(), None);
        
        // Test with integers
        let int_numbers = vec![1, 2, 3, 4, 5];
        assert_eq!(
            int_numbers.iter().copied().mean(),
            Some(3.0)
        );
    }

    #[test]
    fn test_expiring_cache() {
        let cache = ExpiringCache::new(Duration::from_millis(100));
        
        // Test basic insertion and retrieval
        cache.insert("key1", "value1");
        assert_eq!(cache.get(&"key1"), Some("value1"));
        
        // Test expiration
        cache.insert("key2", "value2");
        thread::sleep(Duration::from_millis(150));
        assert_eq!(cache.get(&"key2"), None);
        
        // Test cleanup
        cache.insert("key3", "value3");
        thread::sleep(Duration::from_millis(50));
        cache.insert("key4", "value4");
        thread::sleep(Duration::from_millis(60));
        
        cache.cleanup();
        assert_eq!(cache.get(&"key3"), None);
        assert_eq!(cache.get(&"key4"), Some("value4"));
    }

    #[tokio::test]
    async fn test_retry_with_backoff() {
        let retry = RetryWithBackoff::new(
            3,                                    // max attempts
            Duration::from_millis(10),           // initial delay
            Duration::from_millis(100),          // max delay
            2.0                                  // backoff factor
        );

        // Test successful operation
        let mut counter = 0;
        let result = retry.retry(|| {
            counter += 1;
            Ok::<_, &str>(counter)
        }).await;
        assert_eq!(result, Ok(1));
        assert_eq!(counter, 1);

        // Test operation that fails then succeeds
        let mut attempts = 0;
        let result = retry.retry(|| {
            attempts += 1;
            if attempts < 2 {
                Err("not yet")
            } else {
                Ok(attempts)
            }
        }).await;
        assert_eq!(result, Ok(2));
        assert_eq!(attempts, 2);

        // Test operation that always fails
        let mut fail_counter = 0;
        let result: Result<(), &str> = retry.retry(|| {
            fail_counter += 1;
            Err("always fails")
        }).await;
        assert!(result.is_err());
        assert_eq!(fail_counter, 3); // Should have tried 3 times
    }

    #[test]
    fn test_chunked_read_with_progress() {
        // Create test data
        let data = (0..100).collect::<Vec<u8>>();
        let mut cursor = Cursor::new(data.clone());
        
        // Track progress
        let mut chunks_received = 0;
        let mut total_bytes_received = 0;
        
        // Read with progress tracking
        let result = cursor.read_chunks_with_progress(10, |chunk_size, total| {
            chunks_received += 1;
            assert!(chunk_size <= 10); // Ensure chunk size is never larger than specified
            assert!(total <= 100);     // Ensure total never exceeds data size
            total_bytes_received = total;
        }).unwrap();
        
        // Verify results
        assert_eq!(result, data);
        assert_eq!(total_bytes_received, 100);
        assert_eq!(chunks_received, 10);
        
        // Test empty read
        let mut empty_cursor = Cursor::new(Vec::<u8>::new());
        let empty_result = empty_cursor.read_chunks_with_progress(10, |_, _| {
            panic!("Progress callback should not be called for empty read");
        }).unwrap();
        assert!(empty_result.is_empty());
    }
}