Crates.io | tetanus |
lib.rs | tetanus |
version | |
source | src |
created_at | 2025-01-20 19:24:07.806581+00 |
updated_at | 2025-01-20 20:37:28.123356+00 |
description | A custom utils library for some common unsafe operations |
homepage | |
repository | |
max_upload_size | |
id | 1524190 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A rusty extension of the standard library with powerful utilities and ergonomic tools.
Add this to your Cargo.toml
:
[dependencies]
tetanus = "0.1.0"
Create collections with easy-to-read syntax:
use tetanus::{map, set, vec_of};
// Create a vector with repeated elements
let zeros = vec_of!(0; 5); // [0, 0, 0, 0, 0]
// Create a HashMap inline
let config = map! {
"host" => "localhost",
"port" => "8080",
};
// Create a HashSet inline
let permissions = set!["read", "write", "execute"];
Enhanced Option handling:
use tetanus::OptionExt;
let value = Some(42);
assert!(value.is_none_or(|&x| x == 42)); // true
assert!(!value.is_none_or(|&x| x > 100)); // false
let doubled = value.map_ref(|&x| x * 2); // Some(84)
Easy-to-use atomic containers:
use tetanus::Atomic;
let counter = Atomic::new(0);
let counter_clone = counter.clone();
std::thread::spawn(move || {
*counter_clone.get_mut() += 1;
});
*counter.get_mut() += 1;
String manipulation tools:
use tetanus::StringExt;
let mut s = String::from("HelloWorld");
s.to_snake_case(); // "hello_world"
s.to_camel_case(); // "HelloWorld"
s.truncate(5); // "Hello..."
Enhanced vector operations:
use tetanus::VecExt;
let mut numbers = vec![1, 2, 3, 4, 5];
// Remove all even numbers
numbers.remove_all(|&x| x % 2 == 0);
// Replace numbers greater than 3 with 0
numbers.replace_all(|&x| x > 3, 0);
// Insert maintaining sort order
numbers.insert_sorted(4);
Fixed-size circular buffer:
use tetanus::RingBuffer;
let mut buffer = RingBuffer::new(3);
buffer.push(1);
buffer.push(2);
buffer.push(3);
buffer.push(4); // Automatically removes 1
assert_eq!(buffer.iter().collect::<Vec<_>>(), vec![2, 3, 4]);
Token bucket rate limiting:
use tetanus::RateLimiter;
let mut limiter = RateLimiter::new(
capacity: 100, // bucket size
refill_rate: 10.0 // tokens per second
);
if limiter.try_acquire() {
// Perform rate-limited operation
}
High-precision timing tools:
use tetanus::timing::{Timer, Stopwatch};
// Simple timer
let timer = Timer::new();
// ... do work ...
println!("Operation took {} ms", timer.elapsed_ms());
// Stopwatch for multiple measurements
let mut sw = Stopwatch::new();
// ... do work ...
let split1 = sw.split();
// ... do more work ...
let split2 = sw.split();
println!("Splits: {:?}", sw.splits());
Cache function results automatically:
use tetanus::memoize;
memoize! {
fn fib(n: u64) -> u64 {
if n <= 1 {
n
} else {
fib(n - 1) + fib(n - 2)
}
}
}
// First call computes the result
let result1 = fib(10); // Computed
// Subsequent calls use cached result
let result2 = fib(10); // Retrieved from cache
Compute statistics directly on iterators:
use tetanus::StatisticsExt;
let numbers = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let mean = numbers.iter().copied().mean(); // Some(3.0)
let variance = numbers.iter().copied().variance(); // Some(2.0)
let std_dev = numbers.iter().copied().std_dev(); // Some(√2)
Thread-safe cache with automatic entry expiration:
use tetanus::ExpiringCache;
use std::time::Duration;
let cache = ExpiringCache::new(Duration::from_secs(5));
cache.insert("key", "value");
// Value expires after 5 seconds
std::thread::sleep(Duration::from_secs(6));
assert_eq!(cache.get(&"key"), None);
Automatically retry operations with configurable backoff:
use tetanus::RetryWithBackoff;
use std::time::Duration;
let retry = RetryWithBackoff::new(
max_attempts: 3,
initial_delay: Duration::from_millis(10),
max_delay: Duration::from_millis(100),
factor: 2.0
);
// Retry an operation that might fail
let result = retry.retry(|| {
// Your operation here
}).await;
Read large files with progress callbacks:
use tetanus::ChunkedReadExt;
use std::fs::File;
use std::io::BufReader;
let file = File::open("large_file.txt")?;
let mut reader = BufReader::new(file);
let data = reader.read_chunks_with_progress(1024, |chunk_size, total_read| {
println!("Read {} bytes, total: {} bytes", chunk_size, total_read);
})?;
Tetanus is designed with performance in mind:
We welcome contributions! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Special thanks to:
Made with ❤️ by the Tetanus team