pub fn run_with_timeout<F, T>(
dur: Duration,
f: F,
) -> Result<Option<T>, CoreError>Expand description
Executes a function with a timeout constraint
Spawns the function in a separate thread and waits for completion up to the specified duration. This prevents slow operations from blocking the main thread indefinitely.
§Arguments
dur- Maximum duration to wait for the function to completef- The function to execute with timeout protection
§Returns
Ok(Some(T))- Function completed successfully within timeoutOk(None)- Function timed outErr- Function panicked or returned an error
§Examples
use claude_code_statusline_core::timeout::run_with_timeout;
use std::time::Duration;
let result = run_with_timeout(Duration::from_millis(100), || {
Ok("Success".to_string())
});
assert!(result.unwrap().is_some());
let timeout = run_with_timeout(Duration::from_millis(10), || {
std::thread::sleep(Duration::from_millis(100));
Ok("Too slow")
});
assert!(timeout.unwrap().is_none());§Implementation Notes
- Uses channels for thread communication
- Catches panics and converts them to errors
- Thread is detached after timeout (may continue running)