sleep-utils
A smart sleep utilities library for Rust with flexible input formats and automatic zero-value handling.
Features
- 🕒 Multiple input formats: numbers, text,
Duration - 🚫 Automatic zero/negative handling: no sleep for zero or negative values
- 📏 Multiple time units: milliseconds, seconds, minutes
- 💻 Platform-compatible: uses
isizefor cross-platform support - ⚡ High-performance: optimized regex parsing
- 🎯 Smart parsing: intuitive duration string parsing
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Quick Start
use smart_sleep;
use Duration;
// Various input formats
smart_sleep.unwrap; // 100 milliseconds
smart_sleep.unwrap; // 200 milliseconds
smart_sleep.unwrap; // 1.5 seconds
smart_sleep.unwrap; // 2 seconds
smart_sleep.unwrap; // no sleep
smart_sleep.unwrap; // no sleep
smart_sleep.unwrap; // 1 second
Supported Formats
Numbers (treated as milliseconds)
100→ 100ms500→ 500ms0→ no sleep-100→ no sleep
Text with Units
"100ms","100 millis","100 milliseconds""1s","1 sec","1 second","1 seconds""1.5s","1.5 seconds"→ 1500ms"2m","2 min","2 minutes"→ 120,000ms
Duration Objects
Duration::from_millis(100)Duration::from_secs(1)
API Reference
Main Functions
smart_sleep<S: Into<SleepInput>>(input: S) -> Result<()>
The primary function that accepts various input formats and performs sleep if needed.
parse_sleep_duration(input: &str) -> Result<Duration>
Parses a duration string into a Duration object.
sleep(duration: Duration) -> Result<()>
Standard sleep function for backward compatibility.
Enum SleepInput
Represents different types of sleep inputs:
Number(isize)Text(String)Duration(Duration)
Examples
Basic Usage
use smart_sleep;
// Sleep for 100ms
smart_sleep.unwrap;
// Sleep for 2 seconds
smart_sleep.unwrap;
// No sleep (zero value)
smart_sleep.unwrap;
// No sleep (negative value)
smart_sleep.unwrap;
Advanced Parsing
use parse_sleep_duration;
let duration = parse_sleep_duration.unwrap;
println!; // 90 seconds
Custom Sleep Logic
use ;
let input = from;
if input.should_sleep
Error Handling
The library uses a custom Result<T> type and SleepError enum for error handling:
use ;
match smart_sleep
Features
default(enabled by default): All features enabledminimal: Minimal feature set without complex parsing
Performance
The library uses lazy static regex patterns for efficient parsing and avoids unnecessary allocations.
License
Dual-licensed under:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues on GitHub.
Changelog
See CHANGELOG.md for version history and changes.